Skip to content
  • Quick Ref
  • Contact
  • About
wpcanyon.com

wpcanyon.com

Tag: Users

Redirect author archives to homepage (or page)

Posted on August 19, 2025 By Admin No Comments on Redirect author archives to homepage (or page)

Redirect Author Archives to Homepage (or Page) in WordPress

If you want to redirect author archive WordPress pages to your homepage or a custom page, this tutorial shows you how to do it quickly and effectively. Author archives often reveal usernames or create duplicate content issues, so redirecting them can improve security and SEO.

Quick Fix

  1. Add a simple PHP snippet to your theme’s functions.php file or a site-specific plugin.
  2. Choose whether to redirect to the homepage or a custom URL.
  3. Test the author archive URLs to confirm the redirect works.

Why This Happens

By default, WordPress generates author archive pages at URLs like yoursite.com/author/username. These pages list all posts by that author. However, if you only have one author or want to hide author information for security or SEO reasons, redirecting these archives is a common approach.

Redirecting author archives prevents:

  • Exposure of usernames that can be exploited in brute force attacks.
  • Duplicate content issues that may harm SEO rankings.
  • Unnecessary archive pages that don’t add value to your site.

Requirements

  • Access to your WordPress theme files or ability to add custom code via a plugin.
  • Basic knowledge of editing PHP files safely.
  • Optional: FTP or hosting control panel access for file editing.

Step-by-step Guide to Redirect Author Archives

  1. Backup your site. Always create a backup before editing theme files.
  2. Access your theme’s functions.php file. You can do this via WordPress Admin Dashboard under Appearance > Theme Editor, or via FTP/cPanel file manager.
  3. Add the redirect code snippet. Copy and paste the following code at the end of the functions.php file:
function redirect_author_archives() {
    if (is_author()) {
        wp_redirect(home_url(), 301);
        exit;
    }
}
add_action('template_redirect', 'redirect_author_archives');

This code checks if the current page is an author archive and redirects visitors to the homepage with a permanent (301) redirect.

  1. Redirect to a custom page (optional). If you want to redirect to a specific page instead of the homepage, replace home_url() with the URL of your choice, for example:
wp_redirect(home_url('/custom-page/'), 301);
  1. Save the changes. Update the file and clear any caching plugins or server caches.
  2. Test the redirect. Visit any author archive URL like yoursite.com/author/username to confirm it redirects correctly.

Common Pitfalls

  • Editing the wrong functions.php file: Make sure you edit the active theme’s file or use a child theme to avoid losing changes on updates.
  • Cache interference: Clear browser, plugin, and server caches after adding the code to see the redirect immediately.
  • Plugin conflicts: Some SEO or redirection plugins may override this behavior. Disable them temporarily to test.
  • Incorrect URL in redirect: Ensure the URL passed to wp_redirect() is correct and includes trailing slashes if needed.

Works on

Server/Environment Compatibility
Apache Fully compatible
Nginx Fully compatible
LiteSpeed Fully compatible
cPanel / Plesk Fully compatible
Any WordPress environment Compatible as long as PHP code can be added

FAQ

  1. Can I redirect author archives without editing functions.php?

    Yes, you can use a plugin like “Redirection” or an SEO plugin with redirect features, but adding code is lightweight and efficient.

  2. Will this affect SEO negatively?

    No, a 301 redirect tells search engines the author archive pages are permanently moved, preventing duplicate content issues.

  3. How do I redirect only specific authors?

    Modify the code to check for specific author IDs or usernames before redirecting. Example:

    function redirect_specific_author_archives() {
        if (is_author('username')) {
            wp_redirect(home_url(), 301);
            exit;
        }
    }
    add_action('template_redirect', 'redirect_specific_author_archives');
    
  4. What if I want to disable author archives instead of redirecting?

    You can disable author archives by returning a 404 status or using SEO plugins to noindex those pages.

  5. Is this method safe for multisite WordPress?

    Yes, but ensure you add the code to each site’s theme or a network-activated plugin if you want it site-wide.

…
Admin & Blocks

Add a custom user role with specific capabilities

Posted on August 19, 2025 By Admin No Comments on Add a custom user role with specific capabilities

Add a Custom User Role with Specific Capabilities in WordPress

When managing a WordPress site, you might need to create a custom user role tailored to your specific needs. This allows you to control exactly what users assigned to this role can and cannot do. The quick fix is to add a custom user role programmatically using WordPress functions, specifying the capabilities you want to grant.

Quick Fix: Add a Custom User Role in WordPress

  1. Open your theme’s functions.php file or create a custom plugin.
  2. Use the add_role() function to define the new role and its capabilities.
  3. Save the changes and assign the new role to users via the WordPress admin panel.

Why This Happens

WordPress comes with predefined user roles like Administrator, Editor, Author, Contributor, and Subscriber. However, these roles might not fit every use case. For example, you may want a role that can moderate comments but cannot publish posts, or a role that can manage WooCommerce orders but not access other admin areas. Adding a custom user role with specific capabilities lets you tailor user permissions precisely, improving security and workflow.

Requirements

  • Access to your WordPress site’s files (via FTP, cPanel, or hosting file manager).
  • Basic knowledge of PHP and WordPress theme/plugin editing.
  • Administrator access to the WordPress admin dashboard.

Step-by-Step: How to Add a Custom User Role with Specific Capabilities

  1. Backup your site — Always back up your site files and database before making code changes.
  2. Choose where to add the code — You can add the code to your theme’s functions.php file or create a simple custom plugin.
  3. Write the code to add the role — Use the add_role() function to create the new role and assign capabilities.
  4. Save and upload the file — If editing locally, upload the modified file to your server.
  5. Assign the new role to users — Go to WordPress admin Users Edit user Role dropdown to assign the custom role.

Code Snippet: Adding a Custom User Role

<?php
function add_custom_user_role() {
    add_role(
        'custom_moderator', // Role slug
        'Custom Moderator', // Display name
        array(
            'read' =true, // Can read content
            'edit_posts' =false, // Cannot edit posts
            'delete_posts' =false, // Cannot delete posts
            'moderate_comments' =true, // Can moderate comments
            'upload_files' =true, // Can upload files
            'manage_categories' =false, // Cannot manage categories
        )
    );
}
add_action('init', 'add_custom_user_role');
?>

This example creates a role called “Custom Moderator” that can read content, moderate comments, and upload files but cannot edit or delete posts.

Removing a Custom Role

If you want to remove the custom role later, use the remove_role() function:

<?php
function remove_custom_user_role() {
    remove_role('custom_moderator');
}
add_action('init', 'remove_custom_user_role');
?>

Note: Removing a role does not delete users assigned to it. You should reassign those users to another role before removing.

Common Pitfalls When Adding Custom User Roles

  • Adding roles on every page load: Calling add_role() on every page load can cause issues. It’s best to run it once or check if the role exists before adding.
  • Incorrect capability names: Using invalid or misspelled capabilities will result in unexpected behavior. Refer to the WordPress Roles and Capabilities documentation for valid capability names.
  • Not assigning the role to users: After adding a role, you must assign it to users manually or programmatically.
  • Forgetting to remove roles on plugin deactivation: If you add roles via a plugin, consider cleaning up by removing roles on plugin deactivation.
  • Role conflicts: Avoid using role slugs that conflict with existing roles or plugins.

Works on

  • Web servers: Apache, Nginx, LiteSpeed
  • Hosting panels: cPanel, Plesk, DirectAdmin
  • WordPress versions: 4.0 and above (recommended to use latest stable version)
  • Compatible with both single and multisite WordPress installations

FAQ

Q1: Can I add multiple custom roles at once?
A: Yes, you can call add_role() multiple times with different slugs and capabilities within the same function or hook.
Q2: How do I check if a role already exists before adding it?
A: Use get_role('role_slug'). It returns null if the role does not exist. Example:
if (null === get_role('custom_moderator')) {
    add_role(...);
}
Q3: Can I modify capabilities of an existing role?
A: Yes, use get_role('role_slug') to get the role object, then add or remove capabilities with add_cap() and remove_cap().
Q4: Will custom roles be removed if I switch themes?
A: If you add roles in your theme’s functions.php, switching themes will remove those roles. To keep roles persistent, use a custom plugin.
Q5: How do I assign a custom role to a user programmatically?
A: Use the wp_update_user() function. Example:
wp_update_user(array(
    'ID' =$user_id,
    'role' ='custom_moderator'
));
…
Admin & Blocks

Showing Amount Of Comments A Comment Author Made

Posted on August 19, 2025 By Admin No Comments on Showing Amount Of Comments A Comment Author Made

Showing Amount Of Comments A Comment Author Made

When managing a WordPress site, you might want to display how many comments a particular comment author has made. This can add social proof, encourage engagement, or simply provide context to readers. The quick fix is to use a custom function that counts comments by author and then display that count alongside their comment.

Quick Fix: Count Comments by Author WordPress

  1. Add a custom function to your functions.php file or a small custom plugin that counts comments by the author’s email or user ID.
  2. Modify your comment template to display the count next to each comment author.
  3. Test to ensure the count updates correctly for each commenter.

Why This Happens

By default, WordPress does not show how many comments a user or guest commenter has made. This is because comments are stored individually without aggregation by author. To display the number of comments an author has made, you need to query the database and count comments associated with that author’s email or user ID.

This is especially useful for community sites, forums, or blogs with active discussions where recognizing frequent commenters adds value.

Step-by-Step: Updated Code for Modern WordPress

The following example uses WordPress core functions and best practices to count comments by author email (works for both logged-in users and guest commenters):

<?php
/**
 * Get the number of approved comments by a comment author.
 *
 * @param WP_Comment $comment Comment object.
 * @return int Number of approved comments by the author.
 */
function get_comment_author_comment_count( $comment ) {
    if ( ! $comment instanceof WP_Comment ) {
        return 0;
    }

    $author_email = $comment->comment_author_email;

    if ( empty( $author_email ) ) {
        return 0;
    }

    $args = array(
        'author_email'   => $author_email,
        'status'         => 'approve',
        'count'          => true,
    );

    $count = get_comments( $args );

    return (int) $count;
}
?>

To display this count next to each comment author in your theme’s comments.php or comment template part, add:

<?php
$comment_count = get_comment_author_comment_count( $comment );
if ( $comment_count > 1 ) {
    echo '<span class="comment-author-count">(' . esc_html( $comment_count ) . ' comments)</span>';
}
?>

How to Add via functions.php or a Small Plugin

  1. Open your active theme’s functions.php file or create a new plugin file (e.g., comment-author-count.php).
  2. Paste the get_comment_author_comment_count() function into the file.
  3. Modify your comment template to call this function and output the count as shown above.
  4. Save and upload the changes.

If you prefer a plugin, wrap the function in plugin headers like this:

<?php
/*
Plugin Name: Comment Author Comment Count
Description: Displays the number of comments a comment author has made.
Version: 1.0
Author: Your Name
License: GPL2
*/

function get_comment_author_comment_count( $comment ) {
    if ( ! $comment instanceof WP_Comment ) {
        return 0;
    }

    $author_email = $comment->comment_author_email;

    if ( empty( $author_email ) ) {
        return 0;
    }

    $args = array(
        'author_email'   => $author_email,
        'status'         => 'approve',
        'count'          => true,
    );

    $count = get_comments( $args );

    return (int) $count;
}
?>

Step-by-Step Test

  1. Ensure your site has multiple comments from the same author (same email address).
  2. Add the function to functions.php or activate your plugin.
  3. Edit your theme’s comment template (usually comments.php) to call get_comment_author_comment_count( $comment ) and display the count.
  4. Visit a post with comments and verify the count appears next to the author’s name.
  5. Try commenting again with the same email and refresh to see the count increase.

Block Themes & Gutenberg Notes

With the rise of block themes and Gutenberg, comments are often rendered via blocks or server-side rendering. To integrate this functionality:

  • If your theme uses the core/comment block, you may need to create a custom block variation or extend the comment rendering via render_callback in PHP.
  • Use the same get_comment_author_comment_count() function inside your block’s PHP render callback to output the count.
  • For block-based themes, consider creating a small plugin that hooks into comment rendering filters like comment_text or comment_author to append the comment count dynamically.

Common Pitfalls

  • Counting by email only: Guest commenters with different emails will be counted separately even if they are the same person.
  • Performance: Counting comments for every comment on a large site can cause performance issues. Consider caching results if needed.
  • Comment moderation: Only approved comments are counted to avoid showing counts for spam or pending comments.
  • Theme compatibility: Some themes heavily customize comment output; ensure you insert the code in the correct template file.

Works on

Environment Compatibility
Web Servers Apache, Nginx, LiteSpeed
Control Panels cPanel, Plesk, DirectAdmin
WordPress Versions 5.0 and above (modern block and classic themes)
Themes Classic PHP templates, Block themes with minor adjustments

FAQ

…
WordPress Snippets

Recent Posts

  • Top WordPress Themes for Blogs in 2025
  • WordPress Admin Panel Trick: Adding ID Field to the Posts Listing
  • Solution previous_posts_link and next_posts_link Not Working
  • Show Top Commentators in WordPress Without a Plugin
  • How to Style Admin Comments in WordPress

Recent Comments

    Archives

    • August 2025

    Categories

    • Admin & Blocks
    • Admin & UI
    • Automation
    • Automation & Plugins
    • Comments
    • Comparisons
    • Database & Revisions
    • Developer Snippets
    • Fixes & Errors
    • Media & Thumbnails
    • Queries & Pagination
    • Security
    • Speed & Security
    • Tips & Tricks
    • WooCommerce How‑tos
    • WordPress Snippets
    • WordPress Themes
    • Terms & Conditions
    • Affiliate Disclosure

    Copyright © 2025 wpcanyon.com.

    Powered by PressBook WordPress theme

    Also by the maker of MySurveyReviews.com