WordPress插件样板-静态“激活”函数中的Add_action挂钩

时间:2015-03-25 作者:Aaron Bentley

我希望有人能给我指出正确的方向。

我正在使用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;
    }
}

1 个回复
最合适的回答,由SO网友:bueltge 整理而成

问题是WordPress中的初始化通过register_activation_hook( __FILE__, \'activate_IWCollege_Courses\' ); 该函数是在激活插件时启动的,而不是更多。你必须切换到挂钩init. 对于自定义Post类型,codex中建议使用init hook。在每次WordPress初始化期间,必须注册自定义帖子类型。

但重要的是flush_rewrite_rules();在您的激活方法中,应仅在激活时运行以重写永久链接。对于这个函数,它初始化关于register_activation_hook().

您的问题的伪示例。

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
}

结束

相关推荐

排除Plugins.php HTTP安装路径与单个插件的HTTPS的故障

我正在尝试对插件中显示的安装路径进行故障排除。php作为我在自己的网站上安装的插件,我最初并没有编写该插件。当我的站点的其余部分是HTTPS时,此插件引用和安装的所有资源都会显示为混合内容HTTP。我一直在玩弄下面的代码段,但它似乎没有起到什么作用,也没有解决问题。有人能指出我遗漏了什么吗? /** * registers scripts and stylesheets */ public function register_assets() {&