Delete Page From WordPress Database on Plugin Deactivation

Share this Article
Reading Time: 5 minutes
664 Views

Inside this article we will see how to delete page from wordpress database on plugin deactivation. Article contains very classified information about removing dynamic pages on plugin deactivation.

After this article you will be sure how to delete wordpress pages from tables. We usually remove pages when we create those pages for custom plugins. So on plugin removal we also need to remove dynamic pages too.

Apart from removing pages also we can add more functions which we want should be executed at the time of deactivation.

WordPress works on the basis of filters and hooks. WordPress also provides a deactivation hook. We will see here into complete detail.

Learn More –

Let’s get started.


Plugin Deactivation Hook

WordPress provide a hook for deactivation. We use this deactivation to drop all functions that we have added on plugin activation or theme activation.

register_deactivation_hook(__FILE__, callback);

callback function contains all code which will be executed when plugin/theme deactivates. We will add our logic here which removes all functions.


Plugin Setup

Here, we will create a plugin which generates a dynamic page on plugin activation and also when we deactivate it deletes that page too.

Create a folder myplugin inside /wp-content/plugins folder. Create plugin main file as myplugin.php

<?php

/**
 * Plugin Name:       OWT MyPage
 * Plugin URI:        https://sample-plugin.com
 * Description:       This plugin when activates, create a page inside database. When deactivate it removes the page.
 * Version:           1.0
 * Author:            Online Web Tutor
 * Author URI:        https://onlinewebtutorblog.com/
 */

function add_my_custom_page()
{
    // Create post object
    $my_post = array(
        'post_title'    => wp_strip_all_tags('My Custom Page'),
        'post_content'  => 'My custom page content',
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_type'     => 'page',
        'post_name'     => 'my-page-slug-custom'
    );

    // Insert the post into the database
    wp_insert_post($my_post);
}

// plugin activation hook
register_activation_hook(__FILE__, 'add_my_custom_page');

// plugin deactivation hook
register_deactivation_hook(__FILE__, 'deletepage_deactivation_function');

// callback function to drop table
function deletepage_deactivation_function()
{
    global $wpdb;

    $wpdb->query("DELETE FROM " . $wpdb->prefix . "posts WHERE post_name = 'my-page-slug-custom'");
}

$wpdb->prefix returns your database prefix. Like in most cases it will be wp_.

To remove dynamic generated page from database table –

global $wpdb;

$wpdb->query("DELETE FROM " . $wpdb->prefix . "posts WHERE post_name = 'my-page-slug-custom'");

Using page slug to remove wordpress page from database.

When we go inside wordpress admin. Plugins >> Installed Plugins

Click on Activate.

It will create a dynamic pages inside wordpress table. Page will be saved into {db_prefix}_posts table.

When you deactivate or uninstall this plugin, it will remove generated page from database.

We hope this article helped you to learn Delete Page From WordPress Database on Plugin Deactivation Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5