Delete Page From WordPress Database on Plugin Deactivation

<?php

/**
 * Plugin Name:       EYE MyPage
 * Plugin URI:        https://deshisoft.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://deshisoft.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'");
}
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments