WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.";
share|improve this question
up vote 1 down vote accepted

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...

share|improve this answer
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 Aug 21 '12 at 21:55
I think a guided process would be more suitable. As then the become more WordPress savvy. Its a tough one really. – Barry Carlyon Aug 22 '12 at 18:04

Just for reference:

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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