Support

Account

Home Forums General Issues How to set default custom field value on new posts for a specific user Reply To: How to set default custom field value on new posts for a specific user

  • I’m afraid is not working.. wordpress is no longer loading!
    Questions: where do I set the custom field name?
    Oh and for the moment learning to create a plugin is beyond my learning scope right now..

    I have so far:

    function my_acf_prepare_field( $field ) {
      $field['readonly'] = true; // optionally make the field read-only
      if ( $field['Madrid'] ) { return $field; } // bail if field has a value already
      $for_user = 2;
      $current_user_id = (int)get_current_user_id();
      if ( $current_user_id != $for_user ) { return $field; } // bail if not specified user
      $ip = my_get_field_function();
      if ( $ip ) {
        $field['value'] = $ip;
      }
      return $field;
    }
    add_filter('acf/prepare_field/name=region', 'my_acf_prepare_field');
    
    // this functions gets the custom field value
    function my_get_field_function() {
      if ( !empty( $field] ) ) {
        $field_value = $field;
      } else if ( !empty( 'Madrid' ) ) {
        $field_value = 'Madrid';
      } else {}
      return $field_value;
    }