Enhance your WordPress site with a customized user page that highlights post contributors, thanks to the EasyCodingWithAI – Custom User Page addon. Follow these steps to install, configure, and style the addon.
Example Prompt:
Create a WordPress plugin that overrides the default user page with a custom template, tracks contributors who edit posts, and stores them in a custom database table. Add a shortcode to display the main author and contributors, show contributors in the admin post list, and include a meta box in the post editor for contributors.
Features of the Addon
- Replaces the default WordPress user page with a custom template.
- Tracks and stores contributors in a custom database table.
- Adds a column in the admin posts list to display contributors.
- Provides a shortcode to display authors and contributors.
Step 1: Create the Addon Folder
- Access your WordPress installation via FTP or a file manager.
- Navigate to the wp-content/plugins/ directory.
- Create a new folder named easycoding-user-enhancements.
Step 2: Add the PHP Files
- Inside the
easycoding-user-enhancements
folder, create the following files:easycoding-user-enhancements.php
template-author.php
- Paste the following code into each file:
easycoding-user-enhancements.php
<?php
/*
Plugin Name: EasyCodingWithAI - Custom User Page
Description: Overrides the default WordPress user page with a custom template, flags users who edit posts as contributors, and adds a shortcode to display authors and contributors. Contributors are now stored in a custom database table.
Version: 1.5
Author: EasyCodingWithAI
*/
// Create a custom table on plugin activation
function easycoding_create_contributors_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'post_contributors';
// Check if table exists
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
// Create the table if it does not exist
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id bigint(20) UNSIGNED NOT NULL,
user_id bigint(20) UNSIGNED NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY post_user (post_id, user_id)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
register_activation_hook(__FILE__, 'easycoding_create_contributors_table');
// Redirect author page to custom template
add_action('template_redirect', 'easycoding_custom_user_page_redirect');
function easycoding_custom_user_page_redirect() {
if (is_author()) {
include(plugin_dir_path(__FILE__) . 'templates/template-author.php');
exit();
}
}
// Enqueue CSS for the custom author page
function easycoding_custom_user_page_styles() {
if (is_author()) {
wp_enqueue_style('easycoding-author-page-style', plugin_dir_url(__FILE__) . 'assets/css/easycoding-user-enhancements.css');
}
}
add_action('wp_enqueue_scripts', 'easycoding_custom_user_page_styles');
// Mark users who edit posts as contributors and store in the custom table
add_action('save_post', 'easycoding_mark_contributor_on_edit', 10, 3);
function easycoding_mark_contributor_on_edit($post_id, $post, $update) {
// Don't run on autosave or if this is a revision
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if ($post->post_type == 'revision') return;
// Get the user who edited the post
$user_id = get_current_user_id();
if ($user_id > 0) {
global $wpdb;
$table_name = $wpdb->prefix . 'post_contributors';
// Check if the user is already a contributor for this post
$existing_contributor = $wpdb->get_var($wpdb->prepare(
"SELECT user_id FROM $table_name WHERE post_id = %d AND user_id = %d",
$post_id,
$user_id
));
if (!$existing_contributor) {
// Add user as a contributor
$wpdb->insert(
$table_name,
array(
'post_id' => $post_id,
'user_id' => $user_id
)
);
}
}
}
// Add "Contributors" column to the posts list in admin
add_filter('manage_posts_columns', 'easycoding_add_contributors_column');
function easycoding_add_contributors_column($columns) {
$columns['contributors'] = 'Contributors';
return $columns;
}
// Display the contributors in the new column
add_action('manage_posts_custom_column', 'easycoding_show_contributors_in_column', 10, 2);
function easycoding_show_contributors_in_column($column_name, $post_id) {
if ($column_name == 'contributors') {
global $wpdb;
$table_name = $wpdb->prefix . 'post_contributors';
// Get all contributors for the post
$contributors = $wpdb->get_results($wpdb->prepare(
"SELECT user_id FROM $table_name WHERE post_id = %d",
$post_id
));
if ($contributors) {
// Get the display names of contributors
$main_author_id = get_post_field('post_author', $post_id);
$contributors = array_map(function($contributor) {
$user = get_user_by('id', $contributor->user_id);
return $user ? $user->display_name : 'Unknown Contributor';
}, $contributors);
// Exclude the main author
$contributors = array_diff($contributors, [get_user_by('id', $main_author_id)->display_name]);
echo implode(', ', $contributors);
} else {
echo 'No contributors';
}
}
}
// Shortcode to display the main author and additional contributors
add_shortcode('contributors_list', 'easycoding_contributors_list_shortcode');
function easycoding_contributors_list_shortcode($atts) {
global $post;
$post_id = $post->ID;
$main_author_id = $post->post_author;
$main_author = get_user_by('id', $main_author_id);
// Get contributors from the custom table
global $wpdb;
$table_name = $wpdb->prefix . 'post_contributors';
$contributors = $wpdb->get_results($wpdb->prepare(
"SELECT user_id FROM $table_name WHERE post_id = %d",
$post_id
));
$output = '';
if ($main_author) {
// Make the main author's name a clickable link to their author page
$output .= '<strong>Authored by:</strong> <a href="' . get_author_posts_url($main_author->ID) . '">' . esc_html($main_author->display_name) . '</a>';
}
if ($contributors) {
// Exclude the main author from the list of contributors
$contributors = array_filter($contributors, function($contributor) use ($main_author_id) {
return $contributor->user_id != $main_author_id;
});
if ($contributors) {
// Map contributors to profile links
$contributors = array_map(function($contributor) {
$user = get_user_by('id', $contributor->user_id);
if ($user) {
return '<a href="' . get_author_posts_url($user->ID) . '">' . esc_html($user->display_name) . '</a>';
}
return 'Unknown Contributor';
}, $contributors);
$output .= '<br><strong>Additional contributions by:</strong> ' . implode(', ', $contributors);
}
}
return $output;
}
// Add the contributors meta box to the post editor
add_action('add_meta_boxes', 'easycoding_add_contributors_meta_box');
function easycoding_add_contributors_meta_box() {
add_meta_box(
'contributors_meta_box',
'Contributors',
'easycoding_contributors_meta_box_callback',
'post',
'side',
'default'
);
}
function easycoding_contributors_meta_box_callback($post) {
global $wpdb;
$table_name = $wpdb->prefix . 'post_contributors';
// Get contributors for this post (including the author)
$contributors = $wpdb->get_results($wpdb->prepare(
"SELECT user_id FROM $table_name WHERE post_id = %d",
$post->ID
));
$main_author_id = $post->post_author;
// Filter out the main author from the contributors list
$contributors = array_filter($contributors, function($contributor) use ($main_author_id) {
return $contributor->user_id != $main_author_id;
});
// Check if we have contributors (excluding the main author)
if ($contributors && count($contributors) > 0) {
echo '<ul>';
foreach ($contributors as $contributor) {
$user = get_user_by('id', $contributor->user_id);
if ($user) {
echo '<b><li>' . esc_html($user->display_name) . '</li></b>';
}
}
echo '</ul>';
} else {
// If no contributors exist and the post author is the only one, show "No contributors yet."
echo '<p>No other users have contributed to this post.</p>';
}
}
?>
template-author.php
<?php
/* Template for displaying custom author pages */
get_header();
// Get the queried author object
$author = get_queried_object();
?>
<div class="custom-author-page">
<div class="author-header">
<?php echo get_avatar($author->ID, 120); // Display avatar with size 120px ?>
<div class="author-details">
<h1><?php echo esc_html($author->display_name); ?></h1>
<p><?php echo esc_html($author->description); ?></p>
</div>
</div>
<h2>Posts by <?php echo esc_html($author->display_name); ?></h2>
<ul>
<?php
// Query to get posts authored by the author
$args = array(
'author' => $author->ID,
'posts_per_page' => 10,
);
$author_posts = new WP_Query($args);
if ($author_posts->have_posts()) {
while ($author_posts->have_posts()) {
$author_posts->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
} else {
echo '<p>No posts by this author.</p>';
}
wp_reset_postdata();
?>
</ul>
<h2>Posts Contributed To</h2>
<ul>
<?php
// Query to get posts contributed to by this author
global $wpdb;
// Prepare the SQL query to select posts where the author is a contributor
$query = "
SELECT p.ID, p.post_title
FROM {$wpdb->posts} p
INNER JOIN {$wpdb->prefix}post_contributors c ON p.ID = c.post_id
WHERE c.user_id = %d
AND p.post_author != c.user_id
LIMIT 10
";
// Prepare the SQL query for this author
$results = $wpdb->get_results($wpdb->prepare($query, $author->ID));
if ($results) {
foreach ($results as $post) {
echo '<li><a href="' . get_permalink($post->ID) . '">' . esc_html($post->post_title) . '</a></li>';
}
} else {
echo '<p>No posts contributed to by this author.</p>';
}
?>
</ul>
</div>
<?php get_footer(); ?>
Step 3: Create the CSS Directory
- Inside the easycoding-user-enhancements folder, create a subdirectory:
- In this folder, create a file named easycoding-user-enhancements.css and paste the following code:
easycoding-user-enhancements.css
/* Custom styles for the author page */
.custom-author-page {
max-width: 1024px;
margin: 0 auto;
box-sizing: border-box;
width:100%;
padding: 20px;
}
.custom-author-page h1 {
font-size: 2em;
color: #333;
}
.custom-author-page ul {
list-style-type: none;
padding: 0;
}
.custom-author-page ul li {
margin: 10px 0;
}
.custom-author-page a {
text-decoration: none;
color: #0073aa;
}
.custom-author-page a:hover {
color: #005177;
}
/* Styling for the author header with avatar and details */
.author-header {
display: flex;
align-items: center;
margin-bottom: 20px;
}
.author-header img.avatar {
width: 120px;
height: 120px;
border-radius: 50%;
margin-right: 20px;
}
.author-details {
display: flex;
flex-direction: column;
}
.author-details h1 {
margin: 0;
}
.author-details p {
margin: 8px 0 0;
color: #666;
font-size: 1em;
}
Step 4: Activate the Addon
- Go to your WordPress admin dashboard.
- Navigate to Plugins > Installed Plugins.
- Find the EasyCodingWithAI – Custom User Page plugin and click Activate.
Your plugin is now installed and ready to use.
Step 5: Example Usage of the Addon
Once you have installed and activated the EasyCodingWithAI – Custom User Page addon, you can use it in the following ways:
- Viewing the Custom Author Page
Navigate to any author’s profile page in your WordPress site. The custom template will display the author’s avatar, bio, and posts they have authored and contributed to. - Using the Contributors List Shortcode
You can use the Authored by: Richard Robins shortcode to show the main author and additional contributors for any post. To include it:- Within the content editor:
- Simply paste
Authored by: Richard Robins
in the post where you want the contributors list to appear.
- In template files:
- Add this line of code where you want the shortcode output to display:
echo do_shortcode('[contributors_list]');
- Example Output
When you add the shortcode in a post or template, it might output something like this: Authored by: Jane Doe
Additional contributions by: John Smith, Alex Johnson
Disclaimer
The coding tips and guides provided on this website are intended for informational and educational purposes only. While we strive to offer accurate and helpful content, these tips are meant as a starting point for your own coding projects and should not be considered professional advice.
We do not guarantee the effectiveness, security, or safety of any code or techniques discussed on this site. Implementing these tips is done at your own risk, and we encourage you to thoroughly test and evaluate any code before deploying it on your own website or application.
By using this site, you acknowledge that we are not responsible for any issues, damages, or losses that may arise from your use of the information provided herein.