dongqiao8421 2018-12-06 11:12
浏览 42

Wordpress - ACF:禁用非管理员用户的用户配置文件中的转发器字段

In the user profile page I have a added a repeater ACF with 2 subfields (select and datepicker). I want this field to be disabled if the user is not administrator. So administrators can edit this field and the other users can only view it. Is that possible?

I have tried the solution found here but it work only for simple fields and not ACF.

If it's not possible, how I can create a table and fetch the fields from the database to render them in user profile page?

Here is the code I used to disable normal fields:

/* Disables selected fields in (profile.php) */
function atroul_profile_fields_disable_js() {
?>
  <script>
    jQuery(document).ready( function($) {
      var fields_to_disable = ['first_name', 'last_name', 'display_name'];
      for(i=0; i<fields_to_disable.length; i++) {
        if ( $('#'+ fields_to_disable[i]).length ) {
          $('#'+ fields_to_disable[i]).attr("disabled", "disabled");
        }
      }   
    });
  </script>
<?php
}

function atroul_profile_fields_disable() {
  global $pagenow;
  if ($pagenow!=='profile.php') 
  {
    return;
  }
  if (current_user_can('administrator')) 
  {
    return;
  }
  add_action( 'admin_footer', 'atroul_profile_fields_disable_js' );
  //add_action('admin_head','atroul_remove_personal_options');
}
add_action('admin_init', 'atroul_profile_fields_disable');
  • 写回答

1条回答 默认 最新

  • dongxingchang9345 2018-12-06 12:40
    关注

    Replace the for loop with the below code

    for(i=0; i<fields_to_disable.length; i++) {
            if ( $('tr[data-name="'+fields_to_disable[i]+'"]').length ) {
              $('tr[data-name="'+fields_to_disable[i]+'"] input').attr("disabled", "disabled");
              $('tr[data-name="'+fields_to_disable[i]+'"] select').attr("disabled", "disabled");
            }
          }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端