jeudi 26 mars 2015

WordPress Plugin Boilerplate - add_action hook in static "activate" function


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




Aucun commentaire:

Enregistrer un commentaire