首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在插件被激活时创建自定义post类型,并删除一次停用的-Wordpress

在插件被激活时创建自定义post类型,并删除一次停用的-Wordpress
EN

Stack Overflow用户
提问于 2018-06-12 06:11:10
回答 1查看 2.8K关注 0票数 3

我希望我的插件在激活时自动创建自定义post类型。

这是我的密码

代码语言:javascript
复制
register_activation_hook(__FILE__, 'activate_myplugin');

function activate_myplugin() {

add_action('init', 'create_custom_type_post');

function create_custom_type_post() {

register_post_type('customposttype', array(
    'label' => 'my custom type post',
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'rewrite' => array(
        'slug' => 'mycustomposttype',
        'with_front' => false
        ),
    'query_var' => true,
    'supports' => array(
        'title',
        'editor',
        'excerpt',
        'trackbacks',
        'custom-fields',
        'revisions',
        'thumbnail',
        'author',
        'page-attributes'
        )
    )
);
}

}

以及如何在停用时移除??

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-12 06:30:12

完整的代码在下面,正在为我工作。

代码语言:javascript
复制
       add_action( 'init', 'activate_myplugin' );
        function activate_myplugin() {


                $args=array(
            'label' => 'my custom type post',
            'public' => true,
            'show_ui' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'rewrite' => array(
                'slug' => 'mycustomposttype',
                'with_front' => false
                ),
            'query_var' => true,
            'supports' => array(
                'title',
                'editor',
                'excerpt',
                'trackbacks',
                'custom-fields',
                'revisions',
                'thumbnail',
                'author',
                'page-attributes'
                )
            ); 
                register_post_type( 'customposttype', $args );

        }



        function myplugin_flush_rewrites() {
                activate_myplugin();
                flush_rewrite_rules();
        }

        register_activation_hook( __FILE__, 'myplugin_flush_rewrites' );

        register_uninstall_hook( __FILE__, 'my_plugin_uninstall' );
        function my_plugin_uninstall() {
          // Uninstallation stuff here
             unregister_post_type( 'customposttype' );
        }
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50810282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档