1

I'm hoping someone can point me in the right direction here.

I'm using the Wordpress Plugin Boilerplate to develop a custom plugin. On install, the plugin will register a new post type (course), a new taxonomy (course-areas) and set up permalink structures for the new post type and taxonomy.

I've added my code (to create the post type and taxonomy) to the Activator class as below, but the code doesn't appear to run, I'd guess it's caused by the add_action hook I'm using (namely the 'init' hook as recommended by the codex for plugin installs) as the code will run as intended if I call it within the main plugin file (iwcollege-courses.php) but I'm really not sure.

It's worth noting I'm not receiving any errors when activating the plugin, it appears to activate successfully, but no custom post types or taxonomies are created.

Any ideas on why this is happening and how I can remedy this?

Thanks in advance,

Aaron Bentley :)

iwcollege-courses.php

// The Activator class is called by the following code within the main plugin file as below:

function activate_IWCollege_Courses() {
    require_once plugin_dir_path( __FILE__ ) . 'includes/class-iwcollege-courses-activator.php';
    IWCollege_Courses_Activator::activate();
}

register_activation_hook( __FILE__, 'activate_IWCollege_Courses' );

class-iwcollege-courses-activator.php

class IWCollege_Courses_Activator {

    public static function activate() {

        add_action( 'init', array( get_called_class(), 'create_course_post_type' ), 0 );
        add_action( 'init', array( get_called_class(), 'create_course_area_taxonomies'), 0 );
        add_filter( 'post_type_link', array( get_called_class(), 'course_permalink_structure'), 10, 4 );

        flush_rewrite_rules();
    }

    public static function create_course_post_type() {

        $labels = array(
            'name'               => 'Courses', 'post type general name',
            'singular_name'      => 'Course', 'post type singular name',
            'add_new'            => 'Add New', 'Course',
            'add_new_item'       => 'Add New Course',
            'edit_item'          => 'Edit Course',
            'new_item'           => 'New Course',
            'all_items'          => 'All Courses',
            'view_item'          => 'View Course',
            'search_items'       => 'Search Courses',
            'not_found'          => 'No Courses found',
            'not_found_in_trash' => 'No Courses found in the Trash',
            'parent_item_colon'  => '',
            'menu_name'          => 'Courses',
            'menu-icon'          => 'dashicons-list-view'
        );

        $supports = array (
            'title',
            'editor',
            'thumbnail',
            'excerpt',
            'revisions'
        );

        $capabilities = array(
            'create_posts'  => false
        );

        $rewrite = array(
            'slug'          => 'course-area/%course_areas_taxonomy%/courses',
            'with_front'    => true,
            'hierarchical'  => false
        );

        $args = array(
            'labels'                => $labels,
            'supports'              => $supports,
            'description'           => 'Holds IWCollege Course data',
            'public'                => true,
            'rewrite'               => $rewrite,
            'menu_position'         => 5,
            'has_archive'           => true,
            'taxonomies'            => array('post_tag'),
            'capabilities'          => $capabilities,
            'map_meta_cap'          => true,
            'public'                => true,
            'query_var'             => true,
            'publicly_queryable'    => true

        );

        register_post_type( 'course', $args );
    }


    public static function create_course_area_taxonomies() {

        $labels = array(
            'name'              => 'Course Areas', 'taxonomy general name',
            'singular_name'     => 'Course Area', 'taxonomy singular name',
            'search_items'      => 'Search Course Areas',
            'all_items'         => 'All Course Areas',
            'parent_item'       => 'Parent Course Area',
            'parent_item_colon' => 'Parent Course Area:',
            'edit_item'         => 'Edit Course Area',
            'update_item'       => 'Update Course Area',
            'add_new_item'      => 'Add New Course Area',
            'new_item_name'     => 'New Course Area Name',
            'menu_name'         => 'Course Areas',
            'popular_items'     => null
        );

        $rewrite = array(
            'slug'          => 'course-area',
            'with_front'    => true,
            'hierarchical'  => true,
            'ep_mask'       => 'ep-mask'
        );

        $capabilities = array(
            'manage_terms'  => true,
            'edit_terms'    => true,
            'delete_terms'  => false,
            'assign_terms'  => false
        );

        $args = array(
            'hierarchical'          => false,
            'labels'                => $labels,
            'show_ui'               => true,
            'show_admin_column'     => true,
            'show_in_nav_menus'     => true,
            'show_tagcloud'         => false,
            'query_var'             => true,
            'sort'                  => false,
            'public'                => false,
            'rewrite'               => $rewrite,
            'capabilities'          => $capabilities,
            'update_count_callback' => '_update_post_term_count'
        );

        register_taxonomy( 'course_areas_taxonomy', array('course'), $args );

        register_taxonomy_for_object_type( 'course_areas_taxonomy', 'course' );

    }


    public static function course_permalink_structure ($post_link, $post, $leavename, $sample) {

        if ( strpos( $post_link, '%course_areas_taxonomy%' ) !== false ) {

            $course_post_type_term = get_the_terms( $post->ID, 'course_areas_taxonomy' );
            $post_link = str_replace( '%course_areas_taxonomy%', array_pop( $course_post_type_term )->slug, $post_link );
        }

        return $post_link;
    }
}
  • Your method activate() was init via the Register_activation_hook. This was fired only in activation oft the Plugin. Add the Source of this Methode in the constructor or method than run on init the class. – bueltge Mar 25 '15 at 20:36
  • Hi @bueltge, many thanks for your reply :) "Your method activate() was init via the Register_activation_hook. This was fired only in activation oft the Plugin." - As setting up the post types and taxonomies only needs to be done on install, I thought this was the most effective way to do this? "Add the Source of this Method in the constructor or method than run on init the class." - I'm not 100% sure what you mean by this, do you mean the source of the IWCollege_Courses_Activator class? – Aaron Bentley Mar 26 '15 at 10:03
  • The my answer. My short comment yesterday was written on the mobile. – bueltge Mar 26 '15 at 10:12
2

The problem is the initialization in WordPress via register_activation_hook( __FILE__, 'activate_IWCollege_Courses' ); The function was fired on the Activation of a plugin, not more. You must switch to the hook init. For Custom Post types, the init hook is recommended in the codex. Custom post types must be registered during every WordPress initialization.

But the important part flush_rewrite_rules();in your activation method should be run only on activation to rewrite the permalinks. For this function is it right, that it init about the register_activation_hook().

A pseudo example for your problem.

register_activation_hook( __FILE__, 'example_activate' );
function example_activate() {

   flush_rewrite_rules();
}

add_action( 'init', 'example_register_cpt' );
function example_register_cpt() {

   // All with the goal of:
   register_post_type();
   // And
   register_taxonomy();
   // much more, that run always on WP
}
  • 1
    Thanks for your help @bueltge :) I have moved the code for post types and taxonomy outside the activator {leaving the flush_rewrite_rules() in the activate() method and is now working as intended, thanks again. – Aaron Bentley Mar 26 '15 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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