Support

Account

Home Forums Backend Issues (wp-admin) Getting user_id from user-edit.php into load_field filter

Unread

Getting user_id from user-edit.php into load_field filter

  • Hi,

    I am building a domain / hosting manager using acf and the repeater plugin.

    I’m adding custom fields to the users profile. i.e. user-edit.php?user_id=4

    I have one repeater called domains, and a second called hosting.

    A client may have multiple domains but only one hosting package and as such the hosting package needs to be assigned a ‘primary’ domain from the list of domains already entered.

    I’m using code based on this:
    http://www.advancedcustomfields.com/resources/tutorials/dynamically-populate-a-select-fields-choices/

    To populate a drop down of domain names. It all works fine if I use this code in my functions.php:

    ———————————

    /*
    // This is commented out but it works!
    echo 'user_id: ' . $_GET['user_id'];
    exit; 
    */
    
    function my_acf_load_field( $field ) {
    
    	/*
    	// This DOES NOT WORK! user_id is blank!
    	echo 'user_id: ' . $_GET['user_id'];
    	exit; 
    	*/
    
    	// reset choices
    	$field['choices'] = array();
     
    	$domains = get_field('domain_names', 'user_4');
    	//$domains = get_field('domain_names', 'user_' . $user_id);
     
    	if($domains) {
    
    		while(has_sub_field('domain_names', 'user_4')) {
    		//while(has_sub_field('domain_names', 'user_' . $user_id)) {
    				
    			$url = get_sub_field('domain_name');
    			if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
    		        $url = "http://" . $url;
    		    }
    			
    			$field['choices'][ $url ] = $url;					
    		}
    	}
     
        // Important: return the field
        return $field;
    }
     
    add_filter('acf/load_field/name=primary_domain', 'my_acf_load_field’);

    ———————————

    The problem is that it only works with a hardcoded user id. No matter what I do I can’t dynamically insert the ‘4′ from the example code above.

    From reading the wordpress codex and various forums it seems I should be able to get the user_id either from the GET variables or global variables.

    However from within the my_acf_load_field function user_id does not exist and I can’t access any $_GET or global variables. They just come back empty or null.

    If I echo:

    $screen = get_current_screen();

    or $pagenow

    I get ‘admin-ajax.php’ instead of ‘user-edit.php’ which I feel may be the source of the problem.

    Any ideas how to get the user_id in this scenario?

Viewing 1 post (of 1 total)

The topic ‘Getting user_id from user-edit.php into load_field filter’ is closed to new replies.