I'm writing a simple WordPress plugin that uses shortcode. I want the page that contains the shortcode to have specific <meta>
tags. Is this possible? And if so, is there an elegant way to do it?
I know that I can add <meta>
tags with the wp_head
hook, but I want the meta tag content to match a string produced by the plugin. I could move all the code into the header, but then I'm not sure how to reference it later from the shortcode. In other words, when I declare a variable in the <head>
with a filter, it's not available to the class methods that I'm calling with the shortcode.
Any ideas?
UPDATE:
A nice solution was proposed in which the handler function for the shortcode adds the action to the wp_head hook:
add_shortcode('fakeshortcode', 'fakeshortcode_handler');
function fakeshortcode_handler() {
function add_meta_tags() {
//echo stuff here that will go in the head
}
add_action('wp_head', 'add_meta_tags');
}
This is swell, but the problem is that wp_head happens BEFORE the shortcode gets parsed and adds the action (so nothing gets added to the head with the code above ALONE). To make it work, I borrowed the solution in this post. It's basically a function that "looks ahead" into the post and sees if there is any shortcode coming. If it is, then IT adds the add_action('wp_head'...
.
EDIT: I removed my follow-up question about how to pass the variable. It's a new question here.
First attempt (don't use this... see the 'edit' below):
First, you need to set your shortcode with something like this:
add_shortcode( 'metashortcode', 'metashortcode_addshortcode' );
Then, you'll create the function in which you'll have to add a hook to wp_head
with something like that:
function metashortcode_addshortcode() {
add_action( 'wp_head', 'metashortcode_setmeta' );
}
Then, you'll define what you want to do in the wp_head
:
function metashortcode_setmeta() {
echo '<meta name="key" content="value">';
}
Adding the shortcode [metashortcode]
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.
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:
// 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 '<meta name="key" content="value">';
}
// 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->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' );
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With