簡體   English   中英

Wordpress - CPT、ACF - 如何自定義管理員編輯帖子頁面?

[英]Wordpress - CPT, ACF - How to customize admin edit-post page?

我想編輯/自定義管理員編輯帖子頁面。

描述

  • 我做了一個自定義帖子類型

  • 制作了兩組高級自定義字段組, group_1group_2 (每個包含多個自定義字段)

  • 制作了兩個自定義用戶角色類型user_type_1user_type_2

目標

  • 我希望user_type_1能夠寫入group_1並且只能讀取group_2
  • 我希望user_type_2能夠寫入group_2並且只能讀取group_1

問題

  • 如何編輯管理員編輯帖子頁面? (調用什么操作來操縱它們?)

  • 刪除元素id="post-body-content"默認正文的操作

  • 在元素id="postbox-container-2"自定義呈現 ACF 正文

我將構建一個 jQuery 腳本,該腳本將使用$('input').attr('readonly', true);將輸入字段設置為$('input').attr('readonly', true); (簡化)基於當前用戶角色。

我很害怕,當我不把它作為一個字段時,它不會將灰色字段保存回數據庫。

這是我將來任何人都需要的完整代碼:

add_action('admin_head', 'rtt_conditional_acf_post_input');

function rtt_conditional_acf_post_input()
{

    $role = rtt_get_current_user_roles();

    $target = null;
    if (in_array("user_type_1", $role)) {
        $target = '#acf-group_5e1d936b7e583'; // <--- element id of group_1

    } else if (in_array("user_type_2", $role)) {
        $target = '#acf-group_5e1d93bdf2a56'; // <--- element id of group_2
    }


    if($target) {
        ?>

        <script type="application/javascript">

            $ = jQuery;
            $(document).ready(function () {

                let target = '<?php echo $target; ?>';

                $(target).find("input").each(function () {
                    $(this).attr('readonly', true);
                });

            });
        </script>
        <?php
    }
}

function rtt_get_current_user_roles()
{
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        $roles = ( array )$user->roles;
        return $roles; // This returns an array
        // Use this to return a single value
        // return $roles[0];
    } else {
        return array();
    }
}

在執行完整腳本之前,我只會添加一個檢查當前頁面是否是后期編輯的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM