0

Am I using add_action('init' correctly?
I want to display some data if a user visits example.com/?my_plugin

At the moment I'm using...

<?php
add_action( 'init', 'my_plugin' );

function my_plugin()
{
     if( isset( $_GET['my_plugin'] ) ) {
          ...
          echo $data;
     }
}

This will run every time any page on the blog is loaded, as I understand it. Does that present a performance issue?

Is there a better way I could accomplish this?

2 Answers 2

2

Performance impact of hooked functionality is determined by how often hook fires and how intensive the operation is.

init only ever fires one per load, so multiple runs are not a factor for it.

Mostly the thing you need to pay attention to is context. If your logic fires on every load and result is conditional the first thing it should do is determine if the context is the one you want. In all other cases that it the only thing it should do.

As long as your context check is lightweight the performance impact should be perfectly insignificant.

If your context check is heavy for some reason you might want to find a more specific hook (such as those in template loader logic) that would fire less and in more narrow circumstances. But for something as simple as example you made that won't be necessary.

0

init hook runs after WordPress has finished loading but before any headers are sent.

if your action is not involving any complex query or not trying to call too many extra files then you don't need to be worried.

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.