Ask your WordPress questions! Pay money and get answers fast! (more info)

Hide Or Show Custom Profile Fields (Backend) Based On Role WordPress

  • SOLVED

I have added the following code to my functions.php file to add one additional custom field to the backend profile and this works fine...

I intend to add a few more fields however for simplicity I am showing one field in the example below;

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields($user) {?>

<h3>Extra Profile Information</h3>
<table class="form-table">

<tr>
<th><label for="age">Age</label></th>

<td>
<input type="text" name="age" id="age" value="<?php echo esc_attr
( get_the_author_meta( 'age', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your age.</span>
</td>
</tr>
</table>

<?php }

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;

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


I would like to restrict the view of this field based on the users role... Is it possible to use or nest the following argument <?php if (current_user_can(‘editor’)) { ?> to hide or show the custom field based on the users profile???

Ideally, I would like to say;

if user role is equal to role1 show these custom fields
else if role is equal to role2 show these custom fields

If the role is not equal to either role1 or role2 then do not show any custom profile fields...

If this is possible how is the statement structured in the function.php file???

For clarification, I am referring to custom fields that are displayed in the backend user profile area...

Any help would be greatly appreciated...

Thanks,
Sean.

Answers (3)

2013-12-29

Just Me answers:

<?php
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );

add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );



function my_show_extra_profile_fields($user) {



if ($user->has_cap('subscriber'))



{ ?>





<h3>Extra Profile Information</h3>

<table class="form-table">

<tr>

<th><label for="age">Age</label></th>

<td>

<input type="text" name="age" id="age" value="<?php echo esc_attr ( get_the_author_meta( 'age', $user->ID ) ); ?>" class="regular-text" /><br />

<span class="description">Please enter your age.</span>

</td>

</tr>



<tr>

<th><label for="dropdown">Years Of Experience</label></th>

<td>

<?php

//get dropdown saved value

$selected = get_the_author_meta( 'exp_years', $user->ID );

?>

<select name="exp_years" id="exp_years" style="width:100%;">

<option value="value1" <?php echo ($selected == "value1")? 'selected="selected"' : '' ?>>Less than 1 year</option>

<option value="value2" <?php echo ($selected == "value2")? 'selected="selected"' : '' ?>>1-3 years</option>

<option value="value3" <?php echo ($selected == "value3")? 'selected="selected"' : '' ?>>4-7 years</option>

<option value="value4" <?php echo ($selected == "value4")? 'selected="selected"' : '' ?>>8-15 years</option>

<option value="value5" <?php echo ($selected == "value5")? 'selected="selected"' : '' ?>>15+ years</option>

</select>





<span class="description">Years of experience.</span>

</td>

</tr>





</table>



}

}


make sure you have no white/empty lines at the beginning or at the end of the functions.php file


Sean Gartlan comments:

Thanks for the response... :) That has fixed the problem I was having with escaping out of the php parsing... However, I am still getting a white screen...

I have mucked-up somewhere... I'm linking my functions.php file at https://dl.dropboxusercontent.com/u/16082984/functions.php for those willing to have a look...

The relevant code starts at line 507 and goes to line 564... If I remove this section no white screens are encountered...

Many Thanks,

Sean.

2013-12-30

Hariprasad Vijayan answers:

Hello,

Try this
add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields($user) {?>
<h3>Extra Profile Information</h3>
<?php if($user->has_cap('administrator')) { // Custom field only display to administrator?>
<table class="form-table">
<tr>
<th><label for="age">Age</label></th>
<td><input type="text" name="age" id="age" value="<?php echo esc_attr(get_the_author_meta( 'age', $user->ID ) ); ?>" class="regular-text" />
<br />
<span class="description">Please enter your age.</span> </td>
</tr>
</table>
<?php } if($user->has_cap('subscriber')) {// Custom field only display to subscriber ?>
<table class="form-table">
<tr>
<th><label for="age">Phone</label></th>
<td><input type="text" name="phone" id="phone" value="<?php echo esc_attr(get_the_author_meta( 'phone', $user->ID ) ); ?>" class="regular-text" />
<br />
<span class="description">Please enter your phone.</span> </td>
</tr>
</table>
<?php } }
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;
if(!empty( $_POST['age'])){
update_usermeta( $user_id, 'age', $_POST['age'] );
}
if(!empty( $_POST['phone'])){
update_usermeta( $user_id, 'phone', $_POST['phone'] );
}
}


Sean Gartlan comments:

Hariprasad,

Thanks you have nailed it... Many thanks...

2013-12-29

Balanean Corneliu answers:

Hello Sean,

You can use the WP_User class and the has_cap($role) method. The show_user_profile action passes a WP_User object as a parameter to the called function.
http://codex.wordpress.org/Class_Reference/WP_User#has_cap.28.24cap.29

add_action('show_user_profile', 'my_add_extra_profile_fields');
function my_add_extra_profile_fields($user) {
if ($user->has_cap('subscriber'))
{
//Code here
}
}


Sean Gartlan comments:

Thanks for the reply Balanean...

When I try to implement your suggestion I get a white screen... I think the problem is at the beginning of the html elements, ie. <h3>... My text editor shows the html as red being in error when I slot your suggestion into the appropriate area...

My coding skills are fairly weak at best so I think I might be having a problem trying to escape the php phasing and begin the html elements... What I need is help with structuring the php and html together in one interpretable statement...

See my attempt below which white screens;

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields($user) {

if ($user->has_cap('subscriber'))

{


<h3>Extra Profile Information</h3>
<table class="form-table">
<tr>
<th><label for="age">Age</label></th>
<td>
<input type="text" name="age" id="age" value="<?php echo esc_attr ( get_the_author_meta( 'age', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter your age.</span>
</td>
</tr>

<tr>
<th><label for="dropdown">Years Of Experience</label></th>
<td>
<?php
//get dropdown saved value
$selected = get_the_author_meta( 'exp_years', $user->ID );
?>
<select name="exp_years" id="exp_years" style="width:100%;">
<option value="value1" <?php echo ($selected == "value1")? 'selected="selected"' : '' ?>>Less than 1 year</option>
<option value="value2" <?php echo ($selected == "value2")? 'selected="selected"' : '' ?>>1-3 years</option>
<option value="value3" <?php echo ($selected == "value3")? 'selected="selected"' : '' ?>>4-7 years</option>
<option value="value4" <?php echo ($selected == "value4")? 'selected="selected"' : '' ?>>8-15 years</option>
<option value="value5" <?php echo ($selected == "value5")? 'selected="selected"' : '' ?>>15+ years</option>
</select>


<span class="description">Years of experience.</span>
</td>
</tr>


</table>

}
}