How to set the field as "read-only" for some specific post status?

Here is my situation:

1.There are two kinds of post status,one is “A”,the other one is “B”.

2.There is a custom textarea field(created in ACF) called “test”(its id is “acf-test”:wink:

3.I want to set this condition:

if the post status is “A”,then “test” field can be edit;else, the “test” field will be set as “read-only”.

4.I found this data:http://www.elliotcondon.com/conditional-logic-for-advanced-custom-fields/

But it had some problem while working.Its ACF version is 3.3.1,but I’m using the ACF of 4.3.5,the ‘acf_head-input’ should be replaced as ‘acf/input/admin_head’.

However,I still can’t write the right code of my siuation.

Please help me.

The code of seting elements “read-only” is :

$('input').attr("readonly","readonly");

And the get_post_status code is:

$post_status = get_post_status();

However,I can’t combine via jquery.

  • qmqy
    • Design Lord, Child of Thor

    In the live support session,S H Mohanjith told me that I can use this:

    <script type="text/javascript">
    var my_post_status = "<?php echo get_post_status(); ?>";
    </script>

    Then I trid to modify the code example(http://www.elliotcondon.com/conditional-logic-for-advanced-custom-fields/)like this:

    function my_conditional_logic()
    {
    ?>
    <script type="text/javascript">
    (function($){

    /*
    * hide_fields
    *
    * @description: a small function to hide all the conditional fields
    * @created: 17/07/12
    */

    function readonly_fields()
    {
    $('#acf-field-hero_video').attr("readonly","readonly");
    }

    /*
    * acf/setup_fields
    * - Similar to $(document).ready, but runs after ACF has instantiated itself
    */

    $(document).live('acf/setup_fields', function(e, postbox){

    // trigger change on the select field to show selected field
    $('#acf-hero_type select').trigger('change');

    });

    /*
    * Hero Type change
    */

    $('#acf-hero_type select').live('change', function(){

    // vars
    var value = $(this).val();

    // readonly all fields
    var my_post_status = "<?php echo get_post_status(); ?>";
    if(my_post_status == "department")
    {
    readonly_fields();
    }

    // show the selected field
    if( value == "image" )
    {
    $('#acf-hero_image').show();
    }
    else if( value == "video" )
    {
    $('#acf-hero_video').show();
    }

    });

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

    add_action('acf/input/admin_head', 'my_conditional_logic');

    Tt works,but it has many redundant code.

    So I simplify it as:

    function my_conditional_logic()
    {
    ?>
    <script type="text/javascript">
    (function($){

    /*
    * hide_fields
    *
    * @description: a small function to hide all the conditional fields
    * @created: 17/07/12
    */

    function readonly_fields()
    {
    $('#acf-field-hero_video').attr("readonly","readonly");
    }
    // readonly all fields
    var my_post_status = "<?php echo get_post_status(); ?>";
    if(my_post_status == "department")
    {
    readonly_fields();
    }

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

    add_action('acf/input/admin_head', 'my_conditional_logic');

    It failed.What’s the problem?

  • qmqy
    • Design Lord, Child of Thor

    I solved this problem.Here is the code:

    function my_conditional_logic()
    {
    ?>
    <script type="text/javascript">
    (function($){
    function readonly_fields()
    {
    $('#acf-field-textt').attr("readonly","readonly");
    }

    $('#acf-is_tw').live('change', function(){

    // readonly the field
    var my_post_status = "<?php echo get_post_status(); ?>";
    if(my_post_status == "department")
    {
    readonly_fields();
    }

    });

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

    add_action('acf/input/admin_head', 'my_conditional_logic');

  • Hoang Ngo
    • Code Slayer

    Hi @qmqy

    It’s very amazing that you has resolve it, you rock man :smiley:.

    I have another version here

    function my_conditional_logic() {
    ?>
    <script type="text/javascript">
    (function ($) {
    $(document).live('acf/setup_fields', function (e, postbox) {
    function readonly_fields() {
    $('#acf-field-hero_video').attr("readonly", "readonly");
    }

    // readonly all fields
    var my_post_status = "<?php echo get_post_status(); ?>";
    if (my_post_status == "department") {
    readonly_fields();
    }
    });
    })(jQuery);
    </script>
    <?php
    }

    The idea is after the acf has settled all of fields, we call the function to check the post condition and apply the read only to.

    Best regards,

    Hoang

  • qmqy
    • Design Lord, Child of Thor

    Wow,amazing code.Cool!

    Your version is better than mine!

    Another question,this code only solve the problem that only when I changed the field’s value,the “read-only” effect works.

    However,how can I modify the code that can make “read-only” effect works automatically when the value of “acf-hero_type” is “video”?

    I search some data,however,I know less about jQuery,please help me:slight_smile:

    @hoang Ngo

  • Hoang Ngo
    • Code Slayer

    Hi @qmqy

    Sure :smiley:.

    function my_conditional_logic() {
    ?>
    <script type="text/javascript">
    (function ($) {
    //wait for all fields settled
    $(document).live('acf/setup_fields', function (e, postbox) {
    function readonly_fields() {
    $('#acf-field-hero_video').attr("readonly", "readonly");
    }
    function editable_fields(){
    $('#acf-field-hero_video').removeAttr("readonly");
    }

    // readonly all fields
    var my_post_status = "<?php echo get_post_status(); ?>";
    $('#acf-is_tw').live('change', function(){
    // readonly the field
    if(my_post_status == "department")
    {
    //now we check does current value is video
    if($(this).val()=='video'){
    //If this video, we will readonly
    readonly_fields();
    }else{
    //the value select not video anymore, reopen that
    editable_fields();
    }
    }

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

    If you have any issues please don’t hesitate to let us know so we can assist

    Best Regards

    Hoang

  • qmqy
    • Design Lord, Child of Thor

    Opps,it’s so regret that I’m busy today:slight_frown:

    Can you tell me the time when you are free tomorrow?

    I’m a chinese,so the time is so confused for me…..

    So,could you tell me your free time with two version?One is in your time zone,the other is in my time zone.

    By the way,my free time is 20:00~24:00( 3rd APRIL,your time zone )?7:00~10:40(4th APRIL,your time zone)

    And could you tell me that whether you go to work on weekend?

    Thank you very much!

    @hoang Ngo

  • qmqy
    • Design Lord, Child of Thor

    Opps,it’s so regret that I’m busy today:slight_frown:

    Can you tell me the time when you are free tomorrow?

    I’m a chinese,so the time is so confused for me…..

    So,could you tell me your free time with two version?One is in your time zone,the other is in my time zone.

    By the way,my free time is 20:00~24:00( 3rd APRIL,your time zone )?7:00~10:40(4th APRIL,your time zone)

    And could you tell me that whether you go to work on weekend?

    Thank you very much!

    @hoang Ngo

  • Hoang Ngo
    • Code Slayer

    Hi @qmqy,

    I hope you are doing well today.

    I’m sorry but it seem we are missing again :slight_frown:.

    I will available here from till now to 24h on your time, please feel free to contact me here so i can assist you, and i love to do that :slight_smile:.

    Best Regards

    Hoang

  • Hoang Ngo
    • Code Slayer

    Hi @qmqy,

    Please send me the access info here, so we can work on this issue more effectivite.

    Can you please send in:

    – Mark to my attention – ATTN: Hoang Ngo

    – Link back to this thread

    – Include admin/network access

    – Include cPanel (I will need to look at the DB so need PHPMyAdmin or similar)

    – Include FTP

    – Include any relevant URLS for your site

    On the contact form, select “I have a different question”, this ensures it comes through and gets assigned to me.

    Best Regards

    Hoang

  • Hoang Ngo
    • Code Slayer

    Hi @qmqy,

    Thanks for your info, yes i have get that.

    But how about the worpdress access, it does not include on the email.

    Can you please update me in:

    – Mark to my attention – ATTN: Hoang Ngo

    – Link back to this thread

    – Include wordpress access

    – Include any relevant URLS for your site

    On the contact form, select “I have a different question”, this ensures it comes through and gets assigned to me.

    Best Regards

    Hoang

  • Hoang Ngo
    • Code Slayer

    Hi @qmqy,

    Thanks for your info and i’m very sorry about the delay.

    I has updated your snippet, the reason why it is not work is:

    1.You need to add the

    add_action('acf/input/admin_head', 'my_conditional_logic');

    In the below of the snippet, this will tell wordpress that i need to attach my code when you run your action.

    2.I has updated the element id and option value, now everything is ok :smiley:.

    If you have any issues please don’t hesitate to let us know so we can assist.

    Best Regards

    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.