最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

redirect - WooCommerce get_author_posts_url() - Author URL redirecting to shop base

programmeradmin5浏览0评论

I've noticed that when I have WooCommerce enabled and use get_author_posts_url() it automatically redirects to the 'shop base' when the user's role is Customer.

When I change the user to Subscriber get_author_posts_url() works correctly.

I've disabled all plugins to systematically identify what was causing the issue and can confirm that it's only when WooCommerce is enabled. I've also flushed rewrites. I've also even tried using $user->add_role( 'subscriber' ) to the user's capabability array.

Does anyone know why this is happening and how I can suppress it? (i.e. don't redirect get_author_posts_url() for users with a customer role.

EDIT

I believe this to be a bug, I've opened a bug ticket with WC and have posted on the WP forum thread

For now I've just set all 'customer' users to 'subscribers' when I get a reply/make any headway I will post back here.

Thanks in advance!

I've noticed that when I have WooCommerce enabled and use get_author_posts_url() it automatically redirects to the 'shop base' when the user's role is Customer.

When I change the user to Subscriber get_author_posts_url() works correctly.

I've disabled all plugins to systematically identify what was causing the issue and can confirm that it's only when WooCommerce is enabled. I've also flushed rewrites. I've also even tried using $user->add_role( 'subscriber' ) to the user's capabability array.

Does anyone know why this is happening and how I can suppress it? (i.e. don't redirect get_author_posts_url() for users with a customer role.

EDIT

I believe this to be a bug, I've opened a bug ticket with WC and have posted on the WP forum thread

For now I've just set all 'customer' users to 'subscribers' when I get a reply/make any headway I will post back here.

Thanks in advance!

Share Improve this question edited May 22, 2017 at 19:56 Ryan Dorn asked May 21, 2017 at 20:11 Ryan DornRyan Dorn 3699 silver badges23 bronze badges 2
  • Hi, Does get_author_posts_url() return the URL for customers too? Or it redirects because no URL is outputted? – Johansson Commented May 21, 2017 at 20:24
  • Hi Jack, get_author_posts_url() does return the url for customers. I've run wp_list_authors('hide_empty=0') and all of the URLs are the same. The thing is that if the user is a customer, then it redirects. So, the redirection doesn't appear to be happening because there is no URL being outputted. – Ryan Dorn Commented May 21, 2017 at 20:39
Add a comment  | 

1 Answer 1

Reset to default 1

I found!

https://wordpress/support/topic/how-to-add-author-page-to-custom-user-role/#post-8807168

So in WooCommerce, this is achieved by the following code in wp-content/plugins/woocommerce/includes/wc-user-functions.php:

/**
 * Disable author archives for customers.
 *
 * @since 2.5.0
 */
function wc_disable_author_archives_for_customers() {
    global $wp_query, $author;

    if ( is_author() ) {
        $user = get_user_by( 'id', $author );

        if ( isset( $user->roles[0] ) && 'customer' === $user->roles[0] ) {
            wp_redirect( wc_get_page_permalink( 'shop' ) );
        }
    }
}

add_action( 'template_redirect', 'wc_disable_author_archives_for_customers' );


In your theme’s functions.php (or somewhere else appropriate), you could turn that off:

/* This removes the function that redirects customers to the shop page */
function enable_author_archives_for_customers() {
  remove_action('template_redirect', 'wc_disable_author_archives_for_customers');
}
add_action( 'after_setup_theme', 'enable_author_archives_for_customers' );
发布评论

评论列表(0)

  1. 暂无评论