I'm building my first plugin. This plugin will activate a theme, set up a default front page, register tax. / cpt, and create some content. (cpt = custom post types)
I made it work all this in functions.php
file, using: if (isset($_GET['activated']) && is_admin())
by just activating the theme. But I need this to be a plugin, so the plugin will activate the theme and do the rest.
I achieved to do almost everything work, but I having some prroblems in the creating content process. The cpt that are automatically generated has to be assigned to one term by wp_set_object_terms(
but this isn't working. I have seen that the term is successfully created but maybe the taxonomy is not available when wp_set_object_terms(
is executed, and so can't be assigned to the posts. I need your help..., I don't know how to solve this and I don't find alternative ways to do it.
This is the script:
<?php
/*
Plugin Name: -- CGS Theme Functionality --
Plugin URI: http://www.cgs.com
Version: 1.0
Author: Roy Guadalupe
Description: This plugin implent all functionlaities for CGS Theme
*/
////////////// Create Page
function cgs_create_page() {
$new_page_title = 'Home - Front Tiles';
$new_page_content = 'Please click <a>here</a> to get more info about how to config this theme';
$new_page_template = 'page-frontiles.php';
$page_check = get_page_by_title($new_page_title);
$new_page = array(
'post_type' => 'page',
'post_title' => $new_page_title,
'post_content' => $new_page_content,
'post_status' => 'publish',
'post_author' => 1,
);
if(!isset($page_check->ID)){
$new_page_id = wp_insert_post($new_page);
if(!empty($new_page_template)){
update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
}
}
}
add_action('init','cgs_create_page');
////////////// Set default home page
function set_static_front_page(){
// set the static front page
$home = get_page_by_title('Home - Front Tiles');
update_option('page_on_front',$home->ID);
update_option('show_on_front','page');
// set the blog page
// $blog = get_page_by_title('Blog');
// update_option('page_for_posts',$blog->ID);
}
add_action('init','set_static_front_page');
////////////// Register Custom Post Types and Taxonomies
require_once('inc/cpt-taxonomies.php');
////////////// Add term "mosaic-home" to custom taxonomy "tiles_categories"
function insert_term() {
wp_insert_term(
'Mosaic - Home',
'tiles_categories',
array(
'description' => 'Add Tiles here to load in first term',
'slug' => 'mosaic-home'
)
);
}
add_action('init','insert_term');
////////////// Switch Theme
function updateTheme($theme){
update_option('template', $theme);
update_option('stylesheet', $theme);
update_option('current_theme', $theme);
}
////////////// Make loop for creating 24 posts
function create_frontles_posts() {
$x = 1;
do {
$post_id = wp_insert_post(array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_author' => 1,
'post_name' => 'tile'.$x,
'post_title' => 'Tile',
'post_status' => 'publish',
'post_type' => 'frontiles',
));
$term_taxonomy_ids = wp_set_object_terms( $post_id, array('mosaic-home'), 'tiles_categories' );
$x++;
} while ($x <= 24);
}
register_activation_hook( __FILE__, 'create_frontles_posts' );
function activation_callback() {
//The code inside this function is executed only on plugin activation
// Create Posts
$post_id = -1;
$title='';
// If the page doesn't already exist, then create it
if( null == get_page_by_title( $title ) ) {
create_frontles_posts();
} else {
// Otherwise, we'll stop
$post_id = -2;
}
updateTheme('Creative_Grid');
// programmatically_create_post();
}
register_activation_hook( __FILE__, 'activation_callback' );
?>
This the code included as file for generating custom taxonomies and cpt (it works without problems):
<?php
// Custom Post Type Front tiles
add_action('init', 'cptui_register_my_cpt_frontiles');
function cptui_register_my_cpt_frontiles() {
register_post_type('frontiles', array(
'label' => 'Tiles',
'description' => 'Create tiled elements for the front page.',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'tiles', 'with_front' => true),
'query_var' => true,
'exclude_from_search' => true,
'menu_position' => 3,
'supports' => array('title','editor','excerpt','thumbnail'),
'taxonomies' => array('tiles_categories'),
'labels' => array (
'name' => 'Tiles',
'singular_name' => 'Tile',
'menu_name' => 'Tiles Posts',
'add_new' => 'Add Tile',
'add_new_item' => 'Add New Tile',
'edit' => 'Edit',
'edit_item' => 'Edit Tile',
'new_item' => 'New Tile',
'view' => 'View Tile',
'view_item' => 'View Tile',
'search_items' => 'Search Tiles',
'not_found' => 'No Front Tiles Found',
'not_found_in_trash' => 'No Front Tiles Found in Trash',
'parent' => 'Parent Tile',
)
) ); }
// Taxonomies Front tiles
add_action('init', 'cptui_register_my_taxes_tiles_categories');
function cptui_register_my_taxes_tiles_categories() {
register_taxonomy( 'tiles_categories',array (
0 => 'frontiles',
),
array( 'hierarchical' => true,
'label' => 'Tile Categories',
'show_ui' => true,
'query_var' => true,
'show_admin_column' => true,
'labels' => array (
'search_items' => 'Search Tile Categories',
'popular_items' => '',
'all_items' => 'All Categories',
'parent_item' => 'Parent Tile Category',
'parent_item_colon' => 'Parent Tile Category',
'edit_item' => 'Edit Tile Category',
'update_item' => 'Update Tile Category',
'add_new_item' => 'Add Tile Category',
'new_item_name' => 'New Tile Category',
'separate_items_with_commas' => 'Separate tile categories with commas',
'add_or_remove_items' => 'Add Categories...',
'choose_from_most_used' => 'Choose from the most used Tiles Categories',
)
) );
}
?>