Wordpress Plugin - Delete a Page Created when deactivating

I tried to make a custom post type plugin for my own use, and managed to make a function that creates the page for it so far. What I want to do is to delete the said page when the plugin is activated. How should the code be?

This is my code for creating the said page upon plugin activation:

function create_video_pages() {
    $post = array(
          'comment_status' => 'open',
          'ping_status' =>  'closed' ,
          'post_date' => date('Y-m-d H:i:s'),
          'post_name' => 'videos',
          'post_status' => 'publish' ,
          'post_title' => 'Videos',
          'post_type' => 'page',
    );
    $newvalue = wp_insert_post( $post, false );
    update_option( 'vidpage', $newvalue );
}

Upvotes: 3

Views: 2483

Answers (2)

TeeDeJee
TeeDeJee

Reputation: 3741

Get the post_id from your vidpage option. Then use it to delete that post.

function deactivate_plugin() {

    $page_id = get_option('vidpage');
    wp_delete_post($page_id);

}
register_deactivation_hook( __FILE__, 'deactivate_plugin' );

Upvotes: 5

Avag Sargsyan
Avag Sargsyan

Reputation: 2523

You can do this using register_deactivation_hook and function wp_delete_post which deletes post with everything that is tied to it.

What about this?

function on_deactivating_your_plugin() {

    $page = get_page_by_path( 'about' );
    wp_delete_post($page->ID);

}
register_deactivation_hook( __FILE__, 'on_deactivating_your_plugin' );

Upvotes: 4

Related Questions