Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>First attempt (don't use this... see the 'edit' below)</strong>:</p> <p>First, you need to set your shortcode with something like this:</p> <pre><code>add_shortcode( 'metashortcode', 'metashortcode_addshortcode' ); </code></pre> <p>Then, you'll create the function in which you'll have to add a hook to <code>wp_head</code> with something like that:</p> <pre><code>function metashortcode_addshortcode() { add_action('wp_head', 'metashortcode_setmeta'); } </code></pre> <p>Then, you'll define what you want to do in the <code>wp_head</code> :</p> <pre><code>function metashortcode_setmeta() { echo '&lt;meta name="key" content="value"&gt;'; } </code></pre> <p>Adding the shortcode <code>[metashortcode]</code> should add your meta data as needed. The code was provided only to help you understand how to make it happen. It was not fully tested.</p> <p><strong>Edit : The previous code was just a concept and cannot work because of the execution order. Here is a working example that will get the expected result:</strong></p> <pre><code>// Function to hook to "the_posts" (just edit the two variables) function metashortcode_mycode( $posts ) { $shortcode = 'metashortcode'; $callback_function = 'metashortcode_setmeta'; return metashortcode_shortcode_to_wphead( $posts, $shortcode, $callback_function ); } // To execute when shortcode is found function metashortcode_setmeta() { echo '&lt;meta name="key" content="value"&gt;'; } // look for shortcode in the content and apply expected behaviour (don't edit!) function metashortcode_shortcode_to_wphead( $posts, $shortcode, $callback_function ) { if ( empty( $posts ) ) return $posts; $found = false; foreach ($posts as $post) { if ( stripos( $post-&gt;post_content, '[' . $shortcode ) !== false ) { add_shortcode( $shortcode, '__return_empty_string' ); $found = true; break; } } if ( $found ) add_action('wp_head', $callback_function ); return $posts; } // Instead of creating a shortcode, hook to the_posts add_action('the_posts', 'metashortcode_mycode'); </code></pre> <p>Enjoy!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload