WooCommerce Shop Page Endpoint. No Products - php

I've been trying to add an endpoint for the shop page using this code:
add_action( 'init', 'add_city_endpoint' );
function add_city_endpoint() {
add_rewrite_endpoint( 'city', EP_PAGES );
}
to get products filtered when url is something like: /shop/city/new-york/
But I get page with no products.
How can I make it work?

Alright, I'll answer my own question.
Since the shop page is not an actual wordpress page, but an archive page for "product" post type, the endpoints don't work well for my purpose. And the proper way would be to use add_rewrite_rule instead:
add_action( 'init', 'city_add_rewrite_rules' );
function city_add_rewrite_rules() {
$shop_page = ( $shop_page_id = wc_get_page_id( 'shop' ) ) && get_post( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop';
add_rewrite_tag( '%sr_city%', '([^&]+)' );
add_rewrite_rule(
"^$shop_page/city/([^/]+)/?$", 'index.php?post_type=product&sr_city=$matches[1]', 'top'
);
//pagination
add_rewrite_rule(
"^$shop_page/city/([^/]+)/page/([0-9]+)?$", 'index.php?post_type=product&sr_city=$matches[1]&paged=$matches[2]', 'top'
);
}

Related

Wordpress rewrite_rules can't access second and third add_rewrite_tag

I accepted a new challenge making a custom Wordpress plugin.
After doing some research I started creating clean urls for the plugin and collected the code below.
When I visit the url https://mysite/estates/list/test/baarle/
The first custom var/rewrite_tag "jebamodus" is displayed with a value "list" in the $wp_query->query_vars array. This one's good.
The second and/or third var/rewrite_tag isn't displayed/registered?
Do I need to init each var/rewrite_tag with rewrite url seperated or are there some faults in my code?
I feel like I almost got it to work, but can't find what's wrong.
Some fresh eyes needed.
function wpse_205120_query_vars( $qvars ) {
$qvars[] = 'jebamodus';
$qvars[] = 'jebatest';
$qvars[] = 'jebaregion';
return $qvars;
}
add_filter( 'query_vars', 'wpse_205120_query_vars' );
add_action( 'init', function() {
add_rewrite_tag( '%jebamodus%', '([^/]+)' );
add_rewrite_tag( '%jebatest%', '([^/]+)' );
add_rewrite_tag( '%jebaregion%', '([^/]+)' );
add_rewrite_rule( '^estates/([^/]+)', 'index.php?jebamodus=$matches[1]', 'top' );
add_rewrite_rule( '^estates/([^/]+)/([^/]+)', 'index.php?jebamodus=$matches[1]&jebatest=$matches[2]', 'top' );
add_rewrite_rule( '^estates/([^/]+)/([^/]+)/([^/]+)', 'index.php?jebamodus=$matches[1]&jebatest=$matches[2]&jebaregion=$matches[3]', 'top' );
flush_rewrite_rules(true);
} );
add_filter('template_include', function($template){
global $wp_query;
if( isset($wp_query->query_vars['jebamodus'])) {
echo "<pre>";
print_r($wp_query->query_vars);
wp_die();
}
return $template;
});

add_rewrite_rule - suddenly stopped working (wordpress)

I have a url
example.com/sample-page/?amount=567
It initially worked ok, it redirects to:
example.com/sample-page/567
But after few days, i noticed its not working any more.
Its now redirecting to post without query var or get var
What could be the reason?? Is there any solution??
I used below codes in my functions.php file:
& I already flushed permalinks.
I am using custom post type
I have removed custom post type base from url using a plugin.
UPDATE
I just noticed Its due to empty post id/pagename.
So now, Question is how to get page name of current post...inside init function dynamically??
//https://developer.wordpress.org/reference/functions/wp_redirect/#comment-4109
add_action( 'init', function() {
global $wp;
$pagename = $wp->request;
add_rewrite_rule( ''.$pagename.'/([0-9]+)[/]?$', 'index.php?pagename='.$pagename.'&amount=$matches[1]', 'top' );
} );
add_filter( 'query_vars', function( $query_vars ) {
$query_vars[] = 'amount';
return $query_vars;
} );
function wpb_change_search_url() {
global $wp;
if ( ! empty( $_GET['amount'] ) ) {
wp_redirect( get_permalink( home_url( $wp->request ) ) . urlencode( get_query_var( 'amount' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );

How to add multiple categories in function WordPress

Wordpress - functions.php
I'm using this function to automatically add custom structure in url for posts in a specific categorie.
But now I would like to know how I could add multiple categories without having to copy this function over and over.
Other solution could be to have this function work for the parent post category and all child categories.
// url structure
add_filter( 'post_link', 'custom_permalink', 10, 3 );
function custom_permalink( $permalink, $post, $leavename ) {
// Get the category for the post
$category = get_the_category($post->ID);
if ( !empty($category) && $category[0]->cat_name == "b" ) {
$cat_name = strtolower($category[0]->cat_name);
$permalink = trailingslashit( home_url('/questions/' . $post->post_name .'/' ) );
}
return $permalink;
}
add_action( 'init', 'custom_rewrite_rules' );
function custom_rewrite_rules() {
add_rewrite_rule(
'b/([^/]+)(?:/([0-9]+))?/?$',
'index.php?category_name=b&name=$matches[1]&page=$matches[2]',
'top' // The rule position; either 'top' or 'bottom' (default).
);
}

Remove WooCommerce noindex meta in myaccount page

Here is a solution to remove the meta "noindex" which causes an issue for the myaccount page to be indexed in google, because some people want it to appear for their clients to easy find the login page.
The function match the my-account page and then remove the meta
function remove_wc_page_noindex(){
$url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if ( false !== strpos( $url, 'my-account' ) ) {
remove_action( 'wp_head', 'wc_page_noindex' );
}
}
add_action( 'init', 'remove_wc_page_noindex' );
My question: is there is a way to directly locate the my account page instead of matching part of the url?
You can get more details about the conditional tags here.
/**
* Disable/Enable search engines indexing myaccount pages.
*
*/
function is_wc_page_noindex() {
if ( is_page( wc_get_page_id( 'myaccount' ) ) ) {
remove_action( 'wp_head', 'wc_page_noindex' );
}
}
add_action( 'template_redirect', 'is_wc_page_noindex' );
Since WP 5.7, Woocommerce uses the wp_robots filter. If remove_action( 'wp_head', 'wc_page_noindex' ) did not work for you, then you can try the following:
// Remove WooCommerce noindex meta in cart, checkout and myaccount pages
add_action( 'template_redirect', 'srj_woo_remove_noindex' );
function srj_woo_remove_noindex() {
if ( is_page( wc_get_page_id( 'cart' ) ) || is_page( wc_get_page_id( 'checkout' ) ) || is_page( wc_get_page_id( 'myaccount' ) ) ) {
remove_filter( 'wp_robots', 'wc_page_no_robots', 10 );
}
}

Wordpress/WoOCommerce Changed search-slug: Want to search/show results from specific custom type

Search is set via Relevanssi plugin to only search for the custom type "brands". However, using this code beneath to change the url slug makes it search everything except custom types. How can I alter the code to search specifically in the "brands" custom type?
function change_sok_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/sok/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_sok_url' );
function rewrite_sok_slug() {
add_rewrite_rule(
'sok(/([^/]+))?(/([^/]+))?(/([^/]+))?/?',
'index.php?s=$matches[2]&paged=$matches[6]',
'top'
);
}
add_action( 'init', 'rewrite_sok_slug' );

Categories