我希望我的插件在激活时自动创建自定义帖子类型。
这是我的代码
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'
)
)
);
}
}
以及停用时如何删除它?
答案 0 :(得分:1)
下面的完整代码对我有用。
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' );
}