Im working on a little plugin that when activated creates a theme page and then a function sets this page to published this is the code i have for published:

// function that creates the new ads page on plugin install // 
function mjj_create_page ()
// Create new page object
$ads_page = get_option('mjj_smart_ads_page');

$ads_new_page = array(
    'post_title' => 'Smart Ads',
    'post_content' => '',
    'post_status' => 'publish',
    'post_type' => 'page'  
// Insert the page into the database
$ads_page = wp_insert_post( $ads_new_page );
update_option('mjj_smart_ads_page', $ads_page);

// now lets give this new page a groovy template
$ads_page_data = get_page_by_title('Smart Ads');
$ads_page_id = $ads_page_data->ID;
update_post_meta($ads_page_id, '_wp_page_template','tpl-smart-ads.php');


dont know if its semanticaly wp perfect but it seems to work ok and does what i need, however now im trying to set a function that will on plugin deactivate set this created page to draft status so it doesnt show in the menu, but it just doesbt seem to want to play, heres what im working with:

// function that drafts smart ads page on plugin deactivate // 
function mjj_unpublish_page ()
$old_ads_page = get_option('mjj_old_smart_ads_page'); 

$ads_old_page = array(
    'post_title' => 'Smart Ads',
    'post_content' => '',
    'post_status' => 'draft',
    'post_type' => 'page'  
     // Insert the page into the database
$old_ads_page = wp_update_post( $ads_old_page );
update_option('mjj_old_smart_ads_page', $old_ads_page); 

and below are my hooks

    // create the page to get the info for selling ads and posting ads
    register_activation_hook($file, array(&$this, 'mjj_create_page'));
    //while in this block i will also add the deactivate function to unpublish the created page
register_deactivation_hook($file, array(&$this, 'mjj_unpublish_page'));

as you can see im thinking that setting to draft will include the use of wp_update_post, but it dont seem to be working right when deactivating, im ending up with one Smart Ads page published and one in draft

  • When you update the option with the page id on creation you use an option named mjj_smart_ads_page and when you are trying to change the status you all calling an option named mjj_old_smart_ads_page , is that a typo? is that the problem? if not then what is mjj_old_smart_ads_page? – Bainternet Apr 21 '11 at 13:19
  • Hi Bainternet, thanks for taking the time out to post on the problem/solution, i have managed to get around it, however the stackexchange system wont let me post the code solution as an answer for another 3 hours, but briefly what i did was find the page id by its title and update status that way in just 4 lines of code, when i can publish my answer i will also publish the code, thanks again – MartinJJ Apr 21 '11 at 14:10
  • Glad you found a solution – Bainternet Apr 21 '11 at 14:39
  • thanks, its a temporary solution though i feel, if user changes post title it could mean that it fails, once i have the plugin working 100% i will have to look at a way that covers this eventuality... cos its bound to happen...lol – MartinJJ Apr 21 '11 at 15:09
  • I added an answer that is not title depended. – Bainternet Apr 21 '11 at 15:19

I'm posting this as another solution for you and its based on the page id

$post_id - The ID of the post you'd like to change.
$status -  The post status publish|pending|draft|private|static|object|attachment|inherit|future|trash.
function change_post_status($post_id,$status){
    $current_post = get_post( $post_id, 'ARRAY_A' );
    $current_post['post_status'] = $status;
    return wp_update_post($current_post);

So once you have this function you can use it with the page id of the page you created:

$ads_page = get_option('mjj_smart_ads_page');
$old_ads_page = change_post_status($ads_page,'draft');
update_option('mjj_old_smart_ads_page', $old_ads_page); 
  • Glad i could help. – Bainternet Apr 21 '11 at 15:45
  • You sure have helped, all these bits of code are a heap of help and not to mention a great insight, i have built around 30 wp themes of one kind or another, but from themes a leap into plugins is a new ball game for me, quite exciting realy in a nerdy sort of way... – MartinJJ Apr 21 '11 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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