Home › Forums › General Issues › Creating a new field type from the users table
Hi. iv been knocking my head repeatable to try get this to work. Iv found this plugin amamazing! an absolute peach. But alas i am not rather confussed and stuck.
What im trying to do is create a new field type that is a select field that contains all the users from the wp-user table. So far so good! the trick is i would like to assign those users a value from a your normal select field.
The form lives on the option page, and uses a repeater field.
The custom “New field” pulls in the users just fine. but does not update when assigning them a vaule.
The normal field updates perfectly.
I cant seem to create the logic from a custom select field to link to normal select field.
Basicly the new field type does not save?
where am i going wrong
below is the code im using
<?php
class acf_field_all_users extends acf_field
{
// vars
var $settings, // will hold info such as dir / path
$defaults; // will hold default field options
/*
* __construct
*
* Set name / label needed for actions / filters
*
* @since 3.6
* @date 23/01/13
*/
function __construct()
{
// vars
$this->name = 'All Users';
$this->label = __('All Users');
$this->category = __("Basic",'acf'); // Basic, Content, Choice, etc
$this->defaults = array(
// add default here to merge into your field.
// This makes life easy when creating the field options as you don't need to use any if( isset('') ) logic. eg:
//'preview_size' => 'thumbnail'
);
// do not delete!
parent::__construct();
// settings
$this->settings = array(
'path' => apply_filters('acf/helpers/get_path', __FILE__),
'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
'version' => '1.0.0'
);
}
/*
* create_options()
*
* Create extra options for your field. This is rendered when editing a field.
* The value of $field['name'] can be used (like bellow) to save extra data to the $field
*
* @type action
* @since 3.6
* @date 23/01/13
*
* @param $field - an array holding all the field's data
*/
function create_options($field)
{
// defaults?
/*
$field = array_merge($this->defaults, $field);
*/
// key is needed in the field names to correctly save the data
$key = $field['name'];
// Create Field Options HTML
?>
<tr class="field_option field_option_<?php echo $this->name; ?>">
<td class="label">
<label><?php _e("Preview Size", 'acf'); ?></label>
<p class="description"><?php _e("Thumbnail is advised", 'acf'); ?></p>
</td>
<td>
<?php
do_action('acf/create_field', array(
'type' => 'radio',
'name' => 'fields[' . $key . '][preview_size]',
'value' => $field['preview_size'],
'layout' => 'horizontal',
'choices' => array(
'thumbnail' => __('Thumbnail'),
'something_else' => __('Something Else'),
)
));
?>
</td>
</tr>
<?php
}
/*
* create_field()
*
* Create the HTML interface for your field
*
* @param $field - an array holding all the field's data
*
* @type action
* @since 3.6
* @date 23/01/13
*/
function create_field( $field )
{
global $wpdb;
// get the wordpress users
$wp_user_search = $wpdb->get_results("SELECT * FROM $wpdb->users ORDER BY display_name");
$choices = array();
$blogusers = get_users('role=subscriber');
foreach ($blogusers as $user) {
// echo '<li>' . $user->user_email . '</li>';
var_dump($user);
$user_id = (int) $user->ID;
$display_name = stripslashes($user->display_name);
$choices[$user_id] = $display_name;
}
$field['choices'] = $choices;
// defaults
$field['value'] = isset($field['value']) ? $field['value'] : array();
$field['multiple'] = isset($field['multiple']) ? $field['multiple'] : false;
$field['allow_null'] = isset($field['allow_null']) ? $field['allow_null'] : false;
$field['optgroup'] = isset($field['optgroup']) ? $field['optgroup'] : false;
var_dump($field);
// no choices
if(empty($field['choices']))
{
echo '<p>' . __("No choices to choose from",'acf') . '</p>';
return false;
}
// multiple select
$multiple = '';
if($field['multiple'] == '1')
{
$multiple = ' multiple="multiple" size="5" ';
$field['name'] .= '[]';
}
// html
echo '<select id="' . $field['name'] . '" class="' . $field['class'] . '" name="' . $field['name'] . '" ' . $multiple . ' >';
// null
if($field['allow_null'] == '1')
{
echo '<option value="null"> - Select - </option>';
}
// loop through values and add them as options
foreach($field['choices'] as $key => $value)
{
if($field['optgroup'])
{
// this select is grouped with optgroup
echo '<optgroup label="'.$key.'">';
if($value)
{
foreach($value as $id => $label)
{
$selected = '';
/***************
*
*
*
* selects here
*
*
*
*
*
*
*
*
*
*
*
*
*
********************/
if(is_array($field['value']) && in_array($id, $field['value']))
{
// 2. If the value is an array (multiple select), loop through values and check if it is selected
$selected = 'selected="selected"';
}
else
{
// 3. this is not a multiple select, just check normaly
if($id == $field['value'])
{
$selected = 'selected="selected"';
}
}
echo '<option value="'.$id.'" '.$selected.'>'.$label.'</option>';
}
}
echo '</optgroup>';
}
else
{
$selected = '';
if(is_array($field['value']) && in_array($key, $field['value']))
{
// 2. If the value is an array (multiple select), loop through values and check if it is selected
$selected = 'selected="selected"';
}
else
{
// 3. this is not a multiple select, just check normaly
var_dump($field['value']);
if($key == $field['value'])
{
$selected = 'selected="selected"';
}
}
echo '<option value="'.$key.'" '.$selected.'>'.$value.'</option>';
}
}
echo '</select>';
}
/*
* input_admin_enqueue_scripts()
*
* This action is called in the admin_enqueue_scripts action on the edit screen where your field is created.
* Use this action to add css + javascript to assist your create_field() action.
*
* $info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
* @type action
* @since 3.6
* @date 23/01/13
*/
function input_admin_enqueue_scripts()
{
// Note: This function can be removed if not used
// register acf scripts
wp_register_script('acf-input-all-users', $this->settings['dir'] . 'js/input.js', array('acf-input'), $this->settings['version']);
wp_register_style('acf-input-all-users', $this->settings['dir'] . 'css/input.css', array('acf-input'), $this->settings['version']);
// scripts
wp_enqueue_script(array(
'acf-input-{{field_name}}',
));
// styles
wp_enqueue_style(array(
'acf-input-{{field_name}}',
));
}
/*
* input_admin_head()
*
* This action is called in the admin_head action on the edit screen where your field is created.
* Use this action to add css and javascript to assist your create_field() action.
*
* @info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head
* @type action
* @since 3.6
* @date 23/01/13
*/
function input_admin_head()
{
// Note: This function can be removed if not used
}
/*
* field_group_admin_enqueue_scripts()
*
* This action is called in the admin_enqueue_scripts action on the edit screen where your field is edited.
* Use this action to add css + javascript to assist your create_field_options() action.
*
* $info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts
* @type action
* @since 3.6
* @date 23/01/13
*/
function field_group_admin_enqueue_scripts()
{
// Note: This function can be removed if not used
}
/*
* field_group_admin_head()
*
* This action is called in the admin_head action on the edit screen where your field is edited.
* Use this action to add css and javascript to assist your create_field_options() action.
*
* @info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head
* @type action
* @since 3.6
* @date 23/01/13
*/
function field_group_admin_head()
{
// Note: This function can be removed if not used
}
/*
* load_value()
*
* This filter is appied to the $value after it is loaded from the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value - the value found in the database
* @param $post_id - the $post_id from which the value was loaded from
* @param $field - the field array holding all the field options
*
* @return $value - the value to be saved in te database
*/
function load_value($value, $post_id, $field)
{
// Note: This function can be removed if not used
return $value;
}
/*
* update_value()
*
* This filter is appied to the $value before it is updated in the db
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value - the value which will be saved in the database
* @param $post_id - the $post_id of which the value will be saved
* @param $field - the field array holding all the field options
*
* @return $value - the modified value
*/
function update_value($value, $post_id, $field)
{
// Note: This function can be removed if not used
return $value;
}
/*
* format_value()
*
* This filter is appied to the $value after it is loaded from the db and before it is passed to the create_field action
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value - the value which was loaded from the database
* @param $post_id - the $post_id from which the value was loaded
* @param $field - the field array holding all the field options
*
* @return $value - the modified value
*/
function format_value($value, $post_id, $field)
{
// defaults?
/*
$field = array_merge($this->defaults, $field);
*/
// perhaps use $field['preview_size'] to alter the $value?
// Note: This function can be removed if not used
return $value;
}
/*
* format_value_for_api()
*
* This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $value - the value which was loaded from the database
* @param $post_id - the $post_id from which the value was loaded
* @param $field - the field array holding all the field options
*
* @return $value - the modified value
*/
function format_value_for_api($value, $post_id, $field)
{
// defaults?
/*
$field = array_merge($this->defaults, $field);
*/
// perhaps use $field['preview_size'] to alter the $value?
// Note: This function can be removed if not used
return $value;
}
/*
* load_field()
*
* This filter is appied to the $field after it is loaded from the database
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $field - the field array holding all the field options
*
* @return $field - the field array holding all the field options
*/
function load_field($field)
{
// Note: This function can be removed if not used
return $field;
}
/*
* update_field()
*
* This filter is appied to the $field before it is saved to the database
*
* @type filter
* @since 3.6
* @date 23/01/13
*
* @param $field - the field array holding all the field options
* @param $post_id - the field group ID (post_type = acf)
*
* @return $field - the modified field
*/
function update_field($field, $post_id)
{
// Note: This function can be removed if not used
return $field;
}
}
// create field
new acf_field_all_users();
?>
The topic ‘Creating a new field type from the users table’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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.