Viewing 3 reply threads
  • Author
    Posts
    • #4493
      rcreators
      Member

      Hello Piklist,

      I created one setting page in which i can add departments with text field add more true function. Below is a code i used for it.

      /*
      Title: Departments
      Setting: wp_hr_departments
      Order: 10
      Tab Order: 10
      */

      piklist(‘field’, array(
      ‘type’ => ‘text’
      ,’field’ => ‘departments’
      ,’label’ => __(‘Departments’)
      ,’description’ => __(‘Add as many departments you want.’)
      ,’columns’ => ’12’
      ,’required’ => true
      ,’add_more’ => true
      ,’attributes’ => array(
      ‘class’ => ‘text’
      )
      ));

      This what above code generates: http://prntscr.com/8pgrhs

      Now i am trying to get this department in user profile page. so i created users meta field for same. below is a code.

      /*
      Title: User Department
      Description: Select department for user.
      Capability: manage_wp_hr
      */

      $user_department = get_option(‘wp_hr_departments’);

      piklist(‘field’, array(
      ‘type’ => ‘checkbox’
      ,’field’ => ‘user_department’
      ,’label’ => ‘User Department’
      ,’attributes’ => array(
      ‘class’ => ‘text’
      )
      ,’choices’ => $user_department[‘departments’]
      ));

      This what above code generates: http://prntscr.com/8pgs8x

      Everything works fine but find one issue. As in setting data is stored as serialized, first field value stored as ‘0’. Now when i select first option in user meta box, it don’t store ‘0’ value. so can’t select first option.

      After data saved for usermeta, if i change order of department in setting page, order in usermeta also changed but usermeta selection value also change as serialize value 0,1,2 assigned to new order. So can we have options to have fix value for add more fields and it stays with it whether we change order.

    • #4500
      rcreators
      Member

      Hello,

      I just updated to beta version on piklist, One thing is already sorted which is first item can select now. But second problem is still there.

      From repeat field if i changed the order, my old selection also changed.

      While this order : http://i.imgur.com/uPtW2GH.png – result in selection is this : http://prntscr.com/8pjg4e

      Now i just changed order in repeat field – This order : http://prntscr.com/8pjgib – result also changed in selection : http://prntscr.com/8pjgu6

      I think from above you get my point. Changing order is changing selection. It won’t be happen like that. If i change the order. on profile page only order needs to update, selection needs to be remains same values.

    • #4501
      rcreators
      Member

      That was little bit tricky with Core PHP.

      I did Array_combine with my values and it worked. Now field value is actual field name rather than 0,1,2,3

      Here is final code:

      /*
      Title: User Department
      Description: Select department for user.
      Capability: manage_wp_hr
      */
      
      $user_departments = get_option('wp_hr_departments'); // Getting Setting
      $user_department = $user_departments['departments']; // Getting array value of repeat field
      $user_department_array = array_combine($user_department, $user_department); // Generating new array where value and key both are same
      
      piklist('field', array(
      	'type' => 'checkbox'
      	,'field' => 'user_department'
      	,'label' => 'User Department'
      	,'attributes' => array(
      		'class' => 'text'
      	)
      	,'choices' => $user_department_array
      ));

      Hope this will help someone in future.

    • #4503
      Steve
      Keymaster

      @rcreators– Great job!

      You can also use array_combine at the field level.

      INSTEAD OF:
      'choices' => $user_department['departments']

      USE: 'choices' => array_combine($user_department['departments'], $user_department['departments'])

      Piklist is very flexible.

Viewing 3 reply threads
  • You must be logged in to reply to this topic.