[PHP][Wordpress][Custom Profile Fields] Checkbox Use

I have used the following code and converted it to give me my desired extra fields. I have tried hundreds of combinations, but i just can't figure this out...

<?php
/**
 * Plugin Name: YLDist Profile Checkboxes
 * Plugin URI:
 * Description: Profiles
 * Author: JPowersFreelancing
 * Author URI:
 * Version: 1.0
 */
$lingo = array('ob' => 'Order Button', 'vb' => 'Video Button', 'er' => 'Essential Rewards Button', 'op' => 'Opportunities Button');

function my_user_field( $user ) {
    $gender = get_the_author_meta( 'dealing', $user->ID);
    $number = esc_attr( get_the_author_meta( 'number', $user->ID ) );
	$pnumber = esc_attr( get_the_author_meta( 'pnumber', $user->ID ) );
?>
    <h3><?php _e('Customize Your Website and Contact Information'); ?></h3>
    <table class="form-table">
        <tr>
            <th>
                <label for="Dealing Type"><?php _e('Gender'); ?>
            </label></th>
            <td>
            <label><input type="radio" name="dealing" <?php if ($gender == 'Male' ) { ?>checked="checked"<?php }?> value="Male">Male<br /></label>
            <label><input type="radio" name="dealing" <?php if ($gender == 'Female' ) { ?>checked="checked"<?php }?> value="Female">Female<br /></label>

            </td>
        </tr>
        <tr>
            <th>
                <label for="number"><?php _e('Young Living Membership Number*'); ?>
            </label></th>
            <td>
              <span class="description"><?php _e('Insert your Young Living Membership Number'); ?></span><br>
                <input type="text" name="number" id="number" value="<?php echo $number; ?>" class="regular-text" /><br />
            </td>
        </tr>
		<tr>
            <th>
                <label for="pnumber"><?php _e('Phone Number'); ?>
            </label></th>
            <td>
              <span class="description"><?php _e('Your Phone Number (Optional)'); ?></span><br>
                <input type="text" name="pnumber" id="pnumber" value="<?php echo $pnumber; ?>" class="regular-text" /><br />
            </td>
        </tr>
        <tr>
            <th>
                <?php _e('Website Options'); ?>
            </th>
            <td><?php
                global $lingo;
                foreach($lingo as $key => $value) {
                    $code = 'opt_'.$key;
                    $lang = get_the_author_meta( $code, $user->ID);
                     ?>
                    <label><input type="checkbox" name="<?php echo $code; ?>" <?php if ($lang == 'yes' ) { ?>checked="checked"<?php }?> value="yes" /> <?php echo $value; ?></label><br />
                <?php }
            ?>
            </td>
        </tr>
    </table>
<?php
}

function my_save_custom_user_profile_fields( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) )
        return FALSE;

    update_usermeta( $user_id, 'dealing', $_POST['dealing'] );
    update_usermeta( $user_id, 'number', $_POST['number'] );
	update_usermeta( $user_id, 'pnumber', $_POST['pnumber'] );

    global $lingo;
    foreach($lingo as $key => $value) {
        $code = "opt_".$key;
        update_usermeta( $user_id, $code, $_POST[$code] );
    }
}

add_action( 'show_user_profile', 'my_user_field' );
add_action( 'edit_user_profile', 'my_user_field' );
add_action( 'personal_options_update', 'my_save_custom_user_profile_fields' );
add_action( 'edit_user_profile_update', 'my_save_custom_user_profile_fields' );

?>

I need to use the checkboxes from the custom fields to tell an element in my wp-header.php child website to turn off or on. How do I call these custom checkboxes to display an echo or not to display, depending on the users input?

  • Michelle Shull

    Hey, Jeremy!

    I can get you the help you need here, but do you mind sharing a few more details I can take to that help?

    1. Where should this information display? (You mention header.php, but that can mean a lot of places.)
    2. What information do you want to display/not display?
    3. What are the criteria for displaying/not displaying? (Is it a simple true/false or something more complex?)

    I'm reading through your template code here, and it's not entirely clear to me what the desired outcome of your plan is, knowing a better road map can help our code gurus point you in the right path.

    Thanks, Jeremy!

  • Jeremy

    Hey Michelle,

    1) Anywhere that I would want to output it, using IF { to call the checkbox.
    2) The information will be contained in the IF {.
    3) TRUE/FALSE, If checkbox = 'yes' would be fine.

    That code is the code I have been using to save the custom profile fields in my custom function to add profile fields, the fields save and everything fine, but I can't figure out how to call the array into an IF { statement that I can then give more actions to.

  • Jeremy

    The information is saving in

    global $lingo;
        foreach($lingo as $key => $value) {
            $code = "opt_".$key;
            update_usermeta( $user_id, $code, $_POST[$code] );
        }

    But I can't figure out how to actually use that saved data somewhere else. I don't have to use this code either, just anything that I can use to create custom checkboxes in the user's profile, that are turned on/off, and can be assigned to anywhere on the website through an IF { checkbox is checked, then "blah".

    I hope this makes sense.

    • Jeremy

      Thank you. I could not get that to work, but I've been trying to figure this out for so long, my brain was a little melted. I don't know if this is a hack, but my limited PHP knowledge made me experiment and I came up with this solution, for anyone who comes into this issue in the future.

      First, the code in my options area looks like this:

      <?php
      /**
       * Plugin Name: YLDist Profile Checkboxes
       * Plugin URI:
       * Description: Profiles
       * Author: JPowersFreelancing
       * Author URI:
       * Version: 1.0
       */
      
      function my_user_field( $user ) {
          $sponsor = esc_attr( get_the_author_meta( 'sponsor', $user->ID ) );
      	$number = esc_attr( get_the_author_meta( 'number', $user->ID ) );
      	$orderbutton = get_the_author_meta( 'orderbutton', $user->ID);
      ?>
          <h3><?php _e('Customize Your Website and Contact Information'); ?></h3>
          <table class="form-table">
              <tr>
                  <th>
                      <label for="sponsor"><?php _e('Young Living Membership Number*'); ?>
                  </label></th>
                  <td>
                    <span class="description"><?php _e('Insert your Young Living Membership Number'); ?></span><br>
                      <input type="text" name="sponsor" id="sponsor" value="<?php echo $sponsor; ?>" class="regular-text" /><br />
                  </td>
              </tr>
      		<tr>
                  <th>
                      <label for="number"><?php _e('Phone Number'); ?>
                  </label></th>
                  <td>
                    <span class="description"><?php _e('Your Phone Number (Optional)'); ?></span><br>
                      <input type="text" name="number" id="number" value="<?php echo $number; ?>" class="regular-text" /><br />
                  </td>
              </tr>
              <tr>
                  <th>
                      <?php _e('Website Options'); ?>
                  </th>
                  <td><input type="checkbox" name="orderbutton" <?php if ($orderbutton == 'yes' ) { ?>checked="checked"<?php }?> value="yes" /> Order Button<br />
                  </td>
              </tr>
          </table>
      <?php
      }
      
      function my_save_custom_user_profile_fields( $user_id ) {
          if ( !current_user_can( 'edit_user', $user_id ) )
              return FALSE;
      
          update_user_meta( $user_id, 'sponsor', $_POST['sponsor'] );
      	update_user_meta( $user_id, 'number', $_POST['number'] );
      	update_user_meta( $user_id, 'orderbutton', $_POST['orderbutton'] );
      }
      
      add_action( 'show_user_profile', 'my_user_field' );
      add_action( 'edit_user_profile', 'my_user_field' );
      add_action( 'personal_options_update', 'my_save_custom_user_profile_fields' );
      add_action( 'edit_user_profile_update', 'my_save_custom_user_profile_fields' );
      
      ?>

      Using the code:
      <td><input type="checkbox" name="orderbutton" <?php if ($orderbutton == 'yes' ) { ?>checked="checked"<?php }?> value="yes" /> Order Button
      for the checklist option that the user inputs.

      Then, I save it using this:
      update_user_meta( $user_id, 'orderbutton', $_POST['orderbutton'] );

      Then, on the front end, I call to it using this:

      if (strpos($orderbutton,'yes') !== false) {
              $ob = "<a target='_blank' href='$user_info->googleplus'><img src='https://www.yldist.com/ms/images/social/social-icons-circle-gp.png' width='16px'></a>";
          } else { 
      
          }

      The $ob code is arbitrary, you can place anything there. then call it into your echo, and bam.

      <div class="meta">
      	<?php
      	$user_id_from_email = get_user_id_from_string( get_blog_option($thisblog, 'admin_email'));
      	$user_info = get_userdata($user_id_from_email);
      	$member = $user_info->sponsor;
      	$sponid = 'US&sponsorid=';
      	$enroid = '&enrollerid=';
      
      	if (strpos($orderbutton,'yes') !== false) {
              $ob = "<a target='_blank' href='$user_info->googleplus'><img src='https://www.yldist.com/ms/images/social/social-icons-circle-gp.png' width='16px'></a>";
          } else { 
      
          }
      
      	echo "<a href='https://youtu.be/HrgclTyqKMo?rel=0&autoplay=1'>Watch the Video</a>" . $ob;
      	//echo "<a target='_blank' href='https://www.youngliving.com/signup/?site=;".$sponid."".$member."".$enroid."".$member."'>Order Now</a>" . "<br />";
      	?>
      	</div>

      It works, I don't know if it's a hack and crap job, but it works and I want to punch things I'm so happy. Thank you for your assistance and reference to the user_meta.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.