自定义投递类型/分类的删除垃圾桶/删除选项

时间:2018-02-26 作者:Hassan Alvi

我已经创建了自定义帖子类型prj, 我需要禁用任何用户角色的“移动到垃圾箱”选项!分类法也是如此org 与相同的职位类型关联。

这是我到目前为止想出的代码。。但它只会在管理菜单中隐藏该选项,而不会从用户角色中禁用该功能!此代码从主帖子列表页面隐藏该选项,并阻止删除操作。。问题是“移到垃圾箱”选项仍然可用

//Disabling Trash Option for Project
add_filter( \'post_row_actions\', \'remove_row_actions_post\', 10, 1 );
function remove_row_actions_post( $actions ) {
    if( get_post_type() === \'prj\' ) {
        unset( $actions[\'clone\'] );
        unset( $actions[\'trash\'] );
        return $actions;
    }
}

add_action(\'wp_trash_post\', \'restrict_post_deletion\', 10, 1);
add_action(\'before_delete_post\', \'restrict_post_deletion\', 10, 1);
function restrict_post_deletion($post_id) {
    if( get_post_type($post_id) === \'prj\' ) {
      wp_die(\'The post you were trying to delete is protected.\');
    }
}

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

试试这些:

/**
 * Removes the "Trash" link on the individual post\'s "actions" row on the posts
 * edit page.
 */
add_filter( \'post_row_actions\', \'remove_row_actions_post\', 10, 2 );
function remove_row_actions_post( $actions, $post ) {
    if( $post->post_type === \'prj\' ) {
        unset( $actions[\'clone\'] );
        unset( $actions[\'trash\'] );
    }
    return $actions;
}

add_action(\'wp_trash_post\', \'restrict_post_deletion\');
function restrict_post_deletion($post_id) {
    if( get_post_type($post_id) === \'prj\' ) {
      wp_die(\'The post you were trying to delete is protected.\');
    }
}

/**
 * Removes the "Delete" link on the individual term\'s "actions" row on the terms
 * edit page.
 */
add_filter( \'tag_row_actions\', \'remove_row_actions_term\', 10, 2 );
function remove_row_actions_term( $actions, $term ) {
    if ( \'org\' === $term->taxonomy ) {
        unset( $actions[\'delete\'] );
    }
    return $actions;
}

add_action( \'pre_delete_term\', \'restrict_taxonomy_deletion\', 10, 2 );
function restrict_taxonomy_deletion( $term, $taxonomy ) {
    if ( \'org\' === $taxonomy ) {
        wp_die( \'The taxonomy you were trying to delete is protected.\' );
    }
}

add_action( \'admin_head\', function () {
    $current_screen = get_current_screen();

    // Hides the "Move to Trash" link on the post edit page.
    if ( \'post\' === $current_screen->base &&
    \'prj\' === $current_screen->post_type ) :
    ?>
        <style>#delete-action { display: none; }</style>
    <?php
    endif;

    // Hides the "Delete" link on the term edit page.
    if ( \'term\' === $current_screen->base &&
    \'org\' === $current_screen->taxonomy ) :
    ?>
        <style>#delete-link { display: none; }</style>
    <?php
    endif;
} );
您可能还需要这些:

/**
 * If you want/need to programmatically trash a \'prj\' post, use this function
 * instead of directly calling the wp_trash_post() function.
 */
function my_trash_prj_post( $post_id = 0 ) {
    // Removes the filter THE SAME WAY it was added.
    remove_action(\'wp_trash_post\', \'restrict_post_deletion\');

    // Now trash the post.
    $post = wp_trash_post( $post_id );

    // Re-add the filter.
    add_action(\'wp_trash_post\', \'restrict_post_deletion\');

    return $post;
}

/**
 * If you want/need to programmatically trash a \'org\' term, use this function
 * instead of directly calling the wp_delete_term() function.
 */
function my_delete_org_term( $term, $taxonomy = \'org\', $args = array() ) {
    // Removes the filter THE SAME WAY it was added.
    remove_action( \'pre_delete_term\', \'restrict_taxonomy_deletion\', 10, 2 );

    // Now delete the term.
    $status = wp_delete_term( $term, $taxonomy, $args );

    // Re-add the filter.
    add_action( \'pre_delete_term\', \'restrict_taxonomy_deletion\', 10, 2 );

    return $status;
}

结束

相关推荐

带有自定义POST类型的WP_TRASH_POST操作挂钩

当“联系人”cpt帖子被丢弃时,我需要删除另一个cpt“act”中的元键,我一直在搜索应该使用哪个钩子,但不幸的是codex action reference 页面不是很有帮助。下面是我的代码。理想情况下,我还希望在发布列表屏幕中放置一个管理员通知,用户将被重定向到该屏幕。问题:这是最好的钩子吗</为什么它不能按预期工作以下是我迄今为止的代码。add_action( \'wp_trash_contact\', \'contact_cpt_cleanup\' ); function