WordPress query string to change SEO friendly URL - php

I want to create SCO friendly URL in the WordPress. Also, I have added a hook in my functions.php file as below code but not working.
My URL:http://bddlabs.com/webinar01/?id=pzlyglhbepotwpvbigdatadim_data&storyid=Story_997e9391-71d7-4fac-804b-de891c7aa595
I want http://bddlabs.com/webinar01/pzlyglhbepotwpvbigdatadim_data/Story_997e9391-71d7-4fac-804b-de891c7aa595
Is it possible? I am not sure about it. URL query string is coming from the third party, URL query string is coming from the third party, page content render based on query string.
Below code added landing-page-15.php it WordPress theme base page template
function fb_change_search_url_rewrite() {
if ( ! empty( $_GET['storyid'] ) ) {
wp_redirect( home_url( "/webinar01/" ) . urlencode( get_query_var( 'storyid' ) ) );
load_template( realpath(__DIR__ . '/..') . '/landing-page-15.php' );
exit();
}
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
//additional rewrite rule + pagination tested
function rewrite_search_slug() {
set_query_var('storyid', $_GET['storyid']);
add_rewrite_rule(
'find(/([^/]+))?(/([^/]+))?(/([^/]+))?/?',
'index.php?id=$matches[2]&storyid=$matches[6]',
'top'
);
}
add_action( 'init', 'rewrite_search_slug' );

You may consider using WordPress builtin permalinks option located in Settings tab.
Set %category%/%postname%/ so that permalink should work as per your requirements.
When posting new content, select a sub category and put post title the same you want to appear in your permalink.
Hope it answers your question.

Wordpress can do this by default. https://codex.wordpress.org/Using_Permalinks

Related

Dynamic meta titles for pages with URL parameters?

I've got a page called /team-page/?id=x that's built in WordPress
The URL parameter of "id" determines the content that will dynamically show on that page. However, my meta title for that page is statically set. Is there a way I can dynamically set the meta title based on the page content? Each variation of /team-page will have a unique H1 - ideally, I'd love to just grab this H1 and set it as the meta title.
You can achieve it with document_title_parts filter. It takes $titles as parameter, which consist of two parts - title and site (except for front page - instead of site it's tagline)
So try this
add_filter( 'document_title_parts', 'custom_title' );
function custom_title( $title ) {
// Just to see what is $title
// echo '<pre>';
// print_r( $title );
// echo '</pre>';
// die();
$uri = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ); // get current uri without query params
// Change meta only on team-page.
if ( '/team-page/' === $uri ) {
// you can do here anything you want
$title['title'] = get_the_title() . ' and whatever string you want ' . $_GET['id'];
}
return $title;
}
Also don't forget to check and sanitize your $_GET['id'] if needed.
You're looking for get_query_var()
Retrieves the value of a query variable in the WP_Query class.
Source # https://developer.wordpress.org/reference/functions/get_query_var/
One small thing to understand is that get_query_var() only retrieves public query variables that are recognized by WP_Query.
So we need to register our variable first. This is considered best practice. Simply using $_GET['id'] is considered to be unsafe within the Wordpress ecosystem.
add_filter( 'query_vars', 'wpso66660959_query_vars' );
function wpso66660959_query_vars( $qvars ) {
$qvars[] = 'ref';
return $qvars;
};
Also you should use something other than id as it is already used by Wordpress to handle the WP_Query. I've used ref (reference) but you can choose whatever as long as it doesn't impact Wordpress core.
then we can simply built our custom title.
<?= '<title>' . get_the_title() . ' | Order N° ' . get_query_var( 'ref', 'undifined' ) . '</title>'; ?>
Note that, if your theme is using Wordpress to set your pages title, you might need to set some sort of conditional statement around add_theme_support( 'title-tag' );, in your function.php.
Something like...
//...
if ( ! is_page( 'team-page' ) )
add_theme_support( 'title-tag' );
The title-tag is usually included in your theme options.

WordPress: Overwrite the canonical URL in YoastSEO

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 );

Manipulate slugs and urls on wordpress

Is there an away to create "virtual" slugs on wordpress?
For example:
I create a custom post type named "year" and the slug is /year/. This custom post type was only created to generate the slugs(/year/). I don't have any post inside this custom post type.
I want to type url /year/1988/ and the Wordpress call the single.php file.
If I do it today the wordpress will return 404 because 1988 needs to be a post inside the custom post type(year).
I need some .php code or .htaccess to do something like: If type something after /year/ its load the single file because I can't create a conditional for each year, 1901, 1902, 1903 ... and I will take this information and make the necessary query.
If you need this to work on your home page only (ie. mysite.org/years/1988) you can add a custom rewrite tag and rule:
add_filter( 'init', function() {
add_rewrite_tag( '%years%', '([^&]+)' );
// Match the front page and pass years value as a query var.
add_rewrite_rule( '^years/([^/]*)/?', 'index.php?years=$matches[1]', 'top' );
});
Then use the new query var to load a different template:
add_filter( 'template_include', function( $template ) {
if ( get_query_var( 'years' ) ) {
$new_template = locate_template( array( 'single.php' ) );
if ( !empty( $new_template ) ) {
return $new_template;
}
}
return $template;
});
I tried to use year, but it wouldn't work, I think it's a reserved query var used by Wordpress. And don't forget to flush your toilet, I mean rewrite rules ;).

Wordpress - Customise Search Query

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

How to change Search permalink on wordpress

Is it possible to change the Wordpress search permalink from:
www.mydomain.com/search/my+result+keyword+term
to:
www.mydomain.com/my-result/keyword-term
and to display the search page or any ideas for this.
Thank You.
Open the searchform.php within your theme folder and change the form action. You can of course even rebuild the whole search form if you like.
The wordpress codex has a usefull tutorial on this topic:
http://codex.wordpress.org/Creating_a_Search_Page
It's too late but could help others like me :)
Add following code into your functions.php file.
function wp_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
}
}
add_action( 'template_redirect', 'wp_change_search_url' );
function wp_search_rule(){
add_rewrite_rule('^search/([^/]*)?', 'index.php?s=$matches[1]', 'top');
}
add_action( 'init', 'wp_search_rule' );

Categories