Strip a part of an URL (qtranslate / woocommerce) - woocommerce

Is there a way in php to get rid of a specific parameter in an URL?
E.g: I have this link which activates a printing option in Wordpress Woocommerce frontend.
I am using mqtranslate to have two different languages.
...
myaccount/?lang=en%2Fprint-order%2F2067%2F&print-order-type=receipt
but this gives me an error, because there is
/?lang=en
inside of the URL which should be at the end of the URL to work correct like this:
/?lang=en
I have already found the code which produces this - it's inside woocommerce/templates/myaccount/my-orders.php at line 87
$actions = apply_filters( 'woocommerce_my_account_my_orders_actions', $actions, $order );
if ($actions) {
foreach ( $actions as $key => $action ) {
echo '' . esc_html( $action['name'] ) . '';
I tried to change $action into another url, but then printing is not working.
A working url would look like this:
.../myaccount/print-order/2083/?print-order-type=receipt&lang=en
so how could I remove "?lang=en" in the beginning of the url and set it to the end of the url like "&lang=en"?
is there any way to do it in PHP or should I use jquery?
edit: maybe something like
$('td.order-actions a.print').each(function(){
this.href = this.href.replace('?lang=en', '');
could also do the trick maybe, but I am not sure how to remove the "slashes" //.
Would this be the right way to remove lang=en? How can I then put it at the end?
EDIT 2:
Ok I found a solution - but it's not perfect:
I have a problem with jquery syntax in an url link:
this is the link i am getting:
...myaccount/?lang=en%2Fprint-order%2F2067%2F?print-order-type=receipt
I am using
$('td.order-actions a.print').each(function(){
this.href = this.href.replace('/\?lang=en', '');
to remove the "/?lang=en"
the problem now is, that the link looks ok in my browser
myaccount/print-order/2067/?print-order-type=receipt
but actually there's always a "%2F" instead of a "/" inside the link when I copy it which leads to the problem of getting an 404.
When I manually replace "%2F" the link works.
Where is the problem? Any idea to fix this?

Related

wp_login_url() returns relative URL instead of full URL

Trying to debug an issue on my WordPress site where using wp_login_url() both in templates and emails doesn't return the full URL.
It returns "/login/" instead of "https://sitename.com/login"
Any idea how to fix this?
I think something changed your login url if your not getting wp-login.php.
Anyway you can add anything after your site url by using this function
site_url('anything')
for your url
site_url(wp_login_url())
or (I prefer this)
site_url('login')
For adding filter to your login url , you can use this code (I don't suggest this way)
function filter_login_url( $login_url ) {
return site_url( $login_url );
}
add_filter( 'login_url', 'filter_login_url', 10, 3 );

Pretty URLs for named anchors on WordPress page

I'm working on a WordPress site in which the services page is one long page with anchors for each section.
www.mysite.com/services/#human-resources jumps to the Human Resources section as expected.
Is there any way in the .htaccess file to make a url such as www.mysite.com/services/human-resources/ jump directly to the anchor?
I've tried several different rewrite rules, the closest I've gotten is:
RewriteRule services/human-resources services/#human-resources [NE,L,R=301]
But this shows the # in the url which defeats the purpose. Removing the R=301 just causes it to do nothing.
Thanks.
I have been working on a similar site and this is how I solved that issue.
First add this action to functions.php, witch redirects the user to the proper anchor on the page.
function url_handler(){
$path = substr($_SERVER['REQUEST_URI'], 1); //get the path minus the '/'
if( is_page() || is_404() ){ // do you own conditional tags
wp_redirect( home_url("#" . $path) ); //redirect to the #anchor
exit();
}
}
add_action( 'template_redirect', 'url_handler' );
Next you need to make a javascirpt handler witch gets the url of the page (the url with the anchor) and make the proper event to scroll to that section. Something like this:
var hash = window.location.hash;
hash = hash.replace('#','');
hash = hash.replace('/','');
/* do stuff here */
Hope this helps

Change Wordpress feed <link> for one specific tag

I have an external page that reads a RSS feed for a tag. I can't change anything on the external page, so the challenge is to change the RSS feed on my side to match the requirements.
On the external page the 3 latest posts for a tag are shown, and at the end of the section (note: not after each post but after all 3 posts) there is a "View all" link. This link receives its value from the element in the feed, which is by default set to my blog homepage, e.g. http://myblog.com). For this specific tag the link should be http://myblog.com/tag/myspecialtag.
The requirement is that the "View all" link links to the tag page instead of the homepage.
My idea was to add a condition to the element to change the URL for this specific category. I tried to change the feed template as recommended here: Customizing feeds, but for some reason it doesn't change the template at all. The code I tried is the following:
remove_all_actions( 'do_feed_rss2' );
add_action( 'do_feed_rss2', 'change_feed_rss2', 10, 1 );
function change_feed_rss2( $for_comments ) {
$rss_template = get_template_directory() . '/feeds/feed-custom_rss2.php';
if( file_exists( $rss_template ) )
load_template( $rss_template );
else
do_feed_rss2( $for_comments ); // Call default function
}
Of course I created the custom feed template and stored it in the feeds directory in my theme.
As this didn't work I tried looking into filters/hooks but I couldn't find anything helpful regarding my issue. Any ideas on how to best solve this issue?
I came up with the following solution:
I created a new custom page template custom_rss-feed.php and copied the code from wp-includes/feed-rss.php into it. I assigned this template to a new page. Additionally I added a filter to get_bloginfo_rss like the following:
function alter_bloginfo_rss($value, $key) {
if ( $key === "url" &&
is_page_template("custom_rss-feed.php")
) {
return $value . "/my/custom/url";
}
return $value;
}
add_filter("get_bloginfo_rss", "alter_bloginfo_rss", 10, 2);

How to redirect based on URL

I am using Wp Store Locator plugin.And I have Modified it according my need.But I am stuck in redirection.
Basically this plugin works with short code.So at the time of listing my URL is like that : localhost/wordpress/?page_id=391
Now there is one link which is redirects me to http://localhost/wordpress/?wpsl_id=3
Here is code for that :
add_action("template_redirect", 'my_theme_redirect');
function my_theme_redirect() {
$dpath = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"] ;
$templatefilename = 'single-store.php';
if (strpos($dpath,'wpsl_id') !== false){
$return_template = get_template_directory() .'/' .$templatefilename ;
//$nurl = get_permalink($return_template);
//echo $return_template; exit;
wp_redirect($return_template);
exit;
}
}
This code is not redirecting me to any-where.It stays on the same page localhost/wordpress/?page_id=391 What might be the issue?
Anyone can suggest me how can I direct to URL when it detects wpsl_id in the URL ? I want to redirect them to plugin's template folder where I have added a file for that.
EDITED :
I have added above code in the plugin file just above the short code. Hope I has nothing to do with this issue :)
The template name looks fine. You just need to register the template before you call it.
Add this filter so this will execute before your action:
add_filter( 'template_include', 'single-store' );

paginate_links() outputs "paged=" to link instead of "page="

I've been having a sever and completely unsolvable problem with pagination.
I did notice however this interesting point:
I search a keyword like cute:
?s=cute&submit=Search&post_type=image&paged=2
...is where the link leads to. "Paged=" gives 404 on random pages. But if I modify the URL to say page=
?s=cute&submit=Search&post_type=image&page=2
Every page destination works, bringing joy to my heart, but the pagination tab list always sticks at 0 (not reflecting current page).
I feel like I'm playing a "shell game" with code and settings, with wordpress bamboozling me.
Question reduces to:
How can I get &page= to be output in every case? Conversely, if &paged= should be the way it goes, how do I get it to work without 404s?!?!
This is a problem I've dealt with for almost 3 months now. People are shy to answer.
update:
Trying to deal with this paged variable in the URL which breaks pagination, I created a filter to simply replace it with page.
function symbiostock_pagination_mod( $args ){
$args = str_replace('paged=', 'page=', $args);
return $args;
}
add_filter( 'paginate_links', 'symbiostock_pagination_mod', 1 );
Would seem like a great idea! But then this happens with every new click:
?s=cute&post_type=image&page=2&page=2
?s=cute&post_type=image&page=2&page=3
So where is this being modded, and why?
update:
I'm not sure if I'm the only one that has ever had this problem, but I did solve it (at least for myself) you can see the solution below. Also thanks for the help given. :D
So here is what I came up with...we might as well call it a hack or work-around because its unknown if this was indeed a bug I'm dealing with (from wordpress itself) or just a perfectly hidden problem in my theme's code.
The below function filters the 'paginate_links()' function and achieves this:
I see paged variable generates 404 randomly on search pages, but works fine on archive and taxonomy pages. So, we check that. if_search(), we do our changes...
If this is a search, we get the would-be page # destination using regex (or simply string replace paged to page, depending on permalink structure. We use remove_query_var() to drop faulty variable, and add_query_var() to put the working one.
This is being done because in this strange case, page always generates proper results.
Then we return the edited destination link, and life is good.
Function below:
add_filter( 'paginate_links', 'my_search_pagination_mod', 1 );
function my_search_pagination_mod( $link )
{
if ( is_search() ) {
$pattern = '/page\/([0-9]+)\//';
if ( preg_match( $pattern, $link, $matches ) ) {
$number = $matches[ 1 ];
$link = remove_query_arg( 'paged' );
$link = add_query_arg( 'page', $number );
} else {
$link = str_replace( 'paged', 'page', $link );
}
}
return $link;
}
Also thanks to some help I got here with the regular expression: PHP Regular Expression Help: A quick way to extract the 2 from "page/2/"

Resources