Make WordPress Core

Opened 5 years ago

Last modified 19 months ago

#48786 new defect (bug)

Wrong URLs to categories, authors etc. to a site in a multisite

Reported by: hinjiriyo's profile Hinjiriyo Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.3
Component: Permalinks Keywords:
Focuses: multisite Cc:

Description

WP returns a wrong URL to a term if the term is in another site of a multisite. Terms like categories, tags, authors, date archives and other terms.

How to reproduce: execute this code, with site ID and post ID as examples of valid values in the current installation, in a subsite different to site ID = 3:

<?php
$site_id = 3;
$post_id = 1389;
$category_links = array();

switch_to_blog( $site_id );
$categories = get_the_terms( $post_id, 'category' );
foreach ( $categories as $category ) {
        // get link to category
        $category_links[] = sprintf(
                '<a href="%s">%s</a>',
                esc_url( get_category_link( $category->term_id ) ),
                esc_html( $category->name )
        );
}
print join( ', ', $category_links );
restore_current_blog();

If other base names for the category and tags are used (being specified on the Permalinks Settings page) the bug is more obviuos. In the next example the subsite is the german version of the main site, so the site path is '/de' and the category base name is specified to 'kategorien', and the german name for the category 'General' is 'Allgemein':

On the english main site the code prints this URL to the german category 'Allgemein':
https://www.example.com/de/blog/category/allgemein/
which is wrong. The correct URL is
https://www.example.com/de/kategorie/allgemein/

On the german subsite the code prints this URL to the english category 'General':
https://www.example.com/kategorie/general/
which is wrong. The correct URL is
https://www.example.com/blog/category/general/

The code sets correct links if it is executed in the site with the ID = 3 (e.g. is current site is same as the site being switched to).

Same is with similar functions like:

get_author_posts_url()
get_category_link()
get_month_link()
get_post_format_link()
get_post_type_archive_link()
get_tag_link()

Change History (1)

#1 @timholz
19 months ago

I encounter the same mix-up for the category base:

if (is_archive() && !is_tag()){
    switch_to_blog($myblog_id );
    //get category link of switched blog
    $url_cat = get_category_link(get_query_var( 'cat' ));
    echo $url_cat;
    restore_current_blog();
}

The echo for $myblog_id = 1 is:

http://localhost/es/category/archivo/ //should be categoria

The echo for $myblog_id = 2 is:

http://localhost/categoria/archiv/ //should be category

The same counts for get_tag_link()

Last edited 19 months ago by timholz (previous) (diff)
Note: See TracTickets for help on using tickets.