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

User meta boxes not working WordPress

  • SOLVED

Hello,

I am following this tutorial from here on how to create user meta boxes.

[[LINK href="http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields"]]http://justintadlock.com/archives/2009/09/10/adding-and-using-custom-user-profile-fields[[/LINK]]


This is my exact code below but all I seem to get is internal server error everytime I add them.

I am using this on a multisite. Not sure if that is causing the problem

Need urgent help please. Thanks

// USER META
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="twitter">Publication / Company / Agency</label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'company_publication', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you publication name</span>
</td>
</tr>
<tr>
<th><label for="twitter">Job Title</label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you job title</span>
</td>
</tr>
<tr>
<th><label for="twitter">Publication Territory</label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'publication_territory', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you publication territory</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, 'company_publication', $_POST['company_publication'] );
update_usermeta( $user_id, 'job_title', $_POST['job_title'] );
update_usermeta( $user_id, 'publication_territory', $_POST['publication_territory'] );

}



As I understand, the areas where I have put company_publication, job_title and publication_territory are ment to be my meta keys, which they are.

Thanks in a advance.

Answers (3)

2013-04-08

Francisco Javier Carazo Gil answers:

Josh,

You have the same ids in every input: <input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br />


Francisco Javier Carazo Gil comments:

Change names and ids to the one correct in each case: company_publication, job_title and publication_territory.


Josh Cranwell comments:

Hello thanks but I've dropped this code into my functions block and it causes server error...


// LOGIN HEAD
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="company_publication">Publication / Company / Agency</label></th>
<td>
<input type="text" name="company_publication" id="company_publication" value="<?php echo esc_attr( get_the_author_meta( 'company_publication', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you publication name</span>
</td>
</tr>
<tr>
<th><label for="job_title">Job Title</label></th>
<td>
<input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you job title</span>
</td>
</tr>
<tr>
<th><label for="publication_territory">Publication Territory</label></th>
<td>
<input type="text" name="publication_territory" id="publication_territory" value="<?php echo esc_attr( get_the_author_meta( 'publication_territory', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you publication territory</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, 'company_publication', $_POST['company_publication'] );
update_usermeta( $user_id, 'job_title', $_POST['job_title'] );
update_usermeta( $user_id, 'publication_territory', $_POST['publication_territory'] );
}


Francisco Javier Carazo Gil comments:

Tell us server error, set WP_DEBUG to true to be able to debug it.


Josh Cranwell comments:

Fatal error: Cannot redeclare my_show_extra_profile_fields() (previously declared in /home/sites/honda-mcpr.com/www/wp-content/plugins/mailjet-for-wordpress/wp-mailjet.php:124) in /home/sites/honda-mcpr.com/www/wp-content/themes/honda-mc-pressrides/functions.php on line 649

Ahh. Seems to be mail jet :/ any way round this?


Francisco Javier Carazo Gil comments:

No problem, change this function name: my_show_extra_profile_fields() to any other, for example: josh_extra_profile_fields().

Also change add_action().


Josh Cranwell comments:

Thank you - yeah I realised that mistake as I sent that. Thanks for pointing me in the right direction.


Francisco Javier Carazo Gil comments:

Perfect, if need something more, tell me.

2013-04-08

Daniel Yoen answers:

please try this(tested by me, and works :-)) :

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="twitter">Publication / Company / Agency</label></th>

<td>

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

<span class="description">Please enter you publication name</span>

</td>

</tr>

<tr>

<th><label for="twitter">Job Title</label></th>

<td>

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

<span class="description">Please enter you job title</span>

</td>

</tr>

<tr>

<th><label for="twitter">Publication Territory</label></th>

<td>

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

<span class="description">Please enter you publication territory</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, 'company_publication', $_POST['company_publication'] );

update_usermeta( $user_id, 'job_title', $_POST['job_title'] );

update_usermeta( $user_id, 'publication_territory', $_POST['publication_territory'] );



}


Josh Cranwell comments:

Hello thanks but I've dropped this code into my functions block and it causes server error...


// LOGIN HEAD
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="company_publication">Publication / Company / Agency</label></th>
<td>
<input type="text" name="company_publication" id="company_publication" value="<?php echo esc_attr( get_the_author_meta( 'company_publication', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you publication name</span>
</td>
</tr>
<tr>
<th><label for="job_title">Job Title</label></th>
<td>
<input type="text" name="job_title" id="job_title" value="<?php echo esc_attr( get_the_author_meta( 'job_title', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you job title</span>
</td>
</tr>
<tr>
<th><label for="publication_territory">Publication Territory</label></th>
<td>
<input type="text" name="publication_territory" id="publication_territory" value="<?php echo esc_attr( get_the_author_meta( 'publication_territory', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description">Please enter you publication territory</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, 'company_publication', $_POST['company_publication'] );
update_usermeta( $user_id, 'job_title', $_POST['job_title'] );
update_usermeta( $user_id, 'publication_territory', $_POST['publication_territory'] );
}


Josh Cranwell comments:

Francisco solved my problem, thank you though for helping.

2013-04-08

idt answers:

Hi Josh! What is the line of code in your functions.php just above where you inserted that code?


Josh Cranwell comments:

Francisco just solved my problem, thank you.