1

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',
)
) ); 
}
?>
1
  • Please, post the code which register the custom post type and the custom taxonomy. It is need to see if the problem can be there.
    – cybmeta
    Commented Dec 21, 2014 at 13:29

1 Answer 1

0

I see a problem in the register_taxonomy function. The second argument that you are using is:

array ( 0 => 'frontiles' )

But it should be:

array ( 'frontiles' )

Or, as you are passing only one CPT, it could be also a string:

'frontiles'

Also, you are executing insert_term() in every page load but in your code I see that always is the same term inserted. Once the term was created the first time, there is no reason to repeat that process in every page load. I suggest to remove add_action('init','insert_term'); and execute insert_term() on the registration action hook right before wp_insert_post.

Also, because you want to insert a term and create posts during activation, you have to register the custom post type and taxonomy also in the register_activation_hook, not only at init. That is beacuse the next init event ocurrs when the plugin is still inactive. I've build this plugin with your code and my suggestions and it works:

<?php  
/* 
Plugin Name: Test
Plugin URI: http://wordpress.stackexchange.com/questions/172888/plugin-development-hooks-generate-content
Version: 1.0
Author: CybMeta
Description: Test
*/ 

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

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

function create_frontles_posts() {

    cptui_register_my_cpt_frontiles();
    cptui_register_my_taxes_tiles_categories();
    insert_term();

    $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' );
7
  • No luck..., the same results
    – Dedalos01
    Commented Dec 22, 2014 at 12:47
  • Now that I have the full code in the question I can see whre is the problem. See edited answer.
    – cybmeta
    Commented Dec 22, 2014 at 13:08
  • It works like a charm, you are the man :-D. So you included the functions of cpt and tax again inside the last function..., then they need to be called twice: on "init" and in "register_activation_hook". Why is this?
    – Dedalos01
    Commented Dec 22, 2014 at 13:25
  • I explained in the answer. When you click "Activate" the plugin, the next request execute the init event before the plugin is activated. I see this a normal behaviour. You send a activating request, WordPress is initialized (init event) and execute the request (register_activation_hook), so this init event is executed when the plugin is still inactive, the init actions in the plugin don't run because it is still inactive. In the next request, when the plugin is already active, the init actions will run but not register_activation_hook which run only on activation.
    – cybmeta
    Commented Dec 22, 2014 at 13:34
  • ok, I dont know if I understad very well, So register_activation_hook will always run first than init. So the problem was that I was trying to add a term when the taxonomy was not created?
    – Dedalos01
    Commented Dec 22, 2014 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.