Support

Account

Home Forums General Issues Dynamic value for message field

Solved

Dynamic value for message field

  • Hi

    I would like to be able to add a message field with a dynamic value, including the current post id etc. I am trying to use the code below to update the field but am not sure if the message field works with the update_field function and which admin hook to use.

    global $post;
    	
    $field_key = "field_52010a6df2739";
    $value = $post->ID;  
    update_field( $field_key, $value, $post->ID );
    

    Thanks

    • Elliot

    • August 7, 2013 at 12:38 pm

    Hi @phantomdentist

    I don’t think you need to use the update_field function. This function is to update a value and would not affect the message field data.

    Perhaps what you realy want is to use the acf/load_field filter and customize the $field data on the fly using the current $post data

    Please read the docs to learn more about this filter

  • Thanks Elliot!

    • Beee

    • March 27, 2017 at 3:28 am

    I’m sorry if I continue on this instead of creating a new topic, but i’m exactly looking to do the same. For me load_field updates the message in the back-end instead of doing it on the fly.

    I wrote a placeholder in themessage text, which gets replaced with the value I set in the function. Maybe I understand the function incorrectly, but I thought this function was meant to override the message value before being displayed, but not store it, since it’s different per user/post.

  • Not updating the value in the DB you’ll need to add some conditionals in your load field filter so that it is not altered when editing the field group.

    
    add_filter('acf/load_field/name=field_name', 'alter_message');
    function alter_message($field) {
      $post = get_queried_object();
      if (isset($post->post_type, $post->ID) && $post->post_type != 'acf-field-group') {
        // alter the field
      }
      return $field;
    }
    
    • Beee

    • March 27, 2017 at 5:21 am

    I came to a similar conclusion and ended up with this:

    
    if ( is_page( 'edit-ad' ) ) {
        if ( isset( $_GET['id'] ) ) {
            $ad_id = $_GET['id'];
            $location_values = get_field( 'sd_city_selector', $ad_id );
            $field['instructions'] = '<strong>Your location is: ' . $location_values['cityNameAscii'] . ' (' . $location_values['countryCode'] . ')</strong>';
        }
    }
    
    

    I wanted to populate a message field, but for space I decided to use the instructions field instead.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.