Delete Page From WordPress Database on Plugin Deactivation

<?php

/**
 * Plugin Name:       EYE MyPage
 * Plugin URI:        https://templateeye.com
 * Description:       This plugin when activates, create a page inside database. When deactivate it removes the page.
 * Version:           1.0
 * Author:            deshisoft
 * Author URI:        https://templateeye.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'");
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>