Support

Account

Home Forums Add-ons Repeater Field Setting Repeater Defaults Per Page Template Reply To: Setting Repeater Defaults Per Page Template

  • Hi Rob,

    To run the above code for only specific post types, you can add this code to the top:

    
    // store the current post type in a variable
    $current_post_type = get_post_type( $post_id );
    
    // you can use an array to add 1 or more post types that you
    // want this function to apply to, like so:
    $allowed_post_types = array( 'post-type-1', 'post-type-2', 'post-type-3' );
    
    // then, say... if 'not' in array, return $value untouched
    if ( !in_array( $current_post_type, $allowed_post_types ) ) {
      return $value;
    }
    

    For the last part of your question…
    … hmmm ….

    You could “disable” certain fields based on certain criteria (like post status, or if a custom field is empty)… then… re-enable them once the criteria is met.

    It’s simple enough to disable regular ACF fields (text, number, etc.) and in fact… sometimes you can use the “rules” for field groups to hide them based on post status.

    Here’s a resource about the acf/prepare_field filter that explains how you might disable ACF fields.

    To disable multiple fields, you can call the filter function multiple times:

    
    add_filter('acf/prepare_field/key=field_1', 'my_acf_prepare_field');
    add_filter('acf/prepare_field/key=field_2', 'my_acf_prepare_field');
    

    Be sure that you run your conditions within the function. Check the value of a different field to determine whether to disable the field or not. Use something like:

    
    if ( get_field( 'mandatory-field-key', get_the_id() ) ) {
      // field has a value, probably don't do anything
    } else {
      // field doesn't have a value, disable the other fields
    }
    

    To disable WordPress standard fields like the Title, you can try something like the code below.

    Note: this uses the CSS ID of the HTML element to “select” it, then “disable” it… you can do the same to target other non-text ACF fields. You can control the ID of all ACF fields (look at the Wrapper Attributes).

    
    /* START: Disables some fields until certain criteria is met */
    function my_set_fields_to_read_only() {
    
      $allowed_post_types = array('projects');
      if ( !in_array( get_post_type(), $post_types ) ) { return; }
      
      // if the important field has a value,
      // we don't need to do anything else
      if ( get_field( 'mandatory-field-key' ) ) { return; }
      
      // JavaScript to disable fields
      echo "<script type='text/javascript'>";
      echo "  ( function ( $ ) {";
      echo "    $( document ).ready( function () {";
      echo "      $('#title').attr('disabled','disabled');";
      echo "      $('#title').css({'background': '#f8f8f8'});";
      echo "    });";
      echo "  }( jQuery ) );";
      echo "</script>";
    
      return;
    }
    add_action( 'edit_form_advanced', 'my_set_fields_to_read_only' );
    /* END: Disables some fields until certain criteria is met */
    

    I know that’s a lot thrown in one reply. I hope that you can pick it apart and are able to move forward with your project! Let me know.