Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Created February 26, 2020 07:46
Show Gist options
  • Select an option

  • Save JarrydLong/b092b5d87b9ceaa6d224cfebd8289c1a to your computer and use it in GitHub Desktop.

Select an option

Save JarrydLong/b092b5d87b9ceaa6d224cfebd8289c1a to your computer and use it in GitHub Desktop.
<?php
/**
* Render the extra profile fields on the backend
*/
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="external_username"><?php _e("External Username"); ?></label></th>
<td>
<input type="text" name="external_username" id="external_username" value="<?php echo esc_attr( get_the_author_meta( 'external_username', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
<tr>
<th><label for="external_password"><?php _e("External Password"); ?></label></th>
<td>
<input type="text" name="external_password" id="external_password" value="<?php echo esc_attr( get_the_author_meta( 'external_password', $user->ID ) ); ?>" class="regular-text" /><br />
</td>
</tr>
</table>
<?php }
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
/**
* Save custom profile field values
*/
function save_extra_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'external_username', $_POST['external_username'] );
update_user_meta( $user_id, 'external_password', $_POST['external_password'] );
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
/**
* Shortcode to render these fields on a specific page
*/
function private_profile_fields_shortcode( $atts ){
ob_start();
if( is_user_logged_in() ){
$user_id = get_current_user_id();
?>
<table class="form-table">
<tr>
<th><label for="external_username"><?php _e("External Username"); ?></label></th>
<td>
<?php echo esc_attr( get_the_author_meta( 'external_username', $user_id ) ); ?>
</td>
</tr>
<tr>
<th><label for="external_password"><?php _e("External Password"); ?></label></th>
<td>
<?php echo esc_attr( get_the_author_meta( 'external_password', $user_id ) ); ?>
</td>
</tr>
</table>
<?php
}
$temp_content = ob_get_contents();
ob_end_clean();
return $temp_content;
}
add_shortcode( 'private_profile_fields', 'private_profile_fields_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment