简体   繁体   中英

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

I want to edit/customize the admin edit-post page.

Description

  • I've made a Custom Post Type

  • Made two groups of Advance Custom Fields groups, group_1 and group_2 (each include several custom fields)

  • Made two custom user role types user_type_1 and user_type_2

The goal

  • I want user_type_1 to be able to write group_1 and only read group_2 .
  • I want user_type_2 to be able to write group_2 and only read group_1 .

The Question

  • How can I edit the admin edit-post page? (What actions to call to manipulate them?)

  • What actions to delete default body in element id="post-body-content"

  • Custom render the ACF body in element id="postbox-container-2"

I'll be building a jQuery script that will set the input fields to read-only with $('input').attr('readonly', true); (simplified) Based on the current users role.

I was afraid, when I won't make this a field, it won't save the grayed out fields back to the DB.

This is the complete code I anyone would need this in the future:

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

I'm only going to add a check if current page is post-edit before executing the complete script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM