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 badges1 Answer
Reset to default 0Okay, 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()
版权声明:本文标题:plugin development - Change cannonical URL after changing url with add_rewrite_rule() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281366a1926295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论