You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于作者隐藏Elementor中Jet Engine Talent Profile的编辑按钮?

解决Jet Engine自定义文章类型编辑按钮仅作者可见的问题

原代码问题分析

  1. 正则表达式格式错误:preg_replace要求正则表达式必须包含分隔符(比如/),原代码直接写'a.jet-listing-dynamic-link__link'不符合语法,导致替换失效。
  2. 匹配范围不足:仅匹配类名字符串无法完整移除按钮,需要匹配整个<a>标签及其内部内容。
  3. 未处理未登录用户:未登录用户调用wp_get_current_user()会返回空用户对象,可能引发逻辑错误。

修正后的代码

add_action('template_redirect', 'hide_edit_profile_button');

function hide_edit_profile_button() {
    // 仅在Talent Profile单页执行
    if (!is_singular('talent_profile')) {
        return;
    }

    global $post;
    $current_user = wp_get_current_user();

    // 未登录用户直接隐藏按钮
    if (!$current_user->ID) {
        add_filter('the_content', 'remove_edit_profile_button');
        return;
    }

    // 允许的角色:管理员、编辑、作者,或文章作者本人
    $allowed_roles = array('administrator', 'editor', 'author');
    $is_allowed = ($current_user->ID == $post->post_author) || array_intersect($allowed_roles, $current_user->roles);

    // 不允许的用户则移除按钮
    if (!$is_allowed) {
        add_filter('the_content', 'remove_edit_profile_button');
    }
}

function remove_edit_profile_button($content) {
    // 匹配整个Edit Profile按钮的a标签(包含类名和内容)
    $pattern = '/<a[^>]+class="jet-listing-dynamic-link__link"[^>]*>.*?<\/a>/is';
    return preg_replace($pattern, '', $content);
}

关键修改说明

  • 更换钩子为template_redirect:这个钩子时机更合适,确保在模板加载前完成判断,避免不必要的执行。
  • 完善用户登录判断:先检查用户是否登录,避免未登录用户触发错误逻辑。
  • 修正正则表达式:使用/is修饰符,确保匹配时忽略大小写且跨行匹配完整的按钮标签,彻底移除按钮元素。
  • 优化权限逻辑:将允许条件明确拆分,代码更易读,避免逻辑混乱。

内容的提问来源于stack exchange,提问作者arif hossain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:58:20