[英]Wordpress - ACF: Disable repeater field in user profile for non admin users
在用戶個人資料頁面中,我添加了帶有2個子字段(選擇和日期選擇器)的轉發器ACF
。 如果用戶不是管理員,我希望禁用此字段。 因此,管理員可以編輯此字段,而其他用戶只能查看。 那可能嗎?
我已經嘗試過這里找到的解決方案,但是它僅適用於簡單字段,不適用於ACF
。
如果不可能,如何創建表並從數據庫中獲取字段以在用戶配置文件頁面中呈現它們?
這是我用來禁用普通字段的代碼:
/* 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');
用以下代碼替換for循環
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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.