WordPress.org

Support

Support » Plugins and Hacks » Hacks » Having issues adding custom profile fields

Having issues adding custom profile fields

  • Hi guys,

    I’m trying to write a plugin but all the functions I used to use are deprecated! Could someone have a look over the below and let me know where I’m going wrong?

    The basic objective is to add a user meta value named “showcase_enabled_bool”. When users view the edit profile page, they can either read that value (general user) or change that value (admin only). The idea is that an admin can enable any user to be included a showcase, but the users themselves can only look to see if they’ve been included.

    When I submit the form, it looks as though no data is stored – but I’m having real trouble debugging this, so that might not be the issue. Any tips would be hugely appreciated.

    add_action('personal_options', 'showcase_options');
    
    function showcase_options() {
    $currentStatus = get_user_meta($user_id, 'showcase_enabled_bool', true);
    if ($currentStatus == 1) {
    $currentStatus = "enabled";
    } else {
    $currentStatus = "disabled";
    }
    if ( !IS_PROFILE_PAGE && !is_network_admin() ) :
    // Admin sees this
    ?>
    <h3>Showcase Settings</h3>
    
    <table class="form-table">
    <tr><th><label for="showcase-enabled-bool">Enabled for showcase?</label></th>
    
    <td>
    <input type="radio" name="showcase-enabled-bool" value="1"<? if ($currentStatus == "enabled") { echo " checked"; } ?>> Yes  
    <input type="radio" name="showcase-enabled-bool" value="0"<? if ($currentStatus == "disabled") { echo " checked"; } ?>> No<br>
    
    </td></tr>
    
    </table>
    <?
    else:
    // User sees this
    ?>
    <h3>Showcase Settings</h3>
    
    <table class="form-table">
    
    <tr><th><label>Enabled for showcase?</label></th>
    
    <td>
    Your profile is currently <b><? echo $currentStatus; ?></b> for showcase listing. Contact an administrator to change this setting.
    </td></tr>
    
    </table>
    <?
    endif;
    }
    
    add_action('personal_options_update', 'showcase_save_options');
    add_action( 'edit_user_profile_update', 'showcase_save_optionss' );
    
    function showcase_save_options( $user_id ) {
    	if ($_POST['showcase-enabled-bool'] == 1) {
    	$showcase_input = 1;
    	} else {
    	$showcase_input = 0;
    	}
        add_user_meta( $user_id, 'showcase_enabled_bool', $showcase_input, true);
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • Take a look at this tutorial

    Adding and using custom user profile fields

    Hi guys,

    Thanks for the replies. Working now, here’s the amended code for anyone who stumbles upon this in the future:

    <?php
    /*
    Plugin Name: Showcase Controller
    Plugin URI: http://frontiermedia.net.au
    Description: Allows control of the showcase page.
    Version: 0.1
    Author: Sam Mclean
    Author URI: http://frontiermedia.net.au
    License: GPL2
    */
    
    add_action( 'personal_options', 'showcase_options' );
    
    function showcase_options ( $user ) {
    
    $currentStatus = get_the_author_meta( 'showcase_enabled_bool', $user->ID );
    if ($currentStatus == 1) {
    $currentStatus = "enabled";
    } else {
    $currentStatus = "disabled";
    }
    if ( !IS_PROFILE_PAGE && !is_network_admin() ) :
    ?>
    <h3>Showcase Settings</h3>
    
    <table class="form-table">
    <tr><th><label for="showcase-enabled-bool">Enabled for showcase?</label></th>
    
    <td>
    <input type="radio" name="showcase-enabled-bool" value="1"<? if ($currentStatus == "enabled") { echo " checked"; } ?>> Yes  
    <input type="radio" name="showcase-enabled-bool" value="0"<? if ($currentStatus == "disabled") { echo " checked"; } ?>> No<br>
    
    </td></tr>
    
    </table>
    <?
    else:
    ?>
    <h3>Showcase Settings</h3>
    
    <table class="form-table">
    
    <tr><th><label>Enabled for showcase?</label></th>
    
    <td>
    Your profile is currently <b><? echo $currentStatus; ?></b> for showcase listing. Contact an administrator to change this setting.
    </td></tr>
    
    </table>
    <?
    endif;
    
    }
    
    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    
    	if ( !current_user_can( 'edit_user', $user_id ) )
    		return false;
    
    	/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
    
    	if ($_POST['showcase-enabled-bool'] == 1) {
    	$showcase_input = 1;
    	} else {
    	$showcase_input = 0;
    	}
    	update_usermeta( $user_id, 'showcase_enabled_bool', $showcase_input );
    }
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Having issues adding custom profile fields’ is closed to new replies.
Skip to toolbar