Skip to content

Instantly share code, notes, and snippets.

@vutruso
Created November 14, 2021 12:39
Show Gist options
  • Save vutruso/161df2d4b66d288ef4b20035c2e50f29 to your computer and use it in GitHub Desktop.
Save vutruso/161df2d4b66d288ef4b20035c2e50f29 to your computer and use it in GitHub Desktop.
Custom url author frontend in WordPress
/* Add Custom User Profile Fields - Url author
*===============================================================*/
// Get user meta on frontend with: get_user_meta( $user_id, "field_id", true );
function vts_url_author( $user ) {
$output = ''; $output .= '<h2>'.__("Author Url", "vutruso").'</h2>';
$output .= '<table class="form-table">';
$output .= '<tr>';
$output .= '<th><label for="vts_author_url">'.__("Author url", "vutruso").'</label></th>';
$output .= '<td>';
$output .= '<input placeholder="Author url" type="url" name="vts_author_url" id="vts_author_url" value="'.esc_attr( get_user_meta( $user->ID, 'vts_author_url', true ) ).'" class="regular-text" /><br />';
$output .= '</td>';
$output .= '</tr>';
$output .= '</table>';
echo $output;
}
add_action( 'show_user_profile', 'vts_url_author' );
add_action( 'edit_user_profile', 'vts_url_author' );
// Save Custom User Profile Fields
function vts_url_author_save( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) {
return false;
}
update_user_meta( $user_id, 'vts_author_url', $_POST['vts_author_url'] );
}
add_action( 'personal_options_update', 'vts_url_author_save' );
add_action( 'edit_user_profile_update', 'vts_url_author_save' );
//custom url author frontend
add_filter( 'author_link', 'new_author_link', 10, 1 );
function new_author_link( $link ) {
global $current_user;
$info = wp_get_current_user();
$link = get_the_author_meta('vts_author_url');
if($link!==""){
return $link;
}else{
$link = '#';
return $link;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment