I am working on a simple plugin (kind of maintenance plugin) where I am updating the homepage based on user page option choice.

function set_hp(){

$page = get_option('get_the_page');

if ( $page )
    update_option( 'page_on_front', $page );    
    update_option( 'show_on_front', 'page' );
add_action('wp', 'set_hp');

Now the homepage is updated with user selected one. Is there any way, when the plugin is deactivated, the previously saved homepage (* as set in reading.php front page*) is updated.

PS: I tried with

register_deactivation_hook( __FILE__, 'myplugin_deactivate' );

function myplugin_deactivate(){
    delete_option( 'page_on_front', $page );
    delete_option( 'show_on_front', 'page' );

But this deletes the homepage option and user needs to set this again.



The only way WordPress is going to know what the previous option was, is if you save it before you change it - since you're replacing the built-in option, it has no way of knowing how to get back to that.

So, you could add a custom 'backup' option that you restore on deactivation.

Something like this when your option is first set:

function set_hp(){

   update_option( 'wpse_231804_backup_page_on_front', get_option( 'page_on_front' ) );
   update_option( 'wpse_231804_backup_show_on_front', get_option( 'show_on_front' ) );

   // ... include the rest of your code here ...


With a simple reverse on deactivation:

function myplugin_deactivate(){

   update_option( 'page_on_front', get_option( 'wpse_231804_backup_page_on_front' ) );
   update_option( 'show_on_front', get_option( 'wpse_231804_backup_show_on_front' ) );


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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