admin管理员组

文章数量:1122826

So, I'm making a plugin for some niche application for my customer which involves a table with data, and I created a page within the plugin to show more in-depth information about the table row.

This is how I'm making the rewrite:

function mytableplugin_page_rewrite(){
 $page_slug = 'mytablepluginpage';

 // urls will be in the form
 // /your-page/42/

 add_rewrite_rule(
     'mytableplugin/([0-9a-zA-Z_-]+)/?$',
     'index.php?pagename=' . $page_slug . '&tid=$matches[1]',
     'top'
 );
}

But the redirect doesn't affect the canonical URL, so every "mytableplugin" page has the same "mytablepluginpage" canonical URL

I'm not using Yoast SEO plugin, and I don't want my plugin to depend on it, so, how can I change the canonical link of the page? I want the custom name (mytableplugin != mytablepluginpage) to be kept and the argument to keep on the canonical URL

It's being a problem because the TranslatePress plugin is using the wrong URL when switching languages, it's throwing me back into the "mytablepluginpage" page that has no content without the queries.

Edit: I tried some more stuff, and hijacking the rel_canonical function does work to change the canonical URL, but doesn't solve the problem of the translatepress plugin having the wrong URL for the language switching, here's the code I used:

remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'my_rel_canonical');

function my_rel_canonical() {
    if (is_page('mytablepluginpage')) {
        
        global $wp;
        echo "<link rel='canonical' href='".home_url( $wp->request )."'/>\n";
        
    } else {
        rel_canonical();
    }
}

(This last code came from this StackOverflow question)

Thanks in advance!

So, I'm making a plugin for some niche application for my customer which involves a table with data, and I created a page within the plugin to show more in-depth information about the table row.

This is how I'm making the rewrite:

function mytableplugin_page_rewrite(){
 $page_slug = 'mytablepluginpage';

 // urls will be in the form
 // /your-page/42/

 add_rewrite_rule(
     'mytableplugin/([0-9a-zA-Z_-]+)/?$',
     'index.php?pagename=' . $page_slug . '&tid=$matches[1]',
     'top'
 );
}

But the redirect doesn't affect the canonical URL, so every "mytableplugin" page has the same "mytablepluginpage" canonical URL

I'm not using Yoast SEO plugin, and I don't want my plugin to depend on it, so, how can I change the canonical link of the page? I want the custom name (mytableplugin != mytablepluginpage) to be kept and the argument to keep on the canonical URL

It's being a problem because the TranslatePress plugin is using the wrong URL when switching languages, it's throwing me back into the "mytablepluginpage" page that has no content without the queries.

Edit: I tried some more stuff, and hijacking the rel_canonical function does work to change the canonical URL, but doesn't solve the problem of the translatepress plugin having the wrong URL for the language switching, here's the code I used:

remove_action('wp_head', 'rel_canonical');
add_action('wp_head', 'my_rel_canonical');

function my_rel_canonical() {
    if (is_page('mytablepluginpage')) {
        
        global $wp;
        echo "<link rel='canonical' href='".home_url( $wp->request )."'/>\n";
        
    } else {
        rel_canonical();
    }
}

(This last code came from this StackOverflow question)

Thanks in advance!

Share Improve this question edited Sep 24, 2020 at 16:12 Miguel Vieira asked Sep 24, 2020 at 2:33 Miguel VieiraMiguel Vieira 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Okay, I found a way to do it, if someone ever need to create a permalink or change a permalink for a virtual page or a dynamic page, here's how:

The get_permalink(), the_permalink() and get_the_permalink() functions may be filtered by using the post_type_link, page_link and/or post_link filters, each one of these is for one kind of post.

This is the function I ended up with:

function mytableplugin_rename_permalink($url, $post) {
    
    $table = get_page_by_path('mytablepluginpage');
    global $wp;
    
    if ( 'integer' === gettype( $post ) ) {
        $post_id = $post;
    } else {
        $post_id = $post->ID;
    }

    
    // check if we are targetting the plugin page
    if (  $table->ID == $post_id ) {
        $url = home_url( $wp->request );
    }
    
    apply_filters( 'mytableplugin', home_url( $wp->request ), get_the_ID(), false );

    // Return the value of the URL
    return $url;
}

add_filter( 'post_type_link', 'mytableplugin_rename_permalink', 10, 2 );
add_filter( 'page_link', 'mytableplugin_rename_permalink', 10, 2 );
add_filter( 'post_link', 'mytableplugin_rename_permalink', 10, 2 );

I didn't have to add all the three filters, but I did as to avoid any problems if I ever change the post type of the page in the plugin.

I got the code for making it work from this page and this question

It fixed the canonical URL, the permalink and the translatePress problems.

本文标签: plugin developmentChange cannonical URL after changing url with addrewriterule()