I'm creating a plugin that will need to add a new WP page upon activation and then I want it to remove that same page upon uninstallation. Do I have to manually store that page id in the DB somewhere or is there a native WP option to remember that automatically?

My code so far:

register_activation_hook(__FILE__, 'create_page');
register_uninstall_hook(__FILE__, 'remove_page');

function create_page() {
    global $user_ID;

    $page['post_type']    = 'page';
    $page['post_content'] = 'Page content will go here.';
    $page['post_parent']  = 0;
    $page['post_author']  = $user_ID;
    $page['post_status']  = 'publish';
    $page['post_title']   = 'Page Title';
    $pageid = wp_insert_post ($page);
    if ($pageid == 0) {
        echo "Something went wrong. Could not successfully add a new page.";

2 Answers 2


Why do you need to create a page?

Its possible that before plugin deactivation, the page no longer exists, so your deletion function will need to check for if it exists

But yes you will need to store the ID somewhere. Best option is to just update_option('icreatedapage', $pageid);

if ($pageid == 0) {
    echo "Something went wrong. Could not successfully add a new page.";
} else {
    update_option('somename_relatedtoyour_plugin_name', $pageid);

Tho what do you do if you page fails to create? You will need to considering including instructions on how to manually create it, or let the user do it themselves...

  • Thanks. I was thinking of having an options page where the user can just select any page where this custom code will be inserted, but the users won't be WP-savvy so I wanted to simplify the process. I'll start with this and see how that goes.
    – zen
    Commented Aug 21, 2012 at 21:55
  • I think a guided process would be more suitable. As then the become more WordPress savvy. Its a tough one really. Commented Aug 22, 2012 at 18:04

Just for reference:

A file called uninstall.php will be run automagically when the plugin is deactivated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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