0

I'm guessing this is not possible, as wp_head() is probably finished before the content execution, but here's a quick example of what I'm trying to do:

add_shortcode( 'header_content', function () {
    add_action('wp_head',function () {
        echo '<meta name="test" content="hello world" />';
    } );
} );

My actual use case is to have body content add JSON-LD to the header.

Is there a simple and elegant solution to this without a plugin?

2 Answers 2

3

No, by the time your shortcode runs, the header has already been sent over the network, even the post title. It is too late to change things already sent, aka everything before it reached the_content.

There is 1 way to "do it" sort of, but it carries a very, very high cost, and won't be reliable. Start an output buffer at the beginning of every page, then process the page at the end. However, this will:

  • Utterly cripple the TTFB/time to first byte, making your pages slow, and eliminating all progressive rendering at the client end, and parallelisation on the network side. This will probably have SEO ranking consequences
  • Require a shutdown hook, if this is never reached then nothing will be shown on the page, the user will see a completely blank page with no markup at all
  • Introduces overhead of string parsing HTML, which can get messy
  • It will require a supporting infrastructure to register all the stuff you want to add, otherwise if you try to flush the output buffers in the shortcode you won't have any way to use the shortcode a second time
  • Most of what you want to add will get stripped out by WordPress for not passing wp_kses_post on save. Fixing this will mean opening up major security holes

Fundamentally, this is not the approach to take to solve these problems

Instead, if you want to add JSON-LD to the header, you should use PHP code to add it on the wp_head hook, or if possible in the footer or further down the page. You shouldn't rely on shortcodes for anything other than embedding things into post content that can't be put directly into post content, e.g. iframes or fancy javascript features like interactive graphs

2
  • Thanks for confirming my intro. The rest was highly unnecessary. Mar 20, 2020 at 0:13
  • 1
    @DarioZadro the answer isn't just aimed at you, Stack Exchange is more like a knowledge base/wiki, it isn't a discussion forum, other people who have the same question will need a self contained, comprehensive answer. Otherwise i would have just left a comment. If I hadn't mentioned output buffers, somebody else would have
    – Tom J Nowell
    Mar 20, 2020 at 0:49
0

To add additional tags (like <style>, <script>, <meta>, or etc) to the <head> when a certain shortcode is used in the post, we can use the WordPress has_shortcode function:

function add_meta_tags() {
    global $post;
    if ( has_shortcode( $post->post_content, 'YOUR-SHORTCODE' ) ) {
    
    echo '<meta name="test" content="hello world" />';
    
    }
}
add_action('wp_head', 'add_meta_tags');

function shortcode_handler() {

    // some codes

}
add_shortcode('YOUR-SHORTCODE', 'shortcode_handler');

Your Answer

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

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