How can I convey short short text to wordpress

I am writing a Wordpress plugin that dynamically creates a string. I want to insert this line into the meta tag using wp_head hook.

My plugin works as follows: I define a short code whose handler function declares add_action(... , which adds a special <meta> to the header.

It works, BUT ...

My only problem is that I cannot figure out how to pass a variable containing a string to print in my head. The variable is not defined, although I globalize it in my plugin.

 //INSIDE MY PLUGIN... global $head_string; function initialize_my_plugin_class() { $head_string = "<meta>bla bla bla</meta>"; } function add_action('wp_head', 'add_meta_tags' ) //this is slightly oversimplified //the execution order of wp_head makes //the real code a bit more complicated //for the purposes of this question let's //pretend it like this. function add_meta_tags() { echo $head_string } 

The result works, EXCEPT that the variable $head_string empty. Thus, it prints an empty meta tag. (I know that everything else works, because I can change add_meta_tags() to do something like echo "FAKE META TAG"; and it appears where it should be in the header.)

And what happened to what I'm doing? I think it should include a variable scope, but I'm stuck.

0
source share
1 answer

Try setting $ head_string as global inside your functions.

0
source

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


All Articles