A volte nei plugin è necessario creare delle pagine, questa classe singleton che ho scritto serve proprio per questo scopo.
La classe deve essere inserita in un plugin.
<?php define( WPIT_EVENTS_PAGE_VERSION_NAME , 'wpit-events-page' ); define( WPIT_EVENTS_PAGE_VERSION_NUMBER, '1.1'); class Wpit_Add_Page { //A static member variable representing the class instance private static $_instance = null; /** * Wpit_Add_Page::__construct() * Locked down the constructor, therefore the class cannot be externally instantiated * * @param array $args various params some overidden by default * * @return */ private function __construct() { add_action( 'init', array( $this, 'update_page_check' ) ); } /** * Wpit_Add_Page::__clone() * Prevent any object or instance of that class to be cloned * * @return */ public function __clone() { trigger_error( "Cannot clone instance of Singleton pattern ...", E_USER_ERROR ); } /** * Wpit_Add_Page::__wakeup() * Prevent any object or instance to be deserialized * * @return */ public function __wakeup() { trigger_error( 'Cannot deserialize instance of Singleton pattern ...', E_USER_ERROR ); } /** * Wpit_Add_Page::getInstance() * Have a single globally accessible static method * * @param mixed $args * * @return */ public static function getInstance( $args = array() ) { if ( ! is_object( self::$_instance ) ) self::$_instance = new self( $args ); return self::$_instance; } /** * add_page function. * * @access public * @return void */ public function add_page() { // Do checks only in backend if ( is_admin() ) { if ( empty( $GLOBALS['wp_rewrite'] ) ) $GLOBALS['wp_rewrite'] = new WP_Rewrite(); require_once WPIT_EVENTS_PLUGIN_PATH . 'config/data-page-creation.php'; global $wpdb; foreach ( $pages as $page ) { //if page exists $pagina->ID is the id of the page $pagina = get_page_by_path( $page['post_name'] ); //Create the array for insert post $arg = array ( 'ID' => '', 'post_title' => $page['post_title'], 'post_name' => $page['post_name'], 'post_content' => ( ! empty ( $page['post_content'] ) ? $page['post_content'] : $pagina->post_content) , 'post_type' => $page['post_type'], 'post_status' => $page['post_status'], 'post_author' => $page['post_author'], ); //if page doesn't exist I create it if ( empty( $pagina ) ) { $post_id = wp_insert_post( $arg ); } else { //if page exist add the page ID to the array to modify the right page $arg['ID'] .= $pagina->ID; $page_data = get_post( $pagina->ID ); $arg['post_content'] = $page_data->post_content; $post_id = wp_insert_post( $arg ); } //Verify if in data-page-creation.php exist _wp_page_template and if exist value insert it if (isset ($page[ '_wp_page_template' ]) && ! empty($page[ '_wp_page_template' ])) { update_post_meta ( $post_id , '_wp_page_template' ,$page['_wp_page_template'] ); } } //end foreach // Use a static front page if ( isset($front_page) && ! empty($front_page) ) { $frontpage = get_page_by_path( $front_page['page-slug'] ); if ( ! empty($frontpage) ) { update_option( 'page_on_front', $frontpage->ID ); update_option( 'show_on_front', 'page' ); } } //update version of the plugin update_option( WPIT_EVENTS_PAGE_VERSION_NAME , WPIT_EVENTS_PAGE_VERSION_NUMBER ); } //end if only in the admin } /** * update_page_check function. * * @access public * @return void */ public function update_page_check() { // Do checks only in backend if ( is_admin() ) { if ( version_compare( get_site_option( WPIT_EVENTS_PAGE_VERSION_NAME ), WPIT_EVENTS_PAGE_VERSION_NUMBER ) != 0 ) { $this->add_page(); } } //end if only in the admin } }// chiudo la classe //istanzio la classe $wpit_add_page = Wpit_Add_Page::getInstance();
Per prima cosa definiamo 2 costanti, la prima è il nome della option
define( WPIT_EVENTS_PAGE_VERSION_NAME , 'wpit-events-page' );
La seconda è la versione del plugin, importante perché se vengono aggiunte pagine deve essere modificata la versione per aggiungere le nuove pagine.
define( WPIT_EVENTS_PAGE_VERSION_NUMBER, '1.1');
I dati delle pagine da creare sono contenuti in un file esterno che io ho chiamato
data-page-creation.php
ma voi potete chiamarlo come volete.
Una volta creato il file (più avanti metto il tracciato) dovete dire al plugin dove si trova e come si chiama questo file.
Io ho l’abitudine di creare alcune costanti nei plugin e una di queste è quella che definisce il path del plugin
WPIT_EVENTS_PLUGIN_PATH
ed è sempre mia abitudine inserire i file di configurazione in directory ad hoc, in questo caso nella directory
config
Per dire alla classe dove trovare il file di configurazione dovete modificare la riga 83
require_once WPIT_EVENTS_PLUGIN_PATH . 'config/data-page-creation.php';
Quello che segue è invece il tracciato del file di configurazione
<?php //creo l'array delle pagine //**************************************************************************** // Array completo con tutti i dati, shortcode in post_content // Eliminare i campi che non servono // array( // 'post_title' => 'Post title', // 'post_name' => 'Post slug', // 'post_content' => 'content', // 'post_type' => 'page', // 'post_status' => 'publish', // 'post_author' => 1, // '_wp_page_template' => 'template.php' // ), // //***************************************************************************** $pages = array( array( 'post_title' => 'Login Register', 'post_name' => 'login-register', 'post_content' => '', 'post_type' => 'page', 'post_status' => 'publish', 'post_author' => 1, '_wp_page_template' => 'login-register.php' ), array( 'post_title' => 'User Page', 'post_name' => 'user-page', 'post_content' => '', 'post_type' => 'page', 'post_status' => 'publish', 'post_author' => 1, '_wp_page_template' => 'user-page.php' ), );
Come potete vedere ho messo solo i dati che mi interessavano e nello specifico il titolo della pagina, lo slug, il content vuoto (qui potete inserire testo e/o shortcode), il tipo di post (in questo caso una pagina), lo status, l’autore e il template di pagina.
Potete aggiungere tutti i campi che vi interessano, incluso inserire la pagina da utilizzare come front page, se ne avete bisogno.
Se avete qualche dubbio, suggerimento o miglioramento da apportare i commenti sono per voi 🙂
Sii il primo a commentare