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.

Multi Checkbox custom field author.php page

Home Forums Community Forum Multi Checkbox custom field author.php page

This topic contains 8 replies, has 2 voices. Last updated by  Bruce 4 years, 3 months ago.

Topic Author Topic
Posted: Monday Sep 24th, 2012 at 5:04 pm #26357
Danny Albeck
Username: dalbeck

Ok, so I have been tearing my hair out. I found this thread from back in April: http://www.s2member.com/forums/topic/get-custom-profile-field-values/

I read through that and all the sub links within it. Basically on my site, I am using the members-list plugin that has been recommended a few times on here, and if you click on the name of the user it will take you to the author.php page which outputs the data in their profile to the public (someone who can be logged in or out).

I have 3 custom fields that are multi-select boxes. When I use the following code in my author.php file:

<ul>
<li><?php echo get_user_field('profile_disciplines', $thisauthor->ID); ?></li>
</ul>

Nothing prints out. So Reading around the forums and other links on here on how to get an array to print out I referenced this link: http://www.s2member.com/codex/stable/s2member/api_functions/package-functions/#src_doc_get_s2member_custom_fields%28%29

When I use the following code in my author.php file:

<?php
$fields = get_s2member_custom_fields();
print_r($fields["profile_disciplines"]["config"]); # The Unique Field ID you configured with s2Member.
?>

It returns a very messy output that looks like:

Array ( [config] => Array ( [section] => yes [sectitle] => IAPSC Profile [id] => profile_disciplines [label] => Disciplines/Expertise/Services [type] => checkboxes [deflt] => [options] => Competitive Intelligence, Counterespionage, etc.|Competitive Intelligence, Counterespionage, etc. Emergency Planning/Disaster Recovery|Emergency Planning/Disaster Recovery Expert Witness/Forensic & Litigation Support|Expert Witness/Forensic & Litigation Support Fire/Life Safety|Fire/Life Safety Information Security|Information Security Loss Prevention / Shoplifting|Loss Prevention / Shoplifting Master Plans|Master Plans Security Design, Systems & Technology|Security Design, Systems & Technology Security Operations (personnel, policies, etc.)|Security Operations (personnel, policies, etc.) Security Risk Assessments|Security Risk Assessments Terrorism / Counter Terrorism|Terrorism / Counter Terrorism Threat and Crime Analysis|Threat and Crime Analysis Training|Training Workplace Violence|Workplace Violence [expected] => [required] => no [levels] => all [editable] => yes [classes] => [styles] => [attrs] => ) )

Now when I try the following:

<?php
function get_s2member_custom_fields($user_id = $theUserID) {
    $return = array();
    $user = get_user_option('s2member_custom_fields', $user_id);

    foreach ((array)json_decode($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_fields'], true) as $field) {
        if (isset($user[$field['id']])) {
            $return[$field['id']]['label'] = $field['label'];

            if (empty($field['options']))
                $return[$field['id']]['value'] = $user[$field['id']];
            else {
                $field['options'] = strpos($field['options'], "\n") ? explode("\n", $field['options']) : (array)$field['options'];
                foreach ($field['options'] as $option) {
                    $option = explode('|', $option);
                    $options[$option[0]] = $option[1];
                }
                foreach ((array)$user[$field['id']] as $choice)
                    $return[$field['id']]['options'][$choice] = $options[$choice];
            }
        }
    }
    return $return;
}
$s2_custom_fields = get_s2member_custom_fields($theUserID); 
echo $var['profile_disciplines']['options'][0]['label'];
?>

It just returns a blank white page (assuming a PHP error, which is what Cristian said in his last post on the link to the original thread I posted. The Original poster never replied back saying he got it to work or not. I just want my custom registration field that is multi checkbox field to output as a unordered list. Can anyone please help me with this!

Thank You!

List Of Topic Replies

Viewing 8 replies - 1 through 8 (of 8 total)
Author Replies
Author Replies
Posted: Monday Sep 24th, 2012 at 8:20 pm #26374
Bruce
Username: Bruce
Staff Member

Hi Danny,

With the last code snippet you posted, PHP doesn’t allow you to set the default value of parameters with another variable. Do this instead:

  • This reply was modified 4 years, 3 months ago by  Bruce.
Posted: Tuesday Sep 25th, 2012 at 8:24 am #26423
Danny Albeck
Username: dalbeck

Bruce,

I get the following error:

Fatal error: Cannot redeclare get_s2member_custom_fields() in /wp-content/themes/iapsc/author.php on line 62

Thanks for getting back to me, hope we can figure something out.

Posted: Tuesday Sep 25th, 2012 at 12:12 pm #26445
Bruce
Username: Bruce
Staff Member

Hi Danny,

When PHP gives you that error, it means you have already made that function somewhere else. You can’t have 2 functions with the same name. Either rename the function, or find where you have that function declared somewhere else and delete it. :)

Posted: Tuesday Sep 25th, 2012 at 12:25 pm #26448
Danny Albeck
Username: dalbeck

Hmm, I feel like I might be missing something here, and I apologize if I am being a pest Bruce. Here is my entire author.php file, with the snippet included that you provided above.

<?php get_header(); ?>

<section id="title">

	<article>
		<h2>Become A Member</h2>
	</article>

</section><!--end of title-->

<section id="container">

	<article id="content">

		<aside id="left">

			<?php get_sidebar(); ?>

		</aside><!--end of left-->

		<aside id="right">

			<article>

			    <?php
			    	$thisauthor = get_userdata(intval($author));
			    ?>

			    <h2><?php echo $thisauthor->display_name; ?>, <?php echo get_user_field('profile_certifications', $thisauthor->ID);  ?></h2>

			    <?php if (have_posts()) : ?>

			    <?php endif; ?>

			    <article class="profile-details">

				    <aside class="picture">

				    	<? if(function_exists('get_avatar')) { echo get_avatar($thisauthor->user_email, 96, "" ); } ?>

				    </aside><!--end of .picture-->

				    <ul class="profile-info">
				    	<li><strong><?php echo get_user_field('profile_company', $thisauthor->ID);  ?></strong></li>
				    	<li><?php echo get_user_field('profile_address1', $thisauthor->ID);  ?></li>
				    	<li><?php echo get_user_field('profile_address2', $thisauthor->ID);  ?></li>
				    	<li><?php echo get_user_field('profile_city', $thisauthor->ID);  ?>, <?php echo get_user_field('profile_state', $thisauthor->ID);  ?> <?php echo get_user_field('profile_zip_code', $thisauthor->ID);  ?></li>
				    </ul>

				    <ul class="profile-info">
				    	<li><strong>Phone:</strong> <?php echo get_user_field('profile_phone', $thisauthor->ID);  ?></li>
				    	<li><strong>Fax:</strong> <?php echo get_user_field('profile_fax', $thisauthor->ID);  ?></li>
				    	<li><strong>Email:</strong> <a href="mailto:<? echo $thisauthor->user_email; ?>"><? echo $thisauthor->user_email; ?></a></li>
				    	<li><a href="<?php echo $thisauthor->user_url; ?>" target="_blank"><?php echo $thisauthor->user_url; ?></a></li>
				    </ul>

			    </article><!--end of .profile-details-->

			    <h3>Disciplines/Expertise/Services</h3>

			    <?php
				    function get_s2member_custom_fields($user_id) {
					    if($user_id === NULL)
						    $user_id = $theUserID;
						    $return = array();
						    $user = get_user_option('s2member_custom_fields', $user_id);
				    foreach ((array)json_decode($GLOBALS['WS_PLUGIN__']['s2member']['o']['custom_reg_fields'], true) as $field) {
					    if (isset($user[$field['id']])) {
					    $return[$field['id']]['label'] = $field['label'];
				    if (empty($field['options']))
				    	$return[$field['id']]['value'] = $user[$field['id']];
				    else {
				    	$field['options'] = strpos($field['options'], "\n") ? explode("\n", $field['options']) : (array)$field['options'];
				    foreach ($field['options'] as $option) {
				   	 $option = explode('|', $option);
				   	 $options[$option[0]] = $option[1];
				    }
				    foreach ((array)$user[$field['id']] as $choice)
				    	$return[$field['id']]['options'][$choice] = $options[$choice];
				    }
				    	}
				    }
				    return $return;
				    	}
				    $s2_custom_fields = get_s2member_custom_fields($theUserID);
				    echo $var['profile_disciplines']['options'][0]['label'];
			    ?>

			    <h3>Industries/Environments</h3>

			    <ul>
			    	<li><?php echo get_user_field('profile_industries', $thisauthor->ID); ?></li>
			    </ul>

			    <h3>Experience</h3>

			    <p><?php echo get_user_field('profile_experience', $thisauthor->ID); ?></p>

			    <h3>Speaking Topics</h3>

			    <p><?php echo get_user_field('profile_speaking_topics', $thisauthor->ID); ?></p>

			    <h3>Prior Speaking Experience/Engagements</h3>

			    <p><?php echo get_user_field('profile_engagements', $thisauthor->ID); ?></p>

			    <h3>Publications</h3>

			    <p><?php echo get_user_field('profile_publications', $thisauthor->ID); ?></p>

			    <h3><a href="<?php echo get_permalink('72'); ?>">Disclosure</a></h3>

			</article>

		</aside><!--end of right-->

	</article><!--end of content-->

</section><!--end of container-->

<?php get_footer(); ?>

I don’t see where I have the function being called multiple times.

Posted: Wednesday Sep 26th, 2012 at 1:16 am #26512
Bruce
Username: Bruce
Staff Member

Hi Danny,

If you have the function called anywhere (like in a mu-plugin) in your WordPress setup, this error will come up. Because this function already exists within the standard s2Member setup (the post you found was for editing the file directly, which is not recommended) you are getting that error. Try just changing your file to this:

<?php get_header(); ?>

<section id="title">

	<article>
		<h2>Become A Member</h2>
	</article>

</section><!--end of title-->

<section id="container">

	<article id="content">

		<aside id="left">

			<?php get_sidebar(); ?>

		</aside><!--end of left-->

		<aside id="right">

			<article>

			    <?php
			    	$thisauthor = get_userdata(intval($author));
			    ?>

			    <h2><?php echo $thisauthor->display_name; ?>, <?php echo get_user_field('profile_certifications', $thisauthor->ID);  ?></h2>

			    <?php if (have_posts()) : ?>

			    <?php endif; ?>

			    <article class="profile-details">

				    <aside class="picture">

				    	<? if(function_exists('get_avatar')) { echo get_avatar($thisauthor->user_email, 96, "" ); } ?>

				    </aside><!--end of .picture-->

				    <ul class="profile-info">
				    	<li><strong><?php echo get_user_field('profile_company', $thisauthor->ID);  ?></strong></li>
				    	<li><?php echo get_user_field('profile_address1', $thisauthor->ID);  ?></li>
				    	<li><?php echo get_user_field('profile_address2', $thisauthor->ID);  ?></li>
				    	<li><?php echo get_user_field('profile_city', $thisauthor->ID);  ?>, <?php echo get_user_field('profile_state', $thisauthor->ID);  ?> <?php echo get_user_field('profile_zip_code', $thisauthor->ID);  ?></li>
				    </ul>

				    <ul class="profile-info">
				    	<li><strong>Phone:</strong> <?php echo get_user_field('profile_phone', $thisauthor->ID);  ?></li>
				    	<li><strong>Fax:</strong> <?php echo get_user_field('profile_fax', $thisauthor->ID);  ?></li>
				    	<li><strong>Email:</strong> <a href="mailto:<? echo $thisauthor->user_email; ?>"><? echo $thisauthor->user_email; ?></a></li>
				    	<li><a href="<?php echo $thisauthor->user_url; ?>" target="_blank"><?php echo $thisauthor->user_url; ?></a></li>
				    </ul>

			    </article><!--end of .profile-details-->

			    <h3>Disciplines/Expertise/Services</h3>

			    <?php
				    function get_s2member_custom_fields_by_id($user_id) {
					    if($user_id === NULL)
						    $user_id = $theUserID;
						    $return = array();
						    $user = get_user_option('s2member_custom_fields', $user_id);
				    foreach ((array)json_decode($GLOBALS&#91;'WS_PLUGIN__'&#93;&#91;'s2member'&#93;&#91;'o'&#93;&#91;'custom_reg_fields'&#93;, true) as $field) {
					    if (isset($user&#91;$field&#91;'id'&#93;&#93;)) {
					    $return&#91;$field&#91;'id'&#93;&#93;&#91;'label'&#93; = $field&#91;'label'&#93;;
				    if (empty($field&#91;'options'&#93;))
				    	$return&#91;$field&#91;'id'&#93;&#93;&#91;'value'&#93; = $user&#91;$field&#91;'id'&#93;&#93;;
				    else {
				    	$field&#91;'options'&#93; = strpos($field&#91;'options'&#93;, "\n") ? explode("\n", $field&#91;'options'&#93;) : (array)$field&#91;'options'&#93;;
				    foreach ($field&#91;'options'&#93; as $option) {
				   	 $option = explode('|', $option);
				   	 $options&#91;$option&#91;0&#93;&#93; = $option&#91;1&#93;;
				    }
				    foreach ((array)$user&#91;$field&#91;'id'&#93;&#93; as $choice)
				    	$return&#91;$field&#91;'id'&#93;&#93;&#91;'options'&#93;&#91;$choice&#93; = $options&#91;$choice&#93;;
				    }
				    	}
				    }
				    return $return;
				    	}
				    $s2_custom_fields = get_s2member_custom_fields_by_id($theUserID);
				    echo $var&#91;'profile_disciplines'&#93;&#91;'options'&#93;&#91;0&#93;&#91;'label'&#93;;
			    ?>

			    <h3>Industries/Environments</h3>

			    <ul>
			    	<li><?php echo get_user_field('profile_industries', $thisauthor->ID); ?></li>
			    </ul>

			    <h3>Experience</h3>

			    <p><?php echo get_user_field('profile_experience', $thisauthor->ID); ?></p>

			    <h3>Speaking Topics</h3>

			    <p><?php echo get_user_field('profile_speaking_topics', $thisauthor->ID); ?></p>

			    <h3>Prior Speaking Experience/Engagements</h3>

			    <p><?php echo get_user_field('profile_engagements', $thisauthor->ID); ?></p>

			    <h3>Publications</h3>

			    <p><?php echo get_user_field('profile_publications', $thisauthor->ID); ?></p>

			    <h3><a href="<?php echo get_permalink('72'); ?>">Disclosure</a></h3>

			</article>

		</aside><!--end of right-->

	</article><!--end of content-->

</section><!--end of container-->

<?php get_footer(); ?>
Posted: Wednesday Sep 26th, 2012 at 9:56 am #26568
Danny Albeck
Username: dalbeck

Bruce,

I feel like its a step in the right direction. There are no errors now, but it doesn’t output any of the results from that field. I just double checked the users profile in WordPress to make sure they have a few options checked off and there are.

Any ideas?

Thank you for all of your help!

Posted: Wednesday Sep 26th, 2012 at 8:00 pm #26631
Bruce
Username: Bruce
Staff Member

Hi Danny,

I’m going to run some tests quickly and create a function for you to do this with.

I believe the fastest way to do this would be just to json_decode() the get_usermeta() value [wp->prefix]s2member_custom_fields and get the user field you’re looking for.

I’ll get back to you shortly.

Posted: Wednesday Sep 26th, 2012 at 9:10 pm #26635
Bruce
Username: Bruce
Staff Member

Hi Danny,

Try using this:

<?php get_header(); ?>

<section id="title">

	<article>
		<h2>Become A Member</h2>
	</article>

</section><!--end of title-->

<section id="container">

	<article id="content">

		<aside id="left">

			<?php get_sidebar(); ?>

		</aside><!--end of left-->

		<aside id="right">

			<article>

			    <?php
			    	$thisauthor = get_userdata(intval($author));
			    ?>

			    <h2><?php echo $thisauthor->display_name; ?>, <?php echo get_user_field('profile_certifications', $thisauthor->ID);  ?></h2>

			    <?php if (have_posts()) : ?>

			    <?php endif; ?>

			    <article class="profile-details">

				    <aside class="picture">

				    	<? if(function_exists('get_avatar')) { echo get_avatar($thisauthor->user_email, 96, "" ); } ?>

				    </aside><!--end of .picture-->

				    <ul class="profile-info">
				    	<li><strong><?php echo get_user_field('profile_company', $thisauthor->ID);  ?></strong></li>
				    	<li><?php echo get_user_field('profile_address1', $thisauthor->ID);  ?></li>
				    	<li><?php echo get_user_field('profile_address2', $thisauthor->ID);  ?></li>
				    	<li><?php echo get_user_field('profile_city', $thisauthor->ID);  ?>, <?php echo get_user_field('profile_state', $thisauthor->ID);  ?> <?php echo get_user_field('profile_zip_code', $thisauthor->ID);  ?></li>
				    </ul>

				    <ul class="profile-info">
				    	<li><strong>Phone:</strong> <?php echo get_user_field('profile_phone', $thisauthor->ID);  ?></li>
				    	<li><strong>Fax:</strong> <?php echo get_user_field('profile_fax', $thisauthor->ID);  ?></li>
				    	<li><strong>Email:</strong> <a href="mailto:<? echo $thisauthor->user_email; ?>"><? echo $thisauthor->user_email; ?></a></li>
				    	<li><a href="<?php echo $thisauthor->user_url; ?>" target="_blank"><?php echo $thisauthor->user_url; ?></a></li>
				    </ul>

			    </article><!--end of .profile-details-->

			    <h3>Disciplines/Expertise/Services</h3>

				<?php
					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;
						}
							$fields = s2hack_get_user_checkboxes('profile_disciplines', $thisauthor->ID);
							var_dump($fields['profile_disciplines']);
			    ?>

			    <h3>Industries/Environments</h3>

			    <ul>
			    	<li><?php echo get_user_field('profile_industries', $thisauthor->ID); ?></li>
			    </ul>

			    <h3>Experience</h3>

			    <p><?php echo get_user_field('profile_experience', $thisauthor->ID); ?></p>

			    <h3>Speaking Topics</h3>

			    <p><?php echo get_user_field('profile_speaking_topics', $thisauthor->ID); ?></p>

			    <h3>Prior Speaking Experience/Engagements</h3>

			    <p><?php echo get_user_field('profile_engagements', $thisauthor->ID); ?></p>

			    <h3>Publications</h3>

			    <p><?php echo get_user_field('profile_publications', $thisauthor->ID); ?></p>

			    <h3><a href="<?php echo get_permalink('72'); ?>">Disclosure</a></h3>

			</article>

		</aside><!--end of right-->

	</article><!--end of content-->

</section><!--end of container-->

<?php get_footer(); ?>

I created the function s2hack_get_user_checkboxes() that accepts an array of fields you’d like returned, and returns an associative array of these fields. It’s a quick and dirty solution but it should work just fine.

As it is set up currently, it is only var_dump()ing the data into your page, you can use the array with a <ul> or something similar to finish this up.

Let me know how it goes.

Viewing 8 replies - 1 through 8 (of 8 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.