I'm developing a custom plugin which connects to salesforce and needs to check that the connection hasn't expired on each page refresh. If it does expire, it reconnects etc.

So I am doing this on an init action, but I've noticed a very significant performance hit doing it this way. Particularly loading images from the media library. My understanding now is that init fires with every request, so on a page with 20 images, it will fire at least 20 times??? Is this correct?

So obviously init isn't the best for this purpose, what should I be using?


  • Init fires with every request, even admin-ajax.php (which wp admin checks regularly for auth verification). Such remote requests should make the site slow and it would be better to process them via AJAX.
    – Ismail
    Apr 7, 2017 at 13:07
  • Images are physical files, WordPress isn't loaded to serve requests for physical files.
    – Milo
    Apr 7, 2017 at 15:24
  • So I'm a bit confused by what constitutes a 'request' then, I put a trivial counter in my init script which just increments a $_SESSION variable and by the time I get to the foot of my page my counter is on 7. So init is firing 7 times on 1 page load. I assumed this was because I am calling wp_get_attachment_src a few times - but maybe not?
    – the_lar
    Apr 8, 2017 at 10:00

1 Answer 1


If you want your function to fire only once, you can use other hooks.

wp_head might be useful in your situation since it is fired before the rest of the template is rendered.

template_redirect is also fired before rendering the template.

If the sequence doesn't matter to you, you can fire it at last using wp_footer or get_footer hook.

  • 1
    Thanks @jack, I used 'wp' and that seemed to do the trick to - are there any things I should be aware of using that do you think?
    – the_lar
    Apr 8, 2017 at 10:03
  • This hook runs right after WP class, so in some situations it might be too early to do whatever you are trying to do. If it's working fine for you, then no further action is required.
    – Johansson
    Apr 8, 2017 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.