Tag Archives: author

This WordPress snippet will let you show a list of posts by a specific user. This is useful if you, for example, want to show a list of posts from a specific user on Custom Post Types, etc.

Instructions
1. Add a Custom Field (read more about WordPress Custom Fields) to the Page or Custom Post Type etc. In that custom field add “user-id” as the name and the numeric value “1” (or whatever the ID of the specific user is) as the Value“.

2. Add the following to your template.php file

<?php $the_user_id = get_post_meta( $post->ID, 'user-id', true); if ($the_user_id) { ?>
     <?php query_posts("author=$the_user_id&posts_per_page=5" );?>
          <ul>
               <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a><br><?php the_excerpt() ?></li>      
               <?php endwhile; else: ?>
          </ul>
          <p><?php _e('No posts by this user-id'); ?></p>
          <?php endif; ?>
     <?php wp_reset_query(); ?>
<?php } else { ?>
     <!-- No value ha been set for the Custom Field "user-id" -->
<?php } ?>

Here is a snippet that will enable you to show a short intro (excerpt) of the WordPress author bio of a post and not the complete bio. With this snippet you can make an excerpt of the author bio, with a link to the author page, where you can see the full bio.

Instructions
Add this code to your functions.php

function author_excerpt (){	                     					
	$word_limit = 20; // Limit the number of words
	$more_txt = 'read more about:'; // The read more text
	$txt_end = '...'; // Display text end 
	$authorName = get_the_author();
	$authorUrl = get_author_posts_url( get_the_author_meta('ID'));
	$authorDescription = explode(" ", get_the_author_meta('description'));
	$displayAuthorPageLink = count($authorDescription) > $word_limit ? $txt_end.' '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>' : '' ;
	$authorDescriptionShort = array_slice($authorDescription, 0, ($word_limit));
	return (implode($authorDescriptionShort, ' ')).$displayAuthorPageLink; 		
}

Add this code to your post template file where you want to show the author bio excerpt.

<?php echo author_excerpt(); ?>

Heres a little tip on how to display a list of WordPress authors with avatars and username.

Instructions
Add this code anywhere in your 'templates' file.

<?php
     $display_admins = false;
     $order_by = 'post_count'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
     $order = 'DESC';
     $role = ''; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
     $avatar_size = 161;
     $hide_empty = false; // hides authors with zero posts
		
     if(!empty($display_admins)) {
          $blogusers = get_users('orderby='.$order_by.'&role='.$role);
     } else {

     $admins = get_users('role=administrator');
     $exclude = array();
     
     foreach($admins as $ad) {
          $exclude[] = $ad->ID;
     }

     $exclude = implode(',', $exclude);
     $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&order='.$order.'&role='.$role);
     }
	
     $authors = array();
     foreach ($blogusers as $bloguser) {
     $user = get_userdata($bloguser->ID);

     if(!empty($hide_empty)) {
          $numposts = count_user_posts($user->ID);
          if($numposts < 1) continue;
          }
          $authors[] = (array) $user;
     }
		
     echo '<ul id="grid-contributors">';
     foreach($authors as $author) {
          $display_name = $author['data']->display_name;
          $avatar = get_avatar($author['ID'], $avatar_size);
          $author_profile_url = get_author_posts_url($author['ID']);
			
          echo '<li class="single-item">';
          echo '<div class="author-gravatar"><a href="', $author_profile_url, '">', $avatar , '</a></div>';
          echo '<div class="author-name"><a href="', $author_profile_url, '" class="contributor-link">', $display_name, '</a></div>';
          echo '</li>';
          }
     echo '</ul>';
?>

Heres a little trick on how to remove the WordPress author prefix from the author slug.
To clarify, this snippet will help you turn: ‘http://domain.com/author/peter/‘ into: ‘http://domain.com/peter/‘.

Since we want all links across the site that uses the WordPress author paradigm to auto-update without keeping the original links and then redirecting to a new one, We do not want to use any redirects of any kind but create an actual function in the WordPress functions.php file. We need to add WP rewrite rules to match the names of each of our users in the desired form.

  • The first part of markup hooks into the author_rewrite_rules filter and replaces the author rewrite rules. This retrieves all the usernames and adds a rewrite rule specifically for each user (the markup below won’t handle feeds, so look at the WP No Category Base source if you need that).
  • The second part of markup is a function which hooks into the author_link filter and removes the ‘author’ base from the returned URL.

Instructions
Add this code to your functions.php file.
*Remember to flush your rewrite rules after implementing this snippet, by going into “permalinks” within settings and then click “save changes”.

// The first part //
add_filter('author_rewrite_rules', 'no_author_base_rewrite_rules');
function no_author_base_rewrite_rules($author_rewrite) { 
    global $wpdb;
    $author_rewrite = array();
    $authors = $wpdb->get_results("SELECT user_nicename AS nicename from $wpdb->users");    
    foreach($authors as $author) {
        $author_rewrite["({$author->nicename})/page/?([0-9]+)/?$"] = 'index.php?author_name=$matches[1]&paged=$matches[2]';
        $author_rewrite["({$author->nicename})/?$"] = 'index.php?author_name=$matches[1]';
    }   
    return $author_rewrite;
}

// The second part //
add_filter('author_link', 'no_author_base', 1000, 2);
function no_author_base($link, $author_id) {
    $link_base = trailingslashit(get_option('home'));
    $link = preg_replace("|^{$link_base}author/|", '', $link);
    return $link_base . $link;
}

To prevent collision with users creating usernames similar to pages or categories – you can either create a function or use a plugin that restricts usernames from being created – How to restrict usernames in WordPress.

This snippet will allow you to change the default WordPress author URL base from ie: “mysite.com/author/name”, to ie.”mysite.com/profile/name”. In this example we are changing the slug to “profile”, but you can change this to anything that you would like.

Instructions
1. Add this code to your functions.php

<?php 
add_action('init', 'wp_snippet_author_base');
function wp_snippet_author_base() {
    global $wp_rewrite;
    $author_slug = 'profiles'; // the new slug name
    $wp_rewrite->author_base = $author_slug;
}
?>

2. The permalink settings have to be updated after adding the above snippet to your functions.php. This is simply done by navigating to: settings > permalink and click “Save Changes”.

Related plugins:
https://wordpress.org/plugins/edit-author-slug/

Put this in the theme post-/page file to make an “Edit Page” or “Edit Post” link in your template file – that takes you into the admin page-/post edit mode. This link will only be visible when the viewer is logged in and has appropriate permissions to edit that page (administrator or author).
<?php edit_post_link(__('Edit This')); ?>

Related links:
https://codex.wordpress.org/Function_Reference/edit_post_link