简体   繁体   中英

Wordpress: why is my global variable not being set?

I am running Wordpress 4.3.1. There are 2 plugins in question here: mine and some other author's.

I modified his code a bit to capture the permalink for a $post_id returned after using wp_insert_post into a global variable, like so:

// This is inside one public function of his...

$post_id = wp_insert_post($question_array);

        if($post_id){

            global $fd_success_post_id;
            $fd_success_post_id = get_permalink($post_id);

// The rest of his code does other stuff...not related...

I used the global variable $fd_success_post_id inside one of my PHP files, but when I check if it's set it says that it's not. Why might this be happening?

global $fd_success_post_id;

if(isset($fd_success_post_id)){
    echo $fd_success_post_id;
}
else{
    echo '$fd_success_post_id not set';
}

Any ideas?

It's because $fd_success_post_id is not defined. To make it work define as global there also

global $fd_success_post_id;

if(isset($fd_success_post_id)){
    echo $fd_success_post_id;
}
else{
    echo '$fd_success_post_id not set';
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM