• Resolved stardeuche

    (@stardeuche)


    Hi,

    I’m creating a complex custom fields in user profile of my wordpress. Now, i have got a awesome complex fields in my user profile for select the option for my user…greaat !!. Here is my code of my complex field :

    
    // Add field in user profile_personal_options
    Container::make('user_meta', 'Entité utilisateurs')
        ->add_fields(array(
            field::make('complex', 'crb_entite_utilisateurs')
            ->add_fields(array(
                Field::make('select', 'crb_select_users_entite', 'Entité assignée à l\'utilisateur')
                  ->add_options( spm_list_entite() )
                  ))
        ));
    

    But i want to retrieve this datas for put in my custom columns in admin user screen. First i create my custom column :

    
    function modify_columns_users($columns) {
    	return array_merge($columns, array(
    		'entite'	=> 'Entité'
    	));
    }
    add_filter('manage_users_columns', 'modify_columns_users');
    

    then, i want to put this datas of complex fields in this custom columns. Here is my code :

    
    function add_content_entite_users($value, $column_name, $user_id) {
    	$user = carbon_get_user_meta( $user_id, 'crb_entite_utilisateurs' );
    	if($column_name == 'entite') {
    		echo $user;
    	}
    	return $value;
    }
    add_filter('manage_users_custom_column', 'add_content_entite_users');

    With this code i have got a error in my back office wordpress :

    • Missing argument 2 for add_content_entite_users()
    • Missing argument 3 for add_content_entite_users()
    • Undefined variable: user_id

    I don’t understand my mistake. Have you got an idea ?

    thanks so lot

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Hi @stardeuche ,

    You’ve missed to add the priority and number of arguments in your add_filter() call – it should be like this:

    
    add_filter('manage_users_custom_column', 'add_content_entite_users', 10, 3);
    

    You must always specify the number of expected arguments if your filter function expects more than 1 argument.

    Also you should move the carbon_get_user_meta() call to be inside the if() so you do not get the meta for every column but just for the column you need, like so:

    
    if($column_name == 'entite') {
    	$user = carbon_get_user_meta( $user_id, 'crb_entite_utilisateurs' );
    	echo $user;
    }
    

    This makes the final code look like this:

    
    function add_content_entite_users($value, $column_name, $user_id) {
    	if($column_name == 'entite') {
    		$user = carbon_get_user_meta( $user_id, 'crb_entite_utilisateurs' );
    		echo $user; // NOTE: you should probably return $user, not echo
    	}
    	return $value;
    }
    add_filter('manage_users_custom_column', 'add_content_entite_users', 10, 3);
    
    Thread Starter stardeuche

    (@stardeuche)

    Hi,

    thanks so lot for your response.

    but i have got another problem because my code had changed. I’m using a complex fielf for put more parameters by an user. My code is here :

    
    // Add field in user profile_personal_options
    Container::make('user_meta', 'Entité utilisateurs')
        ->add_fields(array(
            field::make('complex', 'crb_champ_entite_utilisateurs')
            ->add_fields(array(
                Field::make('select', 'crb_select_users_entite', 'Entité assignée à l\'utilisateur')
                  ->add_options( spm_list_entite() )
                  ))
        ));
    

    In add options, i’m putting an array with this code

    
    function spm_list_entite() {
      //on check la présence du terme entite de la taxonomie category
      $checkTermes = term_exists( 'entite', 'category' );
      if($checkTermes !== 0) {
        //initialisation du tableau dans lequel on stocke les entités assignées aux articles
        $arrayVarLogin = array('SUPER ADMIN');
    
        $argsTerms = array (
          'order_by'	=> 'name',
          'order'			=> 'ASC',
          'fields'		=> 'all'
        );
        $termsChildEntites = get_terms( 'category', $argsTerms );
    
          foreach ($termsChildEntites as $termsChildEntite ) {
            if($termsChildEntite->parent > 0 && $termsChildEntite->parent == 13) { //13 est l'id du terme entite. Avec cet id on peut trouver les termes enfants de ce terme parent
              $nameChild = $termsChildEntite->name;
    
              //on push les données dans le tableau $arrayVarLogin
              array_push($arrayVarLogin, $nameChild);
    
              //$arrayVarLogin[$nameChild] = $nameChild;
    
            }
          }
          return $arrayVarLogin;
      } else {
        $defaultEntiteArrays = array('default_array'  =>  'Aucune entité pour cet utilisateur');
        return $defaultEntiteArrays;
      }
    }

    For my other code i’m using your recommendations but my code doesn’t work

    // Add entités user role admin screen
    function modify_columns_users($columns) {
    	return array_merge($columns, array(
    		'entite'	=> 'Entité'
    	));
    }
    add_filter('manage_users_columns', 'modify_columns_users');
    
    function add_content_entite_users($value, $column_name, $user_id) {
    	if($column_name == 'entite') {
    		$user = carbon_get_user_meta( $user_id, 'crb_champ_entite_utilisateurs' );
    			echo $user;
    	}
    	return $value;
    }
    add_filter('manage_users_custom_column', 'add_content_entite_users', 10, 3);

    thanks so lot for your big help…and sorry for my late 🙁

    bye

    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Your problem is not really in the scope of Carbon Fields as it’s related to WordPress admin columns.

    I’d suggest you check out the WordPress admin column documentation or find a suitable admin column plugin/library.

    Thread Starter stardeuche

    (@stardeuche)

    Hi,

    ok thanks for your response

    bye 😉

    Thread Starter stardeuche

    (@stardeuche)

    Hi,

    Sorry but i come back to you with this problem. I don’t uderstand the datas return by a select in complex field

    In your documentation, i can see that your datas return with the default keys value but in my code, i’m trying to return an array with a custom key for retrieve this datas in my users admin column.

    For retrieve this datas i’m creating a function PHP. With this function i’m putting a custom keys arrays but in a complex field i can’t retrieve the good datas

    My code is here

    
    function spm_list_entite() {
      //on check la présence du terme entite de la taxonomie category
      $checkTermes = term_exists( 'entite', 'category' );
      if($checkTermes !== 0) {
        //initialisation du tableau dans lequel on stocke les entités assignées aux articles
        $arrayVarLogin = array('0' => 'SUPER ADMIN');
    
        $argsTerms = array (
          'order_by'	=> 'name',
          'order'			=> 'ASC',
          'fields'		=> 'all'
        );
        $termsChildEntites = get_terms( 'category', $argsTerms );
    
          //on déclare la variable $i afin de modifier les keys du tableau. Le plugin carbon fields ressort uniquement les keys du tableau, il faut donc mettre les résultats
          //dans les keys du tableau
          $i = '';
    
          foreach ($termsChildEntites as $termsChildEntite ) {
            if($termsChildEntite->parent > 0 && $termsChildEntite->parent == 13) { //13 est l'id du terme entite. Avec cet id on peut trouver les termes enfants de ce terme parent
              $nameChild = $termsChildEntite->name;
    
              //incrémentation de la variable $i à chaque boucle
              $i++;
    
              //on push les données dans le tableau $arrayVarLogin
              array_push($arrayVarLogin, $nameChild);
    
              //on modifie les keys du tableau avec nos keys personnalisées
              $arrayVarLogin[$nameChild] = $arrayVarLogin[$i];
              unset($arrayVarLogin[$i]);
    
            }
          }
          return $arrayVarLogin;
        } else {
          $defaultEntiteArrays = array('default_array'  =>  'Aucune entité pour cet utilisateur');
          return $defaultEntiteArrays;
        }
    }

    with a var_dump my datas return are strange

    
    array (size=1)
      0 => 
        array (size=2)
          '_type' => string '_' (length=1)
          'crb_select_users_entite' => string '0' (length=1)
    
    array (size=1)
      0 => 
        array (size=2)
          '_type' => string '_' (length=1)
          'crb_select_users_entite' => string '5' (length=1)
    
    array (size=3)
      0 => 
        array (size=2)
          '_type' => string '_' (length=1)
          'crb_select_users_entite' => string '4' (length=1)
      1 => 
        array (size=2)
          '_type' => string '_' (length=1)
          'crb_select_users_entite' => string '3' (length=1)
      2 => 
        array (size=2)
          '_type' => string '_' (length=1)
          'crb_select_users_entite' => string '1' (length=1)
    

    I have got a difference with this datas of my function PHP and the datas of a complex field…i’m very sorry but i don’t understand my mistake

    thanks so lot

    Thread Starter stardeuche

    (@stardeuche)

    Hi,

    I found it works !!!

    A little mistake in my code and in my procedure in back office wordpress

    Sorry for the inconvenience

    bye 🙂 !

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘problem with a custom field of user profile’ is closed to new replies.