I'm using a snippet to overwrite the canonical URL in YoastSEO for WordPress/WooCommerce. The snippet is based on the official docs example: https://developer.yoast.com/features/seo-tags/canonical-urls/api/
Here's my code:
function prefix_filter_canonical_example( $canonical ) {
if (is_shop() && is_paged() ) :
$canonical = get_permalink(woocommerce_get_page_id( 'shop' )).'page/'.get_query_var('paged').'/';
elseif(WCV_Vendors::is_vendor_page()):
$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
$vendor_id = WCV_Vendors::get_vendor_id( $vendor_shop );
$canonical = WCV_Vendors::get_vendor_shop_page( $vendor_id );
endif;
return $canonical;
}
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example' );
The code doesn't do anything to the canonical URL regardless of the content I return. But the if/else works fine and if I echo the content of $canonical I see the correct URLs.
I tried it already with the basic storefront theme and I deactivated nearly all plugins. But the snippet won't work. Is there anything I miss?
If you don't pass any priority to the action wordpress will take it as 10. Yoast SEO also call this filter to add canonical url so wordpress executed function in the order in which they were added to the action.
So change this line
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example' );
With this
add_filter( 'wpseo_canonical', 'prefix_filter_canonical_example', 20 );
We are working on a localization project in WordPress. In fact, we can correctly add the active browser URL with language support. I'll add the details and the entire code below. Don't worry, my question will not be so open-ended and I will make it more specific.
Step 1
First of all, we should add a variable in the link structure to the WordPress algorithm.
function custom_rewrite_basic_query_vars( $query_vars ){
$query_vars[] = 'lang';
return $query_vars;
}
add_filter( 'query_vars', 'custom_rewrite_basic_query_vars' );
Thus, WordPress detects a variable such as ?lang= and includes it in the query mechanism.
Step 2
Using the "add_rewrite_rule" function, you need to add the variable ?lang= to run in the background of WordPress's persistent link structure.
Easy Example:
/*
LANG PREFIX + INDEX = localhost/en/ = localhost/?lang=en
*/
add_rewrite_rule(
'^(en|fr|de|ru|tr)/?$',
'index.php?lang=$matches[1]',
'top'
);
I will not add the remaining instances one by one, because you should use close to 20 rewrite rules. (Date, Category, Comment, Page, Text, Pagination, etc.)
NOTE: Those who work on such a project and want to use these rules will add it if requested.
Now our persistent connection structure supports variables for the language option.
localhost/en/hello-world
localhost/ru/hello-world
localhost/de/04/07/2019
localhost/fr/page/2
etc. all the links you can think of.
Step 3
If a variable is detected from the browser (for example: lang = en), we will display the contents of the previously saved inter-language translated.
At this point we do not need any support. But when we get to step 4, we're stuck.
Step 4
Activating the prefix language variable that was added before, users when using the links within the site, on all in-site navigation links.
This means that if a user has access to the localhost/fr/hello-world link, they must reach the localhost/fr/contact link when they return to the Home Page or click the Contact link.
To do this, you need to add the language prefix that is currently active in the browser to all in-site links.
Unfortunately, this is the only point where we hang out and we can't find a solution.
In fact, we tried most things before writing here. (Of course within our knowledge). We even looked at plug-ins that offer some language support.
e.g. <WP Multilang>
Of course, it's so complicated and so much code. We didn't even understand the plug-in.
There is, of course, a logical way of doing this, and I hope that person sees this post and answers. Thank you sincerely for all your supports.
One step to Answer
After a while, I reached the following code and confirmed that it was working correctly.
<?php
function link_fn( $url ){
$lang = get_query_var('lang', false);
if( $lang !== false && strpos( $url, $lang ) !== false )
$url = $url . $lang . '/';
return $url;
}
add_filter( 'shortcut_link', 'link_fn' );
add_filter( 'post_link', 'link_fn' );
add_filter( 'page_link', 'link_fn' );
add_filter( 'post_type_link', 'link_fn' );
add_filter( 'attachment_link', 'link_fn' );
add_filter( 'term_link', 'link_fn' );
add_filter( 'author_link', 'link_fn' );
add_filter( 'post_type_archive_link', 'link_fn' );
add_filter( 'day_link', 'link_fn' );
add_filter( 'month_link', 'link_fn' );
add_filter( 'year_link', 'link_fn' );
But at this point, I came across a problem. Here we get data like "localhost/hello-world/[LANG]/".
What I want is "localhost/[LANG]/hello-world".
To resolve this situation, you must add a REGEX query to the existing URL structure, if it does not contain the LANG, add the language code to the BASE URL. As a REGEX query should be added, I could not do this...
I not want to answer my own question and confirm. In addition to the code I have written above, if there is an answer containing the query I want, I will mark it as the answer.
Answer
function link_fn( $url ){
$lang = get_query_var('lang', false);
$site_url = get_option('home');
if( $lang !== false )
{
$new_url = str_replace( $site_url, "", $url );
if( preg_match('/\b$lang\b/', $new_url) !== false )
{
$url = $site_url."/".$lang.$new_url;
}
}
return $url;
}
add_filter( 'home_url', 'link_fn' );
You got really far. Very nice effort
I understand that what's between you and victory is only to change the outcome link structure?
If yes, here is a solution:
function link_fn($url){
$lang = get_query_var('lang', false);
$url_parts = explode('/', $url);
if ( $lang && in_array($lang, $url_parts) === false) { // lang does not exists in URL
array_splice($url_parts, 1, 0, $lang);
}
$url = implode('/', $url_parts);
return $url;
}
I think one possible approach is to get all the HTML content use ob_start() and ob_get_contents() and then get all the link with regEx.
Note: not all links are post/page, there are some static files too.
Can you language constant be a part of query string instead of path?
instead of
localhost/en/hello-world
like this
localhost/hello-world?lang=en
If so take a look How to pass extra variables in URL with WordPress
I suggest to dig into this direction.
add_action( 'pre_get_posts', 'lang_pre_get_posts', 1 );
function lang_pre_get_posts( $query ) {
if ( is_admin() || ! $query->is_main_query() ){
return;
}
add_query_arg( 'lang', filter_var( $_COOKIE["lang"], FILTER_SANITIZE_STRING ) );
}
I'm running the latest Wordpress with WooCommerce
I'm trying to customise my search results when I use the search bar in the header of my site.
This is how the results appear when doing a normal search:
http://www.sunshinetrading.com/snowmasters/?s=snow
This is how they should appear, when I search through WooCommerce.
http://www.sunshinetrading.com/snowmasters/?s=snow&post_type=product
What I need to do is automatically append &post_type=product onto every search query launched from the header.
My attempts at a solution:
I added this to my child theme's functions.php file, to try and append the query which would fix everything.
// Search WooCommerce
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
echo esc_url( add_query_arg( 'post_type', 'product' ) );
$query->set('post_type', 'product');
}
}
}
add_action('pre_get_posts','search_filter');
However, when I do this, and do a search, what the URL should be appears briefly as text on the page, before the website proceeds to load exactly the same page as before.
What am I doing wrong? Maybe I could solve this problem by editing the .htaccess file. I've added the following ...
# REWRITE SNOWMASTERS SEARCH
RedirectMatch 302 snowmasters.com.au/?s=(.*) http://snowmasters.com.au/?s=$1&post_type=product
This should redirect http://snowmasters.com.au/?s=snow to http://snowmasters.com.au/?s=snow&post_type=product
But it's not working?
I would appreciate your help Stack Overflow community :)
Thanks
Did you try this function already?
add_query_arg( 'post_type', 'product', 'http://www.sunshinetrading.com/' );
You can also register you query string var in wordpress like this:
function add_query_vars_filter( $qVars ){
$qVars[] = "post_type";
return $qVars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
This is from the codex: https://codex.wordpress.org/Plugin_API/Filter_Reference/query_vars
I have a WordPress site which uses Yoast SEO to manage canonical links and noindex meta robots, among other things, with a very peculiar configuration.
By default, Yoast's plugin adds a <link rel="canonical" href="..."> in every page. I'm not interested in show a canonical link in every single static page (whatever its type is) but in some type of posts: Pages and Posts (not Attatchment, Category, Archive, etc.)
I know the way to prevent Yoast from adding a canonical link by default, just adding the following code in functions.php: add_filter( 'wpseo_canonical', '__return_false' );. But what if I just want to pass that filter in some post types? Or passing it in every page except some types of posts (both approaches are useful to me). Help would be much appretiated. Any?
UPDATE: Valid answer with some minor fixes
function remove_canonical_from_post_types($url) {
$post_type = get_post_type();
if ($post_type !== 'post' && $post_type !== 'page') { // If post type is not Post nor Page, doesn't add a canonical link in any case
return false;
}
else { // It is Post or Page
if (is_category() || is_author() || is_tag() || is_archive()) { // If page is post type 'Post' we don't want to add canonical in some sub-types: Category, Author, Tag, Archive
return false;
}
return $url; // In any other case (Posts and Pages) adds a canonical link
}
}
add_filter('wpseo_canonical', 'remove_canonical_from_post_types');
Try to use the provided "wpseo_canonical" filter and check there for the correct post type (and return false there).
Something like this:
function remove_canonical_from_post_types($url)
{
$post_type = get_post_type();
if ($post_type !== 'post' && $post_type !== 'page') {
return false;
}
return $url;
}
add_filter('wpseo_canonical', 'remove_canonical_from_post_types');
How do I modify a page title for specific pages in shortcode?
The following will change the title but it executes for every page. I need more control over where it executes.
function assignPageTitle(){
return "Title goes here";
}
add_filter('wp_title', 'assignPageTitle');
Is there a way to call the above in a shortcode function? I know how to use do_shortcode() but the above is a filter.
My goal is to modify the page title based on a URL parameter. This only happens for specific pages.
Although WordPress shortcodes was not designed to do this, it can be done. The problem is shortcodes are processed AFTER the head section is sent so the solution is to process the shortcode BEFORE the head section is sent.
add_filter( 'pre_get_document_title', function( $title ) {
global $post;
if ( ! $post || ! $post->post_content ) {
return $title;
}
if ( preg_match( '#\[mc_set_title.*\]#', $post->post_content, $matches ) !== 1 ) {
return '';
}
return do_shortcode( $matches[0] );
} );
add_shortcode( 'mc_set_title', function( $atts ) {
if ( ! doing_filter( 'pre_get_document_title' ) ) {
# just remove the shortcode from post content in normal shortcode processing
return '';
}
# in filter 'pre_get_document_title' - you can use $atts and global $post to compute the title
return 'MC TITLE';
} );
The critical point is when the filter 'pre_get_document_title' is done the global $post object is set and $post->post_content is available. So, you can find the shortcodes for this post at this time.
When the shortcode is normally called it replaces itself with the empty string so it has no effect on the post_content. However, when called from the filter 'pre_get_document_title' it can compute the title from its arguments $atts and the global $post.
Taken from the WordPress Codex
Introduced in WordPress 2.5 is the Shortcode API, a simple set of
functions for creating macro codes for use in post content.
This would suggest that you can't control page titles using shortcodes as the shortcode is run inside the post content at which point the title tag has already been rendered and is then too late.
What is it exactly that you want to do? Using the Yoast SEO Plugin you can set Post and Page titles within each post if this is what you want to do?
You could create your custom plugin based on your URL parameters as so:
function assignPageTitle(){
if( $_GET['query'] == 'something' ) { return 'something'; }
elseif( $_GET['query'] == 'something-else' ) { return 'something-else'; }
else { return "Default Title"; }
}
add_filter('wp_title', 'assignPageTitle');