javascript - WordPress 仅在帖子作者时才显示编辑和删除按钮

标签 javascript php wordpress author

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

最佳答案

这是禁用编辑和删除按钮的代码。我已经测试了代码,在我这边工作得很好。

仅适用于编辑按钮链接

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

关于javascript - WordPress 仅在帖子作者时才显示编辑和删除按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68463099/

相关文章:

html - 如何修复 Mobile-View 的表格?

javascript - JSHint 和 JSCS 内联忽略相同的下一行

javascript - 字符串和任何数字的正则表达式

javascript - 变得更加 OO 多个 For 循环 JS

PHP登录问题

html - Wordpress CSS主题修改

javascript - wiris 插件在 tinymce 中运行方程保存

php - 带有符号链接(symbolic link)和索引文件的 .htaccess 配置未按预期工作

php - 如何遍历数据库行数组,并选择第一个具有所需值的行?

css - 博客的下一个/上一个导航图像