I have a plugin released, and one of my customers was reporting a problem where the plugin was outputting ads in the header area of their website.

I have a function defined which contains my shortcode definitions, for example, the function contains a few of these add_shortcode( 'myshortcode', 'my_function' );

I am executing the function via the following command add_action( 'init', 'add_my_shortcodes');

Doing it thisway causes a conflich with my plugin (which outputs adsense ads) and the google analytics in the header).

I was testing different hook positions for my add_action, and it appears that add_action( 'loop_start', 'add_my_shortcodes');

If my plugin is for outputting of ads, what is the best place to hook? I need it to be available anywhere in the template, or content. For example, they can put it in posts, sidebar widgets, or straight hard coded into the theme via a do_shortcode in their template files.

In order to ensure my shortcode is registered before any content is generated, but AFTER their header scripts, I moved my way down this list http://codex.wordpress.org/Plugin_API/Action_Reference until the ads worked properly. I ended up landing on the hook loop_start which doesn't sound like an ideal place to hook into.

Can anyone suggest the best hook location so that it is executed before any output, but after the opening <body> tag? I'm assuming I need to get as close to the <body> tag as possible, but I could be wrong.

  • 1
    Are you sure the problem is with the init hook, rather than with your shortcode callback itself? Please edit your question to include your shortcode callback. Commented Nov 21, 2013 at 16:27
  • When you say shortcode callback, do you mean the function in which the shortcode executes? It's very long and complicated. Actually broke across a couple functions. I am not certain that it's not part of my function. The fact that moving the hook location around seemingly fixes the issue leads me to think that their theme (genesis framework) has a conflicting hook. That's why I'm trying a different hook. The standard init hook works on dozens and dozens of other installations.
    – Branndon
    Commented Nov 21, 2013 at 16:40
  • If I'm understanding your question correctly, you have a shortcode defined, but you want it to execute outside of the post content? And you're trying to use a particular action hook to accomplish that? Commented Nov 21, 2013 at 16:42
  • Yes, I think so. Sorry, I don't know all the correct terms. My shortcode outputs adsense ads. If a customer wants to hard code it in their theme files with a do_shortcode command, they should be able to. The topmost location on the page where they should be allowed to insert ads is just after the opening body tag. Therefor I don't want my shortcodes to work if they are called in the <head> of the page. That's why I don't want to use the init hook, and I'd rather use a hook like body_open if such a hook existed.
    – Branndon
    Commented Nov 21, 2013 at 16:53
  • You may be right about my code conflicting. I know it has to do with their theme, but I'm going to see what I may be able to change in my shortcodes.
    – Branndon
    Commented Nov 21, 2013 at 17:04

1 Answer 1


If I'm following things correctly, I think the problem is that your shortcode callback echoes, rather than returns its content - and that you have it set up that way in order for the shortcode content to output outside of the post content.

If that's the case, you're taking the wrong approach.

Your shortcode callback must return, rather than echo, its output. By echoing its content, you're experiencing the issues you've observed:

I have a plugin released, and one of my customers was reporting a problem where the plugin was outputting ads in the header area of their website...

Doing it thisway causes a conflich with my plugin (which outputs adsense ads) and the google analytics in the header).

To use the shortcode outside of the post content, it must be executed via do_shortcode():

<?php echo do_shortcode( '[myshortcode]' ); ?>

How that do_shortcode() call gets added to the template in the correct location is entirely Theme-dependent. If the Theme provides an appropriate hook, then you can hook into it. If not, you may have to modify a template file via Child Theme.

  • Ah, fellow bearded helper! My shortcode does in fact return the content. I even tried ob_clean and return ob_get_clean() just to see if anything was being echo'd by mistake, and it wasn't. Also if it was echoing it, then many many more customers would be reporting the issue.
    – Branndon
    Commented Nov 21, 2013 at 17:13
  • Hmm... then I'm really struggling to follow your problem. Can you try to provide a bit more context: how exactly the shortcode is being used in the template, and what exactly the problem is? Commented Nov 21, 2013 at 17:20
  • Sorry Chip. Here is the problem awesomescreenshot.com/0341zpli55 you can see that there are two ads above everything, and it is inserted within their analytics code. This is how the site looks if I use the init hook. If I change the hook to loop_start this is what happens awesomescreenshot.com/0e11zply95 Nothing has changed other than the hook.
    – Branndon
    Commented Nov 21, 2013 at 17:36
  • And again we go back to the callback. Without seeing the actual code, it is nearly impossible to determine where the problem is. Commented Nov 21, 2013 at 17:38
  • Ok, well I'll work on my code. I'll report back if I get it fixed.
    – Branndon
    Commented Nov 21, 2013 at 17:48

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.