Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Save userdata from checkboxes. WordPress

I have one function that adds checkboxes that are driven from a taxonomy called 'Access' to the user edit screen.


$types = get_terms('type');
foreach ( $types as $type ) {
echo '<input type="checkbox" id="" name="'" value="/> ' . $type->name . '<br />';
}
?>


I need to be able to select any combination of these and save them to one piece of meta data as a comma separated string. So I'll also need another function

function my_save_extra_profile_fields( $user_id ) {

if ( !current_user_can( 'edit_user', $user_id ) )
return false;



update_usermeta( $user_id, 'access', $access );

}


Please can somebody provide the <strong>full code</strong> for providing the correct code in the edit screen (name and values for the checkboxes) and then also to save the data.

Thanks,
Steve

Answers (2)

2012-02-07

Luis Abarca answers:

First, change the checkbox to this.

$types = get_terms('type');

foreach ( $types as $type ) {

echo '<input type="checkbox" id="' . $type->slug . '" name="access[]" value="' . $access[$loop] . '"/> ' . $type->name . '<br />';

}

What is $access and $loop?

And for store the data

function my_save_extra_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) )
return false;

update_usermeta( $user_id, 'access', $_POST['access'] );
}


Steven Jones comments:

The $access and $loop was my code to store in an array so the checkbox would be ticked again when I go back into a profile. If that's not the correct way, please provide the correct code.

Cheers.


Steven Jones comments:

I've edited my base code to remove unnecessary items so please use that for reference.


Luis Abarca comments:

You still need the name of the checkbox, so you can get the selected values from as an array in the save post hook.


echo '<input type="checkbox" id="' . $type->slug . '" <strong>name="access[]"</strong> /> ' . $type->name . '<br />';


save post hook

function my_save_extra_profile_fields( $user_id )
{
if ( !current_user_can( 'edit_user', $user_id ) )

return false;

// get the selected checkbox as an array
$access = $_POST['access'];

// serialize the data in post meta
update_usermeta( $user_id, 'access', serialize($access) );
}


Steven Jones comments:

It's still not working - have you tested it?

I don't understand how that would get the access meta value without doing a get_usermeta.


echo '<input type="checkbox" id="' . $type->slug . '" name="access[]" /> ' . $type->name . '<br />';


Please can you provide the full, working code.

Cheers.

2012-02-07

John Cotton answers:

Try this:


function edit_user_profile( $user ) {
$terms = get_terms('access');

$access = get_user_meta($user->ID, '_access', true);

foreach( $terms as $term ) {
echo sprintf( '<input id="value_%1$d" name="access[%1$d]" value="%1$d" type="checkbox" %3$s/> %2$s', $term->term_id, $term->name, checked( true, isset($access[$term->term_id]), false ) );
}
}
add_action( 'edit_user_profile', 'edit_user_profile');

function save_user_profile( $user_id ) {
if( isset($_POST['access']) ) {
update_user_meta($user_id, '_access', $_POST['access'] );
}
}
add_action( 'personal_options_update', 'save_user_profile');
add_action( 'edit_user_profile_update', 'save_user_profile');


Steven Jones comments:

Hi John,

Thanks for this - how would I print out a comma separated list of the values in the array for use within my theme?

Cheers,
Steve


John Cotton comments:

$access = get_user_meta($user->ID, '_access', true);
echo implode ( ', ', array_values($access) );


Steven Jones comments:

Spot on - thanks.