Create pages
<?php
/**
* Create new post
*
* @package Agency
* @subpackage Agency/includes
* @copyright Copyright (c) 2014, Fulcrum Creatives
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 0.0.1
* @author Fulcrum Creatives <info@fulcrumcreatives.com>
*/
class Agency_Create_Post {
/**
* The arguments array
*
* @var array
*/
public $custom_args;
/**
* The title of the post
*
* @var string
*/
public $title;
/**
* Initialize the class
*
* @since 0.0.1
*/
public function __construct( $title, $custom_args = array() ) {
$this->custom_args = $custom_args;
$this->title = $title;
add_action( 'init', array( $this, 'create_page' ) );
}
/**
* Get the first avalable admin's ID
* @return [type] [description]
*/
private function get_admins() {
$args = array( 'role' => 'administrator' );
// array of active admins
$admins = get_users( $args );
// return the ID of the first admin
return $admins{0}->ID;
}
public function create_page() {
// The defaults
$defaults = array(
'post_type' => 'post',
'post_title' => $this->title,
'post_name' => str_replace( ' ', '-', $this->title ),
'post_status' => 'publish',
'post_author' => $this->get_admins(),
'post_content' => '',
'post_excerpt' => '',
'post_parent' => 0,
'post_password' => '',
'post_category' => '',
'tags_input' => '',
'tax_input' => '',
'menu_order' => 0,
'ping_status' => 'closed',
'to_ping' => '',
'pinged' => '',
'comment_status' => 'default_comment_status',
'import_id' => 0,
'context' => '',
'post_date' => date( 'Y-m-d H:i:s' ),
'post_date_gmt' => date( 'Y-m-d H:i:s' ),
'page_template' => ''
);
$custom_args = $this->custom_args;
$args = array_merge( $defaults, $custom_args );
if( null == get_page_by_title( $args['post_title'], 'OBJECT', $args['post_type'] ) ) {
$post_id = wp_insert_post( $args );
} else {
// Arbitrarily use -2 to indicate that the page with the title already exists
$post_id = -2;
}
}
}