Send variables between action calls - php

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;

Related

I am new to WordPress but I cant seem to add php code?

So I wrote a script which checks if there is a session with a certain name or not and echoes hello world but after switching to WordPress I wasn't able to use any of my old scripts. Whenever I type them it thinks they are text and just prints it. So I tried a couple of stuff like a plugin that I could add my script in and it produces a shortcode that I can use. I tried that but it didn't worked. I can't find what is happening.
Are there some sort of logs I can refer to? If someone knows the fix please help (I'll leave the code down below). result-id session is a msg and result session is another message.
<?php
if(isset($_SESSION['result-id'])){
echo("hello world");
else if(isset($_SESSION['result'])){
echo $_SESSION['result'];
}
session_unset();
?>
To start using sessions in WordPress you need to start it using an action hook:
function start_wp_session(){
if( !session_id() )
session_start();
}
add_action('init','start_wp_session');
If you want to custom code in WordPress, first you must create a child-theme of your currently active theme. Then in the function .php file of your child theme folder, you can write any of your code. But you cannot start writing directly in function.php file. You must bind your code in a custom function, and then hook that function with any hooks present in the WordPress.
Example hooks- wp_header, wp_footer, init, etc.
See the example below.
function custom_function(){
echo "Hello World!";
}
add_action('wp_head','custom_function');
You can wrap any code inside the custom function.

Passing a variable to template file in wordpress plugin

In my wordpress plugin I have a function which has a page id:
public function create_view_for_pdf() {
$page_id = $_POST['page_id'];
$this->template_shortcode('template.php');
}
here, "template_shortcode" function includes a template located in a folder in the plugin directory.
private function KendoPdf_template_shortcode($template_name) {
return include '/template/dir' . $template_name;
}
In the template file, I want to pass the page id so that I can print content there. How can I do that?
Note: Since I am just including the template file, I thought I will get the $page_id variable there normally. But it did not work. I needed the page id in template file because the content will be different than the actual page. That page also has ACF fields. I am basically creating a new template for pdf export. That's why I cannot use all the content of that page.
Please correct me if I am wrong but why would you pass the the page_id to the template file if it is within $_POST? Just access the variable using $_POST['page_id']; within your template file.
Instead of including your template file you could also read it into a string with file_get_contents(); and do your desired replacements before you return it.
Another possibility would be a global variable which is already set: global $post;
And last but not least you could use output buffering with ob_start(); (and consecutive functions).
You see: A lot of ways to solve this.

Trying to use WP shortcode in HTML

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.

How can I pass a variable wordpress shortcode

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.

do_shortcode not working

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!

Categories