【问题标题】:Wordpress show Edit and Delete buttons only if post's authorWordpress 仅在帖子作者时显示编辑和删除按钮
【发布时间】:2021-07-21 01:47:29
【问题描述】:

我的 wordpress 网站使用网站成员提交的帖子(实际上是 CPT)。显示 CPT 帖子时,我希望在作者查看帖子时出现编辑和删除按钮,如果任何其他成员查看帖子,则不会出现。 我研究过代码和插件,但似乎都只适用于作者角色,而不是特定作者本身。 有没有办法使用 PHP 函数/简码、Javascript 或两者的某种组合来添加此功能?

【问题讨论】:

  • iirc,members插件有那种微调每个角色权限的能力,试试看,它也可以修改cpts

标签: javascript php wordpress author


【解决方案1】:

这是禁用编辑和删除按钮的代码。我已经测试了代码,它在我这边运行良好。

仅限编辑按钮链接

function prefix_remove_get_edit_post_link( $link ) {
    global $post;
    $current_user =  get_current_user_id();
    $author_id = get_post_field ('post_author', $post_id);

    if($author_id == $current_user) {
        return $link;
    }
    return null;
}
add_filter('get_edit_post_link', 'prefix_remove_get_edit_post_link');

仅限删除按钮链接

function prefix_remove_get_delete_post_link( $link ) {
    global $post;
    $current_user =  get_current_user_id();
    $author_id = get_post_field ('post_author', $post_id);

    if($author_id == $current_user) {
        return $link;
    }
    return null;
}

add_filter('get_delete_post_link', 'prefix_remove_get_delete_post_link');

对于隐藏按钮的编辑和删除

add_filter( 'post_row_actions', 'remove_row_actions', 10, 1 );
function remove_row_actions( $actions )
{
    global $post;
    $current_user =  get_current_user_id();
    $author_id = get_post_field ('post_author', $post_id);

    if($author_id != $current_user) {
        if( get_post_type() === 'post' ){
            unset( $actions['edit'] );
            unset( $actions['trash'] );
            unset( $actions['inline hide-if-no-js'] );
        }
    }
    return $actions;
}

【讨论】:

  • 虽然我非常感谢提供按下按钮后会发生什么的代码(并计划合并它),但我仍然缺少关于初始按钮本身的答案。如果当前用户不是原作者,如何隐藏它们?
  • 所以你需要删除这些按钮对吗? @BrianWent
  • 我已经更新了我的答案,如果不工作请检查并告诉我@BrianWent
  • 感谢所有代码!我有一些工作要让这些行动发挥作用,但你的基础很好。
猜你喜欢
  • 2021-11-02
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 2023-03-09
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
相关资源
最近更新 更多