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:

Read More
// 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?

Related posts

1 comment

  1. 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';
    }
    

Comments are closed.