Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

Just for reference:

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

share|improve this answer
add comment

Your Answer

 
discard

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.