• My plugin is loading twice, and I cannot understand why.

    Plugin design:
    The plugin uses a Singleton class to avoid loading twice.

    Verification:
    I added a simple error_log() to dump the debug_backtrace(). This debug outputs this identical stack twice (with different timestamps) in the log file:

    /wp-content/plugins/rover-idx/roveridx.php / Function: require_once / Line: 49
    /wp-content/plugins/rover-idx/roveridx.php / Function: __construct / Line: 38
    /wp-content/plugins/rover-idx/roveridx.php / Function: get_instance / Line: 53
    /wp-settings.php / Function: include_once / Line: 255
    /wp-config.php / Function: require_once / Line: 99
    /wp-load.php / Function: require_once / Line: 37
    /wp-blog-header.php / Function: require_once / Line: 13
    /index.php / Function: require / Line: 17

    Things I’ve tried:
    – I’ve found this to be happening on three websites. One was part of a multisite, and two were standalone.
    – Some folks who complain about this have determined that various plugins or the fact that they are running locally was the root cause. I’ve captured debug from three sites using my plugin. All are remote, and I’ve disabled all other plugins.
    – Made sure I wasn’t logged into WordPress – so admin initialization is not affecting this problem.
    – My plugin has the option to load content into the header. I’ve disabled it – but unfortunately still seeing the problem.

    Any suggestions?

    PS – I really hope this doesn’t get sucked into the black hole of this very busy forum.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter stevemullen

    (@stevemullen)

    More info:

    The difference seems to be in HTTP_ACCEPT. The 1st iteration has the value:

    [HTTP_ACCEPT] => [text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8]

    The 2nd iteration has the value:

    [HTTP_ACCEPT] => [image/webp,image/*,*/*;q=0.8]

    I have no way to detect that the plugin loaded once already. Can I just NOT load if the HTTP_ACCEPT is looking for an image?

    Moderator bcworkz

    (@bcworkz)

    I don’t think HTTP_ACCEPT is going to be a reliable indicator. But if you’ve done a lot of testing and it works for you, then go for it.

    From my own testing, I am seeing plugins reloaded due to internal redirects. Simply leaving the terminal slash off the URL is enough. But requesting a page with the terminal slash results in a single plugin load in my experience.

    Generally speaking, I don’t think it’s a good idea to assume any WP code will only execute once. If you’re counting something, there should be some mechanism installed to prevent mis-counting due to redirects or whatever. Maybe set a session variable, if it exists the execution is probably due to a redirect and should not be counted again. I don’t know if that will actually work, just an idea.

    This Hacks sub-forum is a lot less busy than the other forums, so it’s less of a black hole. OTOH, there are not too many regulars answering questions here, it takes a while to get an answer.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Plugin loading twice’ is closed to new replies.