Support

Account

Forum Replies Created

  • Thank you John! It really wasn’t that clear to me that I had to use update_field() inside acf/save_post. It isn’t all too obvious in the documentation, at least not to me.

  • I don’t get the documentation for acf/update_field() either. It says the $field parameter is the field array containing all settings. All the settings of what? The field I want to collect data from? In that case it seems this should work but it doesn’t.

    
    function save_updated_acf_fields($location)
    {
        // Collect GM field data
        $location = get_field('field_634147f894e08', $post_id);
        $gmap_street_name = $location['street_name'];
        $gmap_street_nbr = $location['street_number'];
    
        // Fields retrieving updated values
        $empty_street_name = get_field('field_6341483d94e09', $post_id);
        $empty_street_nbr = get_field('field_634151528a090', $post_id);
    
        if ($location) {
            $empty_street_name = $gmap_street_name;
            $empty_street_nbr = $gmap_street_nbr;
        }
    }
    
    add_filter('acf/update_field ', 'save_updated_acf_fields', 10, 1);
    

    Would you please translate the documentation into something a plain dummy like myself can understand?

  • I have tried with the following instead:

    
    // Testing to save/update field value using another field value
    function my_acf_save_post( $post_id ) {
    
        
    // Get newly saved values.
        $values = get_fields( $post_id );
        $location = get_field('location', $post_id);
        $street_name = get_field('street_name', $post_id);
    
    	if ( ! get_post_type == 'realestate') {
    	   // Stop right away
    	   return;
    	}  else {
                    // Check the new value of a specific field.
    		if ($location['street_name']) {
    		   $street_name = $location['street_name'];
    		}
    	}
    }
    add_action('acf/save_post', 'my_acf_save_post');
    

    Of course this is not doing anything because I don’t understand the documentation properly. Please help.

  • Hi John,

    Please forget everything mentioned above because the circumstances have changed drastically since my last post. So, here comes the new conditions:

    A custom post type called “agents” is used to retrieve data from an external api, where all the info about each agent is entered and saved.

    I have registered some custom REST fields for all the incoming data (collected by an AJAX call) and then collecting and returning the data for these into any given custom field. This is the code used for that:

    // Register custom Rest field
    add_action('rest_api_init','itc_custom_rest');
    
    // Collect the data from REST and return the given custom field
    function itc_custom_rest() {
        register_rest_field('radgivare', 'minutePrice', array(
    		'get_callback' => function() {
    			return get_field('price_per_minute');
    		},
    		'get_callback' => function() {
    			return get_field('agent_status_available');
    		},
    		'get_callback' => function() {
    			return get_field('agent_status_inCall');
    		},
    		'get_callback' => function() {
    			return get_field('agent_status_loggedOut');
    		}
    	));
    }

    The new fields show up in REST alright.

    My big question now is, can I use the following code in order to echo whatever is retrieved from REST, or do I need to use something else?

    // Example used to echo certain colors and texts if the fields (which are type bool) are true
    <div class="card__btn-block">
                <a href="#" class="btn rounded <?php
                  if (get_field('agent_status_available')) {
                    echo $green_status;
                  }
                  if (get_field('agent_status_inCall')) {
                    echo $yellow_border_status;
                  } else {
                      echo $red_border_status;
                  } ?>">
                  <?php
                  if (get_field('agent_status_available')) {
                    echo 'Call Now';
                  } 
                  if (get_field('agent_status_inCall')) {
                    echo 'In Call';
                  } else {
                      echo 'Offline';
                  }
                  ?></a>
              </div>

    Thank you very much for your superior efforts of helping clueless people like myself!

  • Hi, thank you for your answer. I will give your proposals a try. But this isn’t the actual format though, is it $post_id = "user_{$user_id}";? Looks weird, almost like the javascript template literals. Please correct me if I’m wrong.

    I have found a different possible solution involving the use of get_transient() function that may or may not be a better way of handling this. I’m not sure yet, because I haven’t got it working as of yet.

    I know it’s off topic in this forum but if it could help someaone else with a similar issue, I’ll post my current code here. And just to clarify, my “advisors” are actually a custom post type where each post will have an author (user) connected to them.

    In functions.php:

    <?php
    function is_user_online( $user_id ) {
        // get the online users list
        $logged_in_users = get_transient( 'users_online' );
    
        // online, if user is in the list and last activity was less than 15   minutes ago
        return isset( $logged_in_users[$user_id] ) && ( $logged_in_users[$user_id] >     ( current_time( 'timestamp' ) - ( 15 * 60 ) ) );
    }
    ?>

    And in the template file (in my case this is in a child theme, template partial file):

    <?php
                  if (is_user_online( $post->post_author )) {
                    echo $green_status;
                  } else {
                      echo $red_border_status;
                  } ?>"><?php
                  if (is_user_online( $post->post_author )) {
                    echo 'Available';
                  } else {
                      echo 'Offline';
                  }
    
    // and the card status message
    
    if (is_user_online( $post->post_author )) {
              echo $green_status;
            }
            if (! is_user_online( $post->post_author )) {
              echo $red_fill_status;
            } ?>">
            <?php
            if (is_user_online( $post->post_author )) {
              echo 'online';
            }
            if (! is_user_online( $post->post_author )) {
              echo 'offline';
                  ?>

    This is working in the way that the css selector is actually echoed out, giving the button and the status message color AND text content. However it still does not seem to recognize if the user is actually logged in or not, because it always returns the same value – the css selector for the red color and the “Offline” status text.

  • Hey guys,

    I would like to use ACF to show whether or not a user with the role ‘advisor’ is online. As of now I have a true/false field set up (inside a group) and I can manually switch between the different statuses for each user and see the update reflected on the frontend. However I would like this to function automatically, and this is where I’m lost.

    This is the function I have produced so far – but it’s not working.

    add_action('wp_loaded', 'itc_assign_advisor_online_status');
    
    function itc_assign_advisor_online_status() {
    	$online_status = get_field('onlinestatus');
    
    	if (is_user_logged_in() && ! current_user_can( 'administrator' )) {
    		$online_status['is_online'] = 1;
    	} else {
    		update_field($online_status['is_online'], 0);
    	}
    }

    Am I using the wrong hook, maybe?

    For the record, this is my graphical interface code:

    <?php
    
    $online_status = get_field('onlinestatus');
    $green_status = 'online';
    $yellow_fill_status = 'busy--filled';
    $red_fill_status = 'offline--filled';
    $yellow_border_status = 'busy';
    $red_border_status = 'offline';
    
            if ($online_status) { ?>
              <div class="card__btn-block">
                <a href="#" class="btn rounded <?php
                  if ($online_status['is_online']) {
                    echo $green_status;
                  }
                  if ($online_status['is_busy']) {
                    echo $yellow_border_status;
                  }
                  if ($online_status['is_offline']) {
                    echo $red_border_status;
                  } ?>"><?php
                  if ($online_status['is_online']) {
                    echo 'Available';
                  }
                  if ($online_status['is_busy']) {
                    echo 'Busy';
                  }
                  if ($online_status['is_offline']) {
                    echo 'Offline';
                  }
                  ?></a>
              </div>
            <?php } ?>

    Any help is highly appreciated!

  • Ok I solved it, kind of, by setting Show if rules to:

    Current User Role is equal to Administrator AND
    Post Type is equal to ‘my_custom_post_type’

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