I'm writing a Wordpress plugin which dynamically creates a string.
I want to insert this string in a meta tag using the wp_head hook.
My plugin works as follows: I define a shortcode whose handler function declares an add_action(... that adds a special <meta> tag into the header.
This works, BUT...
My only problem is that I can't figure out how to pass the variable containing the string to get printed into the head. The variable isn't defined, even though 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's like this.
function add_meta_tags() {
echo $head_string
}
The result works, EXCEPT that the $head_string variable is empty. So it prints an empty meta tag. (I know everything else works because I can change add_meta_tags() to do something like echo "FAKE META TAG"; and it shows up where it should in the header.)
So what's wrong with what I'm doing? I think it must involve variable scope, but I'm stuck.
Try setting $head_string as global inside your functions.
Related
I'm using one of the plugins inside Wordpress which needs to be rendered with PHP because I want to use it as shortcode inside WPbakery.
Developer instructions for displaying in custom location is this line of code:
<?php wccf_print_product_field(array('key' => ‘mykey’)); ?>
I tried to add this in shortcode function like:
function newshortcode1() {
wccf_print_product_field(array('key' => ‘mykey’));
}
add_shortcode( 'newshortcode', 'newshortcode1' );
But sadly this doesn't work no matter how I change this code.
I'm using [newshortcode] insite post to display it.
Any suggestions what I'm doing wrong here?
You are using ‘ (apostrophes) instead of ' (Single quotes).
So, update from:
wccf_print_product_field(array('key' => ‘mykey‘));
to:
wccf_print_product_field(array('key' => 'mykey'));
Firstly a shortcode function needs to return the desired output (It essentially replaces the shortcode text in the content). IF wccf_print_product_field returned the desired output, then you could do
return wccf_print_product_field
HOWEVER I strongly suspect that wccf_print_product_field prints or echo's the output directly when called. So it would be doing it when wordpress calls apply_filters to the page content, NOT returning it to the shortcode text.
So if you really must use it in a shortcode (if there is no other function that just returns the product field), then you will have to trap the output so you can return it in the shortcode. Something like:
function newshortcode1() {
ob_start(); /* catch the output, so we can control where it appears in the text */
wccf_print_product_field(array('key' => ‘mykey’));
$output .= ob_get_clean();
return $output;
If you view source on the page with the shortcode as it is now, I'd bet that you see your product field somewhere earlier in the page where it shouldn't be, possibly right at the start.
I'm trying to make a shortcode OUTSIDE POST/PAGE in WordPress.
The problem is, when I'm placing the do_shortcode() function, HTML automatically document the PHP code (e.g <!--*php code* -->).
What can cause the problem?
You don't actually place the do_shortcode() in your pages. Somewhere in your functions.php or shortcodes.php file you set up the function and assign a shortcode to the function.
function myFunction($params = array()) {
//function code here
}
add_shortcode('shortcodename', 'myFunction');
Then once that is done you can put [shortcodename] into your pages, inside square brackets just like that. There are some plugins that will allow you to run php and enter it right on the page, but the default shortcode functionality requires you to add the shortcode first.
I'm trying to create and if() statement that works in accordance to the kind of post (i.e., page or article) and, if page, the title of such page.
The type of post can be obtained from its class function post_class() and the title from its title function the_title().
So I know where I can get the info I need but, then, no matter what, I cannot turn this info into a string I can test. Wherever I put any of these two functions I get an output onto the page.
First, I tried:
if(strpos(post_class(), 'page')) {
//DO SOMETHING
}
Didn't work. Just had the post_class() dumped onto the page.
Then, I tried calling the function as the value of a variable:
$this_class = post_class();
And had the same result.
I've since tried a couple of other dirtier ways of doing it but to no avail. It seems wherever these WP functions are placed, they will dump their values onto the page.
Perhaps somebody out there knows how I can successfully get the type of post and title and set them to if() statements in order to trigger whatever else.
Thanks!
According to Wordpress Docs:
post_class()
When the post_class function is added to a tag within the loop, for
example >, it will print out and add
various post-related classes to the div tag.
In case you would like to be able to retrieve the value you should use
the get_post_class() function which returns that value.
Retrieve the classes for the post div as an array.
I have 2 actions:
add_action('init', 'activate');
add_action('wp_footer', 'code');
activate needs to set cookies and generate some html code out of it in a variable called $html
code then simply needs to output that code at the wp_footer along with other html code.
I noticed that $html is empty at code even though init has been called first and the variable should be filled with the right code. calling global $html didn't help, the variable is still empty.
How can I change it so $html has the right contents when code is called and is not empty?
It worked for me to define the variable $html globally, e.g. global $html;
I've been stuck on this for a while. I'm working on a wordpress site where I wrote the theme from scratch, I use php calls to get the wordpress functionality that I need in certain sections.
I'm trying to use a plugin, but calling it via
echo do_shortcode('[STORE-LOCATOR]');
just isnt working. Even when I switch to the default template and post that code, it still doesnt work. It simply echoes "[STORE-LOCATOR]"
Any help would be greatly appreciated.
[STORE-LOCATOR] is probably not a 'shortcode' in WordPress sense.
I encountered this on different plugin, Stream media player. They use the same syntax as shortcodes, but they are actually not.
Try using:
echo apply_filters( 'the_content',' [STORE-LOCATOR] ');
instead of do_shortcode, and see if it helps.
do_shortcode() returns a string.
I get it working by doing:
<?php echo do_shortcode(...); ?>
This is specific to the Store Locator plugin, not do_shortcode in general.
apply_filters can be an acceptable workaround for other plugins, but this does not work for Store Locator; you will only see an empty space and some controls. This is because it is looking for that shortcode in the page/post body to determine whether or not to include all of its js references at the top of the page. And without these references, nothing will work. See the sl_head_scripts function in sl-functions.php.
To change this behavior, simply modify that function to match based upon page title instead. In my instance I wanted it only on a "shop" page, so I commented out the entire $on_sl_page test and replaced it with this:
$on_sl_page = ( strpos($pagename, 'shop') === 0 );
I then called it from my page with apply_filters as indicated in the other answer:
echo apply_filters( 'the_content','[STORE-LOCATOR]');
And this appears to work perfectly.
echo do_shortcode('[STORE-LOCATOR][/STORE-LOCATOR]');
Try using shortcode after the WordPress environment has been set up.
function my_function() {
echo do_shortcode('[STORE-LOCATOR]');
}
add_action('wp', 'my_function');
If you're writing the whole thing from scratch, you'll want to make sure that the function you create is in the root php file of your plugin. The function might look something like this, but you'll have to sub in whatever logic you're using to arrive at the store location:
<?php
function doCoolStuff () {
$var1 = "value1";
$var2 = "value2";
$output = $var1+$var2;
}
return $output;
}
add_shortcode('SOTRE-LOCATIOR', 'doCoolStuff');
?>
Then in your template put the code:
<?php echo do_shortcode('[STORE-LOCATOR]');?>
Happy coding and good luck!