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.