latest stable versions: v150827 (changelog)

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Get User's Selected Checkboxes With PHP

Home Forums Community Forum Unofficial Extensions/Hacks Get User's Selected Checkboxes With PHP

This topic contains 3 replies, has 3 voices. Last updated by  Ryan McNeil 3 years, 7 months ago.

Topic Author Topic
Posted: Wednesday Nov 7th, 2012 at 11:07 pm #30983
Bruce
Username: Bruce
Staff Member

Hack Description

If you're looking for a way to show Users what they (or other users) have selected within one of your Multi-Checkbox Custom Profile Fields, you may be running into issues. The following should help you do this.

Hack Instructions

1. Move the Code

Put the following code into a Must-Use Plugin file (if you don't have a /wp-content/mu-plugin directory, create it):

'Virginia', 'GA', 'Georgia', 'AK' => 'Alaska'))
*/
function s2hack_get_user_checkboxes($field_ids, $id = null)
{
	global $wpdb;
	global $current_user;
	/**/
	$field_ids = (array)$field_ids;
	/**/
	if($id === NULL)
		$id = $current_user->ID;
	/**/
	$values = array();
	/**/
	$sel_fields = get_user_meta($id, $wpdb->prefix.'s2member_custom_fields', true);
	$all_fields = get_s2member_custom_fields();
	/**/
	foreach($field_ids as $f):
		$pos = $all_fields[$f]['config']['options'];
		$sel = $sel_fields[$f];
		$values[$f] = array();
		/**/
		$pos = explode("n", $pos);
		foreach($pos as $key => $str)
			{
				$str = explode('|', $str);
				$pos[$str[0]] = $str[1];
				unset($pos[$key]);
			}
		/**/
		foreach($sel as $sf)
			{
				if($pos[$sf] !== NULL)
					$values[$f][$sf] = $pos[$sf];
			}
	endforeach;
	/**/
	return $values;
}
2. Use the Function Created

Use the function we created within the Must-Use plugin to get the user's selected fields. This can be accomplished like this:

<?php
$selected_fields = s2hack_get_user_checkboxes('countries');
$countries = $selected_fields['countries'];

echo &#039;<ul>';
foreach($countries as $id =&gt; $country):
	echo '<li>'.$country.'</li>';
endforeach;
echo '</ul>';

The above code will output the selected checkboxes for the s2Member field countries as a list.

List Of Topic Replies

Viewing 3 replies - 1 through 3 (of 3 total)
Author Replies
Author Replies
Posted: Friday Nov 9th, 2012 at 12:58 pm #31155

Having trouble implementing this hack. I’m using an older theme “s2clean”, I created a new directory in /wp-content/ named mu-plugin and named my file must-use.php. This seems pretty straight forward and I figure I’m not messing any of this up so far. However I can’t seem to get the 2nd code to output my custom fields from my profile/registration page.

Posted: Tuesday Nov 13th, 2012 at 5:09 pm #31488
Bruce
Username: Bruce
Staff Member

Hi Avery,

Sorry for the delayed response

Could you try making sure that your field ID (i.e. ‘countries’ in the example code) is correct? Also, you may want to try creating a new User and setting up fields, and calling the function with his ID as the second arguments to ensure that it is not simply trying to get data from the wrong source. You can find the User’s ID easily with WP Show IDs

Posted: Wednesday May 29th, 2013 at 11:03 pm #50889
Ryan McNeil
Username: rmcneil

Bruce,
Thanks for this hack.

A related question:

Is there any way to list all the multi-option variable choices (not user specific)?
I want to use foreach to create a list of specialties (there are over 40) that will change dynamically if the client adds more specialties, or edits or deletes current ones. Is this possible?

Thanks again!

Ryan McNeil

Viewing 3 replies - 1 through 3 (of 3 total)

This topic is closed to new replies. Topics with no replies for 2 weeks are closed automatically.

Old Forums (READ-ONLY): The community now lives at WP Sharks™. If you have an s2Member® Pro question, please use our new Support System.

Contacting s2Member: Please use our Support Center for bug reports, pre-sale questions & technical assistance.