Domanda

sto lavorando su un piccolo plug-in che, quando attivata crea una pagina a tema e poi una funzione imposta questa pagina ad pubblicata questo è il codice che ho per pubblicato:

// 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');

}

Non so se la sua semanticaly wp perfetto, ma sembra funzionare bene e non quello che mi serve, ma ora nel tentativo di impostare una funzione che il plug-in di disattivazione impostata questa pagina creata per stato di bozza in modo che non mostra nel menu, ma semplicemente doesbt sembrano voler giocare, ecco che cosa im che lavorano con:

// 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); 
}

e di seguito sono i miei ganci

    // 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'));

come si può vedere di pensare im che l'impostazione di progetto prevede l'utilizzo di wp_update_post, ma non sembrano funzionare a destra quando la disattivazione, im finendo con una pagina di Smart annunci pubblicati e uno nel progetto

È stato utile?

Soluzione

Sto postando questo come un'altra soluzione per voi e per la sua base nella pagina 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);
}

Quindi, una volta che avete questa funzione si può utilizzare con la pagina id della pagina è stato creato:

$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); 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top