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

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/"

Related

Remove a div with ID from the_content WordPress

I was trying to remove a div having some ID from the_content WordPress.
I am trying to achieve something like this
jQuery( "#some_id" ).remove();
but on server side,
I don't have a clue how I can do this on server side within the_content filter hook.
This is something I like to achieve,
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$content.find('#some_id').remove();
return $content;
}
I think this is the answer for your problem: https://stackoverflow.com/a/1114925/7335278
In your case this would look like:
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$doc = new DOMDocument();
$doc->loadHTML($content);
$element = $doc->getElementById('some_id');
$element->parentNode->removeChild($element);
$content = $doc->saveHTML();
return $content;
}
hth, let us know if this will work.
cheers, joel
The code is not tested. Just wrote for you... check this code... if this gives any syntactical error plz fix and let me know. But inside the content hook HTML/jsscripts are not running so you can try something like the following --
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
$content = preg_replace("(<([a-z]+id=\"some_id\")>.*?</\\1>)is","",$content);
return $content;
}
Sorry for the previous answer.
I saw to late that what I answered was not what you were after.
Here something that might work.
$str = "<html><head></head><body><span id='remove_this'>This is one
text</span><br /><span>this is a text after that</span></body></html>";
$id_pos = strpos($str, 'remove_this');
$tmp_str = substr($str, ($id_pos-10), (strpos(substr($str, ($id_pos-10)), '</span>' )+7));
$str = str_replace($tmp_str, '', $str);
echo $str;
in the example above I start with searching for where the id is located in the string.
Than I do a substring function to get the start of the html element, in this case being the span.
Than in the same substring call I search for the first closing span tag and add 7 to the position to include the closing span tag.
than I replace the result of the substring in the original string and you get the result you are looking for.
I know there are always better ways but this is one :)
NOTE: This example of mine does use set amount of distance between the id position and the start of the tag as it directly after the start.
So there are most likely much better ways to get to the first instance of the < in php but I don't know it yet.
Hope this might get you a bit further.
I could also have gone into regular expressions but this is easier although all but the best way.
Regular expressions on the other hand are a lot more complex and I'm not really familiar with that stuff.
Cheers
and again sorry for the wrong answer at first.

Strip a part of an URL (qtranslate / 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?

WooCommerce Registration Shortcode - Error messages problems

I am currently creating a widget to display the registration form on a WordPress website that uses WooCommerce. For now, I only have 3 basic fields which are email, password, repeat password. I'm looking forward to add more WooCommerce fields, but want to solve that problem before jumping to the next step.
I'm having some problems with the messages output (wrong password, account already exists, etc).
I searched on the web and there was no shortcode already built for WooCommerce registration, beside their registration page. So I went ahead and created a shortcode, with a template part.
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
This is a snippet I use to get the template part inside a variable, since the default function would "echo" the content instead of "returning" it.
function load_template_part($template_name, $part_name=null) {
ob_start();
get_template_part($template_name, $part_name);
$var = ob_get_contents();
ob_end_clean();
return $var;
}
So, the problem is, when I call woocommerce_show_messages or $woocommerce->show_messages(); from my template part, nothing is showing, or if it is, it shows at the top of the page.
I did try to put the calls inside my shortcode function:
function custom_register_shortcode( $atts, $content ){
global $woocommerce;
$woocommerce->show_messages();
$form = load_template_part('framework/views/register-form');
return $form;
}
add_shortcode( 'register', 'custom_register_shortcode' );
Doing so, the message output inside the <head> tag, which is not what I want.
I tried to do the same trick with ob_start(), ob_get_contents() and ob_clean() but nothing would show. The variable would be empty.
I also did try to hook the woocommerce_show_messages to an action as saw in the core:
add_action( 'woocommerce_before_shop_loop', 'woocommerce_show_messages', 10 );
For something like:
add_action( 'before_registration_form', 'woocommerce_show_messages');
And I added this in my template-part:
<?php do_action('before_registration_form'); ?>
But I still can't manage to get the error messages show inside the box. It would always be inserted in the <head>
I will share final solution when everything is done.
Thanks for your time,
Julien
I finally got this working by hooking a custom function to an action which is called in my header.php
I guess hooking functions inside template part does not work as intended.
In header.php, I got this:
do_action('theme_after_header');
And here's the hooked function. Works perfectly.
function theme_show_messages(){
woocommerce_show_messages();
}
add_action('theme_after_header', 'theme_show_messages');
However, I will look into 'unhooking' the original show message function since it might show twice. Need to test some more ;)
You can also just use the [woocommerce_messages] shortcode in your template where you want it displayed
Replying to a bit of an old question, but you can also try the following:
$message = apply_filters( 'woocommerce_my_account_message', '' );
if ( ! empty( $message ) ) {
wc_add_notice( $message );
}

Disable caching in WordPress Feeds generation

i'm working on a plugin that adds another parameter to feeds, i want to add a numberOfItems on the url and the feed returns that number of articles. I dont want to use the builtin option from WP Admin because the feed will be added on other websites with different number of items, it's a little bit complicated, the point is i need this implementation. i've added something like
function _my_custom_option( $option )
{
global $wp_query, $wp_rewrite;;
remove_filter( 'pre_option_posts_per_rss', '_my_custom_option' );
//$number = get_query_var('number');
$number = $wp_query->query_vars['numberOfItems'];
if(isset($number))
$option = $number;
else
$option = 10;
//$wp_rewrite->flush_rules();
add_filter( 'pre_option_posts_per_rss', '_my_custom_option' );
return $option;
}
add_filter( 'pre_option_posts_per_rss', '_my_custom_option' );
It seems that the feed is cached somehow and doesnt generate with the number of items, because when i add paged=2, it works. But if i change on page=2 numberOfItems with another number, it doesnt change. Plus, i added some junk text in wp-includes >feed-rss2.php just to verify if it cached or not. And it doesnt show after the first 2-3 page refreshes.
To be honest, i'm stuck, i don't know how to approach this, i've looked on the wordpress code and i dont see where the caching is done.
What about this?
function do_not_cache_feeds(&$feed) {
$feed->enable_cache(false);
}
add_action( 'wp_feed_options', 'do_not_cache_feeds' );

Woocommerce Filter Hooks

I'm hoping to get a little insite for using filters in Woocommerce. My main question is, what am I looking for in the template files? or what are the variables that can be targeted using filters? If we look at the filters list I see filter name and files. Using this filter
single_product_small_thumbnail_size
Files - product-thumbnail.php and woocommerce-template.php
What am I looking for in those files that can be targeted and changed? Would you give me a simple example? Maybe something like change thumbnail size?
add_filter('filter_name', 'your_function_name');
function your_function_name( $variable ) {
// Your code
return $variable;
}
I understand what each part of the function and filter are, but I'm not sure what code to write for "Your code." What variable am I grabbing from the file? How do I apply the change? I can't completely wrap my mind around this. Any help would be greatly appreciated.
Thanks,
~MK
As you can see in e.g. woocommerce-template.php you can filter the shop_catalog string:
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
That string is used in the subsequent code to determine the image size to be used:
$image = wp_get_attachment_image_src( $thumbnail_id, $small_thumbnail_size );
So, if you would like to use another image size, you can filter the string like e.g.:
add_filter( 'single_product_small_thumbnail_size', 'my_single_product_small_thumbnail_size', 25, 1 );
function my_single_product_small_thumbnail_size( $size ) {
$size = 'large';
return $size;
}

Resources