2009-07-27 17 views
11

Come creare automaticamente una pagina WordPress (ad esempio, quando il plugin è attivato)?WordPress - creazione automatica della pagina

+0

cosa intendi esattamente? Vuoi codificare un plugin che crea una pagina usando l'API wp core? –

+0

sì, esattamente quello. Voglio creare una NUOVA pagina, non inserire quella esistente da qualche parte. – Phil

risposta

21

Usa wp_insert_post(), che possono inserire pagine così: http://codex.wordpress.org/Function_Reference/wp_insert_post

See post_type di seguito.

$post = array(
    'ID' => [ <post id> ] //Are you updating an existing post? 
    'menu_order' => [ <order> ] //If new post is a page, sets the order should it appear in the tabs. 
    'page_template' => [ <template file> ] //Sets the template for the page. 
    'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments. 
    'ping_status' => [ ? ] //Ping status? 
    'pinged' => [ ? ] //? 
    'post_author' => [ <user ID> ] //The user ID number of the author. 
    'post_category' => [ array(<category id>, <...>) ] //Add some categories. 
    'post_content' => [ <the text of the post> ] //The full text of the post. 
    'post_date' => [ Y-m-d H:i:s ] //The time post was made. 
    'post_date_gmt' => [ Y-m-d H:i:s ] //The time post was made, in GMT. 
    'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs. 
    'post_name' => [ <the name> ] // The name (slug) for your post 
    'post_parent' => [ <post ID> ] //Sets the parent of the new post. 
    'post_password' => [ ? ] //password for post? 
    'post_status' => [ 'draft' | 'publish' | 'pending' ] //Set the status of the new post. 
    'post_title' => [ <the title> ] //The title of your post. 
    'post_type' => [ 'post' | 'page' ] //Sometimes you want to post a page. 
    'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags. 
    'to_ping' => [ ? ] //? 
); 

// Insert the post into the database 
wp_insert_post($post); 
+1

Poiché le pagine sono semplicemente post contrassegnati come pagine. –

+0

Grazie. Più facile che ho pensato :) – Phil

+0

Inoltre, domanda del plugin newbie dev ... questa pagina diventerà quando attiverò il plugin o devo aggiungere del codice per specificare che voglio che il plugin faccia quella pagina al momento in cui è attivato? – Phil

-3

Wordpress fornisce il metodo API wp-> query per l'astrazione del database. È possibile creare la query appropriata per creare una pagina quando necessario.

+4

Questo è un pessimo suggerimento in generale. È necessario utilizzare la query solo se non è possibile ottenere lo stesso con una funzione API. Il motivo principale è che le future modifiche alla tabella potrebbero interrompere la query mentre le funzioni sono auspicabilmente aggiornate. –

Problemi correlati