0

Sorry for being such a noob. I'm sure this is an easy question for most. I'm trying to register activation and deactivation hooks for my plugin. In the docs, they warn about the activation callback not having access to global variables:


If you’re using global variables, you may find that the function you pass to register_activation_hook() does not have access to global variables at the point when it is called, even though you state their global scope within the function like this:

...

The main body variables are therefore in the scope of the activate_plugin() function and are not global unless you explicitly declare their global scope:

global $myvar;
$myvar = 'whatever';

function myplugin_activate() {

   global $myvar;
   echo $myvar; // this will be 'whatever'
}
register_activation_hook( __FILE__, 'myplugin_activate' );

So, following that advice, I set up my code like this:

global $post_id;
$post_id = 0;

function install_xxx() {
    global $post_id;

    $post = array(
        'post_title'    => __( 'Thank You', 'xxx' ),
        'post_content'  => __( 'Lorem ipsum dolor.', 'xxx' ),
        'post_status'   => 'publish',
        'post_name'     => 'thank-you',
        'post_type'     => 'page'
    );

    $post_id = wp_insert_post( $post, true );
    error_log('Inserted page: ' . $post_id);
}

function uninstall_xxx() {
    global $post_id;
    error_log('Removing page: ' . $post_id);
    $post = wp_delete_post( $post_id, true );
}

register_activation_hook( __FILE__, 'install_xxx' );
register_deactivation_hook( __FILE__, 'uninstall_xxx' );

But in my logs, it will echo something like:

  • Inserted page: 109
  • Removing page: 0

And, of course, the page isn't removed.

In other words, the global $post_id isn't being updated. Any clue what I'm doing wrong?

3
  • 2
    You're assuming that if you change the value of a global variable on one page, it is changed for all pages, that is not the case, and that is not the purpose of the global keyword. Remember, PHP and WordPress are loaded from a blank slate on every request, nothing is carried over, and everything is loaded from scratch. Global variables let you share variables across scopes within a single request, but they are not persisted. If you want to store the page ID on one request, and retrieve it on another, you need to save that ID in the database
    – Tom J Nowell
    Apr 20, 2021 at 15:15
  • thanks @TomJNowell, that's a very useful concept to understand. heh, i'm not even a PHP developer, they just put me on this project because there's no one else in the company. so, is add_option() the preferred way to persist this value? or direct DB manipulation?
    – T Nguyen
    Apr 21, 2021 at 1:14
  • 1
    if you find yourself writing SQL while working on WordPress that's usually a warning sign that what you're doing is not a good idea. Always use the APIs available first
    – Tom J Nowell
    Apr 21, 2021 at 8:44

1 Answer 1

0

Thanks to @Tom J Nowell's help, I fixed my code to persist the created page ID to Options:

function install_xxx() {    
    $post = array(
        'post_title'    => __( 'Thank You', 'xxx' ),
        'post_content'  => __( 'Lorem ipsum dolor.', 'xxx' ),
        'post_status'   => 'publish',
        'post_name'     => 'thank-you',
        'post_type'     => 'page'
    );

    $post_id = wp_insert_post( $post, true );
    add_option( 'xxx_thankyou_page_id', $post_id );
    error_log('Inserted page: ' . $post_id);
}

function uninstall_xxx() {
    $post_id = get_option( 'xxx_thankyou_page_id' );
    error_log('Removing page: ' . $post_id);
    if ( $page_id ) {
        $post = wp_delete_post( $post_id, true );
    }
}

register_activation_hook( __FILE__, 'install_xxx' );
register_deactivation_hook( __FILE__, 'uninstall_xxx' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.