php - 激活插件时创建自定义帖子类型,并在停用后删除 -Wordpress

标签 php wordpress custom-post-type

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

这是我的代码

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

以及如何在停用时将其删除?

最佳答案

完整的代码在下面,对我有用。

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

关于php - 激活插件时创建自定义帖子类型,并在停用后删除 -Wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50810282/

相关文章:

wordpress - 在 WordPress 中,如何在侧边栏小部件中显示自定义帖子类型的单个随机帖子?

php - 从 Woocommerce 3 中的订单中获取选定的变体属性

html - 如何设置单个 WordPress 菜单项的样式?

WordPress 显示除一种之外的所有自定义帖子类型

javascript - Wordpress:在另一个插件文件中使用一个插件文件的功能

php - 如何在 WooCommerce 中使用 add_to_cart() 方法?

wordpress - 在页面上显示最近的自定义帖子类型的内容

php - 来自一张表的自定义输出数据

php - Smarty/如何包含PHP函数来压缩CSS文件

php - 在 PHP 中使用 preg_match 通过多个分隔符拆分字符串