The MU forums have moved to WordPress.org

Detecting a plugin is running in a widget (25 posts)

  1. dizzy99
    Member
    Posted 17 years ago #

    Ok heres the deal.

    I have a plugin that has a sidebar widget, so surprises there.

    The plugin in question forced you to manually install the sidebar widget if thats what you want. As this is MU and users don't have access to theme templates, they are bound to use the sidebar widget.

    So instead of causing them confusion i copied that sidebar widget into the widget directory and deleted it within the plugin. Great, it functions as it should and now the user isn't forced to click "install the widget" in the plugin management screen.

    Now, i thought, if i checked for the plugin, i could show or hide the widget accordingly. No problems i thought, i'll just check for it as i would in a template.

    So

    '<?php if (function_exists('useronline')): ?>
    ------ DO THAT WIDGET STUFF DONT YA KNOW
    <?php endif; ?>'

    However this doesn't work for some reason. Am i missing something here ?

  2. lunabyte
    Member
    Posted 17 years ago #

    Simple. Just call the widget file as an include from the main plugin file.

    To keep it from showing up as a stand alone widget, put in the top of the widget file:

    define('whatever', '1');

    Then in the widget file, do this:

    if ( !defined('whatever') ) {
    return;
    }

    (note, tab the return line in.)

    Then, if the widget file isn't included by the plugin file, the code won't run.

    // Useronline plugin, huh? ;)

  3. dizzy99
    Member
    Posted 17 years ago #

    Little lost here :)

    Lets see if i understand this right.

    In my main plugin file i inserted

    'define('whatever', '1');'
    'include "mywidget";'

    Just after all the remarks

    and then in the widget, which is back to residing in the same directory as the plugin

    'if ( !defined('whatever') ) {
    return;
    }'

    At the top of the widget, above the remarks.

    This worked and included the widgets plugin but still shown the options to enable it.

    I could, i guess, just include the widget function within the body of the main plugin and then call the widget with a 'add_action('plugins_loaded', 'widget_useronline_init')' call but i think i'm getting confused where to insert such a call when the plugin is active.

  4. lunabyte
    Member
    Posted 17 years ago #

    Hmm. Could be that the widget code is read, and not executed when putting it in the drag/drop area.

    Although, with the return set, it "shouldn't" execute anything. So trying to configure it "shouldn't" work. So, at the least, you could kick the function that spits out the widget options and make it an if/else. If !function_exists, then run a message about having to enable the plugin first. or else, show the configuration options.

    If it's dragged onto the sidebar, with the return in there, it shouldn't do anything when a page is loaded.

  5. lunabyte
    Member
    Posted 17 years ago #

    Crap, I just thought about this.

    The actions that set-up the widget functions.

    Putting those in an if/else could do the trick, and checking for the defined variable.

  6. dizzy99
    Member
    Posted 17 years ago #

    woohoo i just solved it :)

    Ok its really rather simple.

    There is a trigger event activated when the plugin running .

    I hope i can explain this right anyway, here goes.

    Say my plugin has a main function called 'useronline();' the way a plugin author seems to detect that is with an add action

    so

    'add_action('plugins_loaded', 'useronline_init');'

    So the plugin has initialised and loaded.

    Its the same for widgets actually so in this case it was simply a matter of copying the entire function and initialisation into my plugins main php file for this to work

    At the end, thankfully, and what put me on the right track, was the widget call checking to see if that was running.

    'add_action('plugins_loaded', 'widget_useronline_init');'

    Well this plugin is already running by the time it gets to that as its been initialised, bingo. Activating the plugin brings in the widget, de-activiating means the statement above is false and thus the widget goes away.

    So now my members won't have to activate the plugin AND the widget as this loads it on state. Brilliant.

    I hope that explains it to some people cos i got myself a little confused there trying to get what i think its doing out for everyone to understand :) Apologies if its not clear.

    Now i'm wondering if i can put this into best practice for plugins that i only want output on the sidebar.

    You know the types of plugins where you have to edit the template to insert the function tag. I wonder if can just wrap a widget around it, avoiding all those edits and give the user flexibility on where on the sidebar to put it.

    I wonder if i could just include the template call in the widget instead of the template. Any thoughts ?

  7. Ovidiu
    Member
    Posted 17 years ago #

    if you were talking about the user online plugin, could you make your modified/unified plugin/widget solution available? maybe here or at wpmudev?

    after reading your detailed posts here I think this would be an elegant solution for the event calendar plugin / widget too :-)

  8. dizzy99
    Member
    Posted 17 years ago #

    Yes i was talking about the user online plugin :)

    Certainly. I'll package it up tomorrow morning and see how to submit it at wpmudev.

    One thing i was going to work on was creating a page with the tag it defines for the purpose.

    At the moment you have to manually create the page, which is pretty normal, and insert the tag. But having seen it implemented in Simple Forum i'd like to have a go at creating it by default.

    It allows you and other members to click Users Online and it goes to a page, showing who they are (if known) and more importantly, where they are.

    I think i can take the code from Simple Forum to do this on its first initiate so i'll look into that.

    something like

    ' $wpdb->query(
    "INSERT INTO {$table_prefix}posts ( post_status, post_title, post_name, post_type, post_author, post_content, comment_status, ping_status )
    VALUES ( 'publish', 'Users Online', 'useronline', 'page', '1', '[page_useronline]', 'closed', 'closed' )"
    );'

    The only issues i can see here is its created with the Admin and not the owner of the blog. Anyone know how to detect the blog admins ID ? And i don't know how to insert unix dates and times so i left that out :)

    The less burden on my members the better is what i say :)

    I also removed the ajax javascript function call because firstly it was defaulting to blog ID 1 and i couldn't work out why and secondly it was pretty useless i feel and not something that is usual for this type of thing.

    Now can you link me to this events calendar ? Because i was going to use RS events but if there's better I'm up for that :)

  9. Ovidiu
    Member
    Posted 17 years ago #

    here you go for the event calendar: http://wpcal.firetree.net/

  10. dizzy99
    Member
    Posted 17 years ago #

    Thanks for the calender links, i'll certainly check that out.

    OK i got the plugin automatically creating the page it needs to output some handy info, authored by the admin. The only issue i'm having is with dates (i'm no php or mysql coder).

    At the moment the page is being created with default unix time (because we are not setting anything).

    I thought i could use 'current_time('timestamp')' and i probably can but this returns the current time (including whatever offset you set in your options) in unix time and no, as the page posts want, real world time.

    We need to insert something like '2006-12-24 19:33:53'

    Apart from that, it seems to be working well.

    One caveat is that if you de-activate the plugin and re-activate it without deleting the created new page, it will create another one. I'm sure theres likely a very easy way to search the database for the string "Users Online" and then only insert the new page creation query if we don't find it. But as i say, i'm no coder so this is all winging and praying for me :)

    I won't post this to wpmudev at the moment because, frankly, i am not entirely sure its working 100%. But once we get this date right and put a check in the page creation process, i can host a link to download for testing.

    Please bare in mind that i'm learning on the job and kinda hit the ground running with wordpress :) I've only been with the system two weeks.

  11. dizzy99
    Member
    Posted 17 years ago #

    Well aint i the one having a crash course :)

    Got the page checking working so it now checks if the page title exists and if it does, it doesn't create a new one.

    This was pretty simple actually.

    ' $test = $wpdb->query ("SELECT * from {$table_prefix}posts WHERE post_title='Users Online'");'

    And then a simple 'if (!test) {create a new page routine}';

    Going to need some help on unix dates though as that just plain confuses me.

    I'm getting worried that i'm actually starting to understand this stuff :)

  12. dizzy99
    Member
    Posted 17 years ago #

    Ok got it :) I'm so pleased with myself i may break out into a smile :)

    For reference, and any newbie like me that didn't know, you can use the built in 'current_time()' function.

    So now all i do is insert into post_date, post_date_gmt, post_modified and post_modified_gmt the current active time plus the GMT offset.

    So that would be

    'current_time('mysql',1)'

    It works :)

    I'll package this up and provide a link for testing but it appears to be working for me :)

  13. dizzy99
    Member
    Posted 17 years ago #

    OK as promised heres the revised plugin

    http://nptgroups.co.uk/useronline.zip

    I'll leave the upload up for a few days to make sure everything is working as it should.

    Heres what i have fixed or altered.

    • Widget is now combined and activated only on plugin activation
    • plugin checks for a User Online Page and creates it if its not there on plugin activation
    • Disabled Ajax refresh as it was pointless and i couldn't get it to work
    • Adjusted the timestamp on Users Online page and Admin panel to reflect GMT offset time instead of server time

    Let me know how you get on.

  14. Ovidiu
    Member
    Posted 17 years ago #

    ok, here is my feedback:

    unzipped it into plugins, it created a folder for itsself called useronline then I activated it and got this error:


    WordPress database error: [You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CHARSET=latin1' at line 1]
    CREATE TABLE wp_1_useronline ( timestamp int(15) NOT NULL default '0', userid int(10) NOT NULL default '0', username varchar(150) NOT NULL default '', displayname varchar(255) NOT NULL default '', useragent varchar(255) NOT NULL default '', ip varchar(40) NOT NULL default '', location varchar(255) NOT NULL default '', url varchar(255) NOT NULL default '', type enum('member','guest','bot') NOT NULL default 'guest', UNIQUE KEY useronline_id (timestamp,username,ip,useragent)) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    Warning: Cannot modify header information - headers already sent by (output started at /var/www/web5/web/wp-includes/wp-db.php:111) in /var/www/web5/web/wp-includes/pluggable.php on line 281

    any ideas?

    I left it like this and now wherever I click in the admin menu, I get these arrors on top of the admin are:

    WordPress database error: [Table 'web5_db1.wp_1_useronline' doesn't exist]
    DELETE FROM wp_1_useronline WHERE userid = '1' OR (timestamp < 1168604493)

    WordPress database error: [Table 'web5_db1.wp_1_useronline' doesn't exist]
    INSERT INTO wp_1_useronline VALUES ('1168604793', '1', 'admin', 'ovizii', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0', '62.159.242.114', 'zice.ro', '%2Fwp-admin%2Fplugins.php', 'member')

    WordPress database error: [Table 'web5_db1.wp_1_useronline' doesn't exist]
    SELECT COUNT(*) FROM wp_1_useronline

  15. Ovidiu
    Member
    Posted 17 years ago #

    I deleted this part: DEFAULT CHARSET=latin1;

    now it works but I realized the usersonline page does not get deleted when I deactivate the plugin...

  16. dizzy99
    Member
    Posted 17 years ago #

    I didn't add that part, that's part of the original plugin and seems to work here. It may not be supported perhaps in different sql versions ? Maybe an idea to drop it entirely incase of issues.

    And no the page doesn't get deleted when you de-activate the plugin as i don't know how to detect that :) Its the same as not removing the database table when de-activated. Unless you write a clean up i don't know if there's an easy way around that.

    But it checks to make sure the page is there on init and thats the most important part.

    Apart from that is it working ok for you ?

    Check times as thats the most important part that i tried to address. The plugin was using server time instead of control panel time.

  17. dizzy99
    Member
    Posted 17 years ago #

    Ah it is setting MyISAM database engine.

    'ENGINE=MyISAM DEFAULT CHARSET=latin1;' so it should be safe to remove that part of the sql as its usually not needed (apparently).

    I've adjusted the download.

  18. Ovidiu
    Member
    Posted 17 years ago #

    its using server time, check it out here: http://zice.ro/useronline/

    and I had another problem, I had to refresh maybe 10 times until that page showed up, between that I did resave the permalinks, now it works.... I dunno why the page did not show me the useronline stuff, it was showing my frontpage instead???

    the widget works great though

  19. dizzy99
    Member
    Posted 17 years ago #

    I had something very similar the very first time i tried to create the page and then it went and i haven't been able to reproduce it so no idea. I tried it on 3 blogs on my server and they all worked fine after that very first issue.

    As i say i'm no coder, just hackling together bits and pieces.

    When you say server time do you mean your options time or your actual server time ? Its showing as GMT time for me

    '1 Member Online Now

    #1 - admin on 12.01.2007 @ 13:18
    zice.ro [url]
    1 Guest Online Now

    #1 - guest on 12.01.2007 @ 13:19
    zice.ro ยป Users Online [url]'

  20. Ovidiu
    Member
    Posted 17 years ago #

    well the times above are server times, the options time is +2 h so it is not working...

    my settings are these:

    Time zone name: EET
    Time zone offset: +0200
    Date and time: Fri, 12 Jan 2007 15:32:34 +0200 (EET)

  21. dizzy99
    Member
    Posted 17 years ago #

    Ah i'm out of my league then as i have it set to GMT by the looks of it and couldn't find out how to pull the actual options time.

  22. dizzy99
    Member
    Posted 17 years ago #

    Ok i think i have found the issue with the page creation. We wasn't creating a default category so i think that was it. I've changed the routine as well to make it easier to re-use in other code and updated the zip.

    As regards the UTC, GMT timezone thing this seems an issue with a variety of plugins actually and i still haven't a solution :(

    The additions i made work perfectly for me in GMT but will offset for anyone outside of that.

    What you could do is change these additions back so search for

    current_time('timestamp',1)

    and replace with

    current_time('timestamp')

    That may help you, but won't help me :)

    If anyone knows how to detect the Times in the weblog should differ by option and adjust the timestamp accordingly in a plugin please let me know.

  23. billT
    Member
    Posted 17 years ago #

    Everything works fine for me. Mahalos

  24. dizzy99
    Member
    Posted 17 years ago #

    I found that the timezone issue was actually my server being in one location and me in another, causing an offset. So i reverted the plugins timezone BACK to its original and all is well.

    The zip has been updated too.

    Sorry for the confusion :)

  25. maka1
    Member
    Posted 15 years ago #

    hey dizzy99, are you still on that thing? is it working for you now? where can i get the plugin? :)

About this Topic