Custom Product Search URL in Wordpress - wordpress

Hi I have tried the following code and does seem to work and changes the regular search url for a keyword "holi"
https://www.englishbix.com/search/?s=holi&post_type=product to https://www.englishbix.com/search/holi/
but it uses the default search template to display the product search results instead of the woocommerce search result template.
//change the url to static slug instead of dynamic one
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
Results I want
Results I get with above code

Related

How can I replace a custom-post-type with a custom post category in permalink without getting a 404 in wordpress?

I'm a wordpress newbie. I will try to describe my problem in the clearest way possible.
I'm trying to do two things here:
Remove CPT from a permalink.
Add a custom taxonomy type where it used to be the CPT in the permalink.
Permalinks on site used to be like this:
http://example.com/custom-post-type/post-name
I managed to remove the CPT from the permalink based on this:
how to remove custom post type from wordpress url?
Then I modify my code to add the custom taxonomy type to the permalink to be like this:
http://example.com/post-category/post-name
This is my code:
function remove_cpt_slug( $post_link, $post ) {
if ( 'custom-post-type-name' === $post->post_type && 'publish' === $post->post_status ) {
$post_tags = get_the_terms($post->ID, 'custom-post-type-name-category');
$post_link = str_replace( '/' . $post->post_type . '/', '/' . $post_tags[0]->slug . '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );
function add_cpt_post_names_to_main_query( $query ) {
// Return if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Return if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Return if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'custom-post-type-name' ) );
}
add_action( 'pre_get_posts', 'add_cpt_post_names_to_main_query' );
This worked but now every post from my CPT gives a 404. How can I solve this?

How to change search URL in wordpress?

I want to change the search url to something like this
http://simply.mmag.in/blog/?s=coaching
Where as my current search result shows which is recommended by most user and article
http://simply.mmag.in/blog/coaching
Code which i am using for the above search result
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
and the base url code
function re_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->search_base = 'blog';
$wp_rewrite->flush_rules();
}
add_action('init', 're_rewrite_rules');
How can i get search result like this
http://simply.mmag.in/blog/?s=coaching
What happends if you just remove or comment out your wpb_change_search_url() function?
Have you tried to just remove this custom code? Wordpress by default uses ?s= in urls, and code used by you just rewrite it.

Redirect to external URLs in Woocommerce and add rel="nofollow"

I am trying to customize Wocommerce to redirect the image and product name links to affiliate URLs, as now they are linking to the product page.
So far, I came to a solution in which I can redirect them to the affiliate external links adding this piece of code to functions.php:
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
wp_redirect( $product->get_product_url() );
exit;
}
}
However I would like to make this links nofollowed as they are affiliate links, as well as open them in a new tab, but after trying several coding options and plugins I haven't found a suitable solution. Using plugins that would nofollow all external links is not working as they are treated now as internal links (even though redirected to external).
Any help would be much appreciated, thank you.
Add this to your child theme function:
function custom_woocommerce_template_loop_product_link_open() {
echo '<a href="' . get_the_permalink() . '" rel="nofollow">';
}
remove_action( 'woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10 );
add_action( 'woocommerce_before_shop_loop_item', 'custom_woocommerce_template_loop_product_link_open', 10 );

Add second single product page in Woocommerce

Is it possible to add a second single product page in Woocommerce?
So basically when I am at the single product page I click the "next" button and I get directed to the same single product page with another template. So I just want to retrieve the same data on the second page.
Single product page:
Next page:
And the next page would be the checkout page but that would just be a link to the checkout page so that part would be easy.
What I would do in this case is add a link that will reload the page with a custom query arg in the URL.
Then you can filter the template via template_include to load a different template. Untested, so be careful of syntax typos.
add_filter( 'template_include', 'so_30978278_single_product_alt' );
function so_30978278_single_product_alt( $template ){
if ( is_single() && get_post_type() == 'product' && isset( $_GET['next-step'] ) && intval( $_GET['next-step'] ) == 1 ) {
$template = locate_template( 'single-product-alt.php' );
}
return $template;
}
add_action( 'woocommerce_before_add_to_cart_form', 'so_30978278_additional_template_button' );
function so_30978278_additional_template_button(){
printf( '<a class="button" href="%s">%s</a>', esc_url( add_query_arg( 'next-step', 1 ) ), __( 'Next Step' ) );
}
See add_query_arg for reference.

How do i change the url of wordpress search results?

by default wordpress search results go to sitename.com/?s=terms but i would like it be changes to sitename.com/search/?s=terms
I have tried using the
fb_change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
This get close as now when you do search it goes to search/searchterm but does not have the ?s= in the url
Alternatively, you can change the rule in .htaccess
Add the following on the line after "RewriteBase /":
RewriteRule ^?s=(.*)$ search/?s=$1
See reference here: http://wordpress.org/support/topic/change-default-search-string-s-to-permalink-structure

Resources