Use WordPress Short Code to Add <meta> Tags

I am writing a simple WordPress plugin that uses shortcode. I want the page containing the short code to have special <meta> tags. Is it possible? And if so, is there an elegant way to do this?

I know that I can add <meta> tags using wp_head hook, but I want the contents of the meta tag to match the line created by the plugin. I could move all the code to the header, but then I'm not sure how to reference it later from the short code. In other words, when I declare a variable in a <head> with a filter, it is not available for class methods that I call using short code.

Any ideas?

UPDATE:

A nice solution was proposed in which the handler function for short code adds an 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 a swelling, but the problem is that wp_head happens before the short code gets parsed and adds an action (so nothing is added to the head with the code above ALONE). For this to work, I borrowed the solution in this post . This is basically a function that โ€œlooks aheadโ€ to the message and sees if there is any short code. If so, then IT adds add_action('wp_head'...

EDIT: I removed the question of how to pass a variable. This is a new question here .

+7
source share
1 answer

First try (do not use this ... see below "edit") :

First, you need to set your shortcode something like this:

 add_shortcode( 'metashortcode', 'metashortcode_addshortcode' ); 

Then you will create a function in which you will need to add a hook to wp_head with something like this:

 function metashortcode_addshortcode() { add_action( 'wp_head', 'metashortcode_setmeta' ); } 

Then you determine what you want to do in wp_head :

 function metashortcode_setmeta() { echo '<meta name="key" content="value">'; } 

Adding [metashortcode] should add your metadata as needed. The code was provided only to help you understand how to do this. It has not been fully tested.

Change: the previous code was just a concept and cannot work due to 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 it!

+12
source

Source: https://habr.com/ru/post/909953/


All Articles