是否必须删除插件停用时的自定义帖子或分类?

时间:2016-10-25 08:12:53

标签: wordpress

我正在开发一个插件,为该帖子类型注册自定义帖子类型和自定义分类。现在,一旦任何人停用插件,该怎么办?我应该取消注册我的自定义帖子或分类法或其他任何东西吗?我是插件开发的新手,请让我知道我应该怎么做插件停用?这是我的自定义帖子类型的代码。

function portolfio_post_type() {

    $labels = array(
        'name'                  => __( 'Portfolio Post Type', 'Post Type General Name' ),
        'singular_name'         => __( 'Portfolio Post Type', 'Post Type Singular Name' ),
        'menu_name'             => __( 'Portfolio'),
        'name_admin_bar'        => __( 'Post Type'),
        'archives'              => __( 'Portfolio Archives' ),
        'parent_item_colon'     => __( 'Parent Item:' ),
        'all_items'             => __( 'All Portfolios'),
        'add_new_item'          => __( 'Add New Portfolio'),
        'add_new'               => __( 'Add New'),
        'new_item'              => __( 'New Portfolio' ),
        'edit_item'             => __( 'Edit Portfolio' ),
        'update_item'           => __( 'Update Portfolio' ),
        'view_item'             => __( 'View Portfolio' ),
        'search_items'          => __( 'Search Portfolio' ),
        'not_found'             => __( 'Not found'),
        'not_found_in_trash'    => __( 'Not found in Trash' ),
        'featured_image'        => __( 'Featured Image'),
        'set_featured_image'    => __( 'Set featured image' ),
        'remove_featured_image' => __( 'Remove featured image' ),
        'use_featured_image'    => __( 'Use as featured image'),
        'insert_into_item'      => __( 'Insert into Portfolio'),
        'uploaded_to_this_item' => __( 'Uploaded to this Portfolio'),
        'items_list'            => __( 'Portfolios list' ),
        'items_list_navigation' => __( 'Portfolios list navigation'),
        'filter_items_list'     => __( 'Filter Portfolios list'),
    );

    $args = array(
        'label'                 => __( 'Portfolio' ),
        'description'           => __( 'Portfolio Post type to add portfolio of your work' ),
        'labels'                => $labels,
        'supports'              => array('title', 'editor', 'thumbnail' ),
        'taxonomies'            => array( 'portfolio_category'),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'post',
    );

    register_post_type( 'portfolio', $args );

}
add_action( 'init', 'portolfio_post_type', 0 );

我只是在我的插件文件中粘贴此代码,并且在激活时它会创建自定义帖子类型,但我不知道如何在停用插件时对此帖子类型做什么...?请帮助我的任何人......

此外,插件开发中的任何好技巧都会受到赞赏:)

2 个答案:

答案 0 :(得分:0)

您最终可以将您的cpt帖子转换为草稿帖子,卸载操作并添加管理员通知。

答案 1 :(得分:0)

是的,您应该删除插件卸载时的所有代码。为此,您可以创建uninstall.php文件并将该文件放在主插件目录中。 uninstall.php的代码如下所示: 注意:这是我的插件的uninstall.php文件。

if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) )
exit();

function go_delete_now() {
    global $wpdb;

    $posts = get_posts( array(
        'numberposts' => -1,
        'post_type' => 'films',
        'post_status' => 'any' ) );

    foreach ( $posts as $post ){
        wp_delete_post( $post->ID, true );
    }
}

go_delete_now();

// Set global
global $wpdb;
// Delete terms
$wpdb->query( "
    DELETE FROM
    {$wpdb->terms}
    WHERE term_id IN
    ( SELECT * FROM (
        SELECT {$wpdb->terms}.term_id
        FROM {$wpdb->terms}
        JOIN {$wpdb->term_taxonomy}
        ON {$wpdb->term_taxonomy}.term_id = {$wpdb->terms}.term_id
        WHERE taxonomy = 'films_category'
    ) as T
    );
");
// Delete taxonomies
$wpdb->query( "DELETE FROM {$wpdb->term_taxonomy} WHERE taxonomy = 'films_category'" );

注意:在我的情况下,我的帖子类型名称为films,电影类别为films_category

您只需根据自己的需要更改这些帖子和分类名称,它就可以正常使用。如果出现任何问题,请告诉我。