WordPress Plugin Boilerplate - add_action hook in static “activate” function

Aaron Bentley just a moment. 1 answers, 0 views
plugins hooks actions init

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 :)


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

register_activation_hook( __FILE__, 'activate_IWCollege_Courses' );


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 );


    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 (

        $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;

1 Answers

bueltge 04/08/2017.

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() {


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

   // All with the goal of:
   // And
   // much more, that run always on WP

HighResolutionMusic.com - Download Hi-Res Songs

1 Martin Garrix

Yottabyte flac

Martin Garrix. 2018. Writer: Martin Garrix.
2 Alan Walker

Diamond Heart flac

Alan Walker. 2018. Writer: Alan Walker;Sophia Somajo;Mood Melodies;James Njie;Thomas Troelsen;Kristoffer Haugan;Edvard Normann;Anders Froen;Gunnar Greve;Yann Bargain;Victor Verpillat;Fredrik Borch Olsen.
3 Sia

I'm Still Here flac

Sia. 2018. Writer: Sia.
4 Blinders

Breach (Walk Alone) flac

Blinders. 2018. Writer: Dewain Whitmore;Ilsey Juber;Blinders;Martin Garrix.
5 Dyro

Latency flac

Dyro. 2018. Writer: Martin Garrix;Dyro.
6 Cardi B

Taki Taki flac

Cardi B. 2018. Writer: Bava;Juan Vasquez;Vicente Saavedra;Jordan Thorpe;DJ Snake;Ozuna;Cardi B;Selena Gomez.
7 Bradley Cooper

Shallow flac

Bradley Cooper. 2018. Writer: Andrew Wyatt;Anthony Rossomando;Mark Ronson;Lady Gaga.
8 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
9 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
10 Kelsea Ballerini

This Feeling flac

Kelsea Ballerini. 2018. Writer: Andrew Taggart;Alex Pall;Emily Warren.
11 Mako

Rise flac

Mako. 2018. Writer: Riot Music Team;Mako;Justin Tranter.
12 Dewain Whitmore

Burn Out flac

Dewain Whitmore. 2018. Writer: Dewain Whitmore;Ilsey Juber;Emilio Behr;Martijn Garritsen.
13 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
14 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
15 Charli XCX

1999 flac

Charli XCX. 2018. Writer: Charli XCX;Troye Sivan;Leland;Oscar Holter;Noonie Bao.
16 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora.
17 Diplo

Electricity flac

Diplo. 2018. Writer: Diplo;Mark Ronson;Picard Brothers;Wynter Gordon;Romy Madley Croft;Florence Welch.
18 Jonas Blue

Polaroid flac

Jonas Blue. 2018. Writer: Jonas Blue;Liam Payne;Lennon Stella.
19 Lady Gaga

Look What I Found flac

Lady Gaga. 2018. Writer: DJ White Shadow;Nick Monson;Mark Nilan Jr;Lady Gaga.
20 Avril Lavigne

Head Above Water flac

Avril Lavigne. 2018. Writer: Stephan Moccio;Travis Clark;Avril Lavigne.


Popular Tags