我希望有人能给我指出正确的方向。
我正在使用Wordpress插件样板来开发一个自定义插件。安装时,插件将注册新的帖子类型(课程)、新的分类法(课程区域),并为新的帖子类型和分类法设置永久链接结构。
我已将代码(用于创建帖子类型和分类)添加到Activator类中,如下所示,但代码似乎没有运行,我希望guess 这是由我使用的add\\u操作挂钩(即codex为插件安装推荐的“init”挂钩)引起的,因为如果我在主插件文件(iwcollege-courses.php)中调用它,代码将按预期运行,但我真的不确定。
值得注意的是,我在激活插件时没有收到任何错误,它似乎激活成功,but no custom post types or taxonomies are created.
关于为什么会发生这种情况,以及我如何补救这种情况,有什么想法吗?
提前感谢,
亚伦·本特利:)
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;
}
}