Support

Account

Home Forums Feature Requests Options page input used as field label for other custom fields

Solved

Options page input used as field label for other custom fields

  • Something I would LOVE to have is having fields on an options page, and use input from those fields as labels on another custom field.

    Make sense? Food for thought. ;]

  • Set the default label when you create the field

    
    add_filter('acf/load_field/key=field_0123456789abc', 'set_my_field_label');
    function set_my_field_label($field) {
      $label = get_field('field_label_option', 'option');
      if ($label) {
        $field['label'] = $label;
      }
      return $field;
    }
    
  • Being the relative newbie that I am, where would I plug this in? On my template page? Can you run through what the parts of this does to accomplish what I’m wishing for? And how would one choose which custom field this would show up as a label for?

  • Put it in your functions.php file

    
    // this adds a filter for the field that you want
    // to dynamically set the label for
    // it can be used in several ways
    // https://www.advancedcustomfields.com/resources/acfload_field/
    add_filter('acf/load_field/key=field_0123456789abc', 'set_my_field_label');
    
    // this is the function that is called for the filter
    function set_my_field_label($field) {
      // get the value from the field you want to use for the label
      $label = get_field('field_label_option', 'option');
      if ($label) {
        // if the option is set then change the field label
        // almost any field argument can be changed dyamically
        $field['label'] = $label;
      }
      return $field;
    }
    
  • Thank you for your reply. Sorry, I have yet more questions. How do I tell a field to populate the label with the value from that stored other value?

    Can this be done to multiple fields?

  • Yes, you would need to create a unique filter for each field. Of you can make the decision base on the field.

    You can run your filter on every field, or a specific field as explained in the document I linked to above.

    ACF passes the current field values to your function in $field

    
    function set_my_field_label($field) {
    

    This will have the field name, field key, and all the other information about the field. If you want to see what you have to work with add

    
    echo '<pre>'; print_r($field); echo '</pre>';
    

    to the top of your filter, I think this is also explained in the documentation. This will output all of the field values. You can then base what field you get from the options page on one of the field values, like the name.

  • Perfect! I have a lot to look into at work with, but for the time being, this will help me work.

    Thank you!

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.