admin-ajax.php 400 error when filtering blog posts - wordpress

This one is driving me crazy, I've tried lots of solutions on stack but can't seem to get one working with the below.
I'm testing out an ajax filter on some blog posts, but I'm just getting a 400 error "POST /wp-admin/admin-ajax.php 400"
If anybody could shed some light I'd be grateful.
Thank you

I can think only of 3 things:
Your add_actions doesn't get hit in time, remember that Wordpress ajax requests are processed by admin-ajax.php, make sure that your add_action gets called before that
Inside your JS code you used ajax.type = post, that's wrong. I guess you wanted to use ajax.method = POST and eventually dataType = json. You can see the docs here https://api.jquery.com/jquery.ajax/
Your action doesn't return any valid data, return some json, some string, something. Its ok to call die() but well, return something maybe ? Since you are expecting something in ajax callback
P.s. = don't post code as image, we cannot copy easily from there

Apart from the suggestion posted by #Diego, ajax_url should be the full URL, like https://yourwebsite.com/wp-admin/admin-ajax.php

Related

WP API Ignoring parameters

I have a function that loops through my products and creates some json files. Recently this function stopped working correctly.
I tracked it down to the fact the call to the WP API is ignoring the per_page parameter and always grabbing just the first 10.
$response = wp_remote_get($api_url . '/wp-json/wp/v2/products?per_page=50');
I have tried multiple parameters, like adding an offset, nothing works. It grabs the first 10 and that is it.
Has something changed? I tried Googling and I don't find anything relevant. The API documentation says this should work.
If I just put that url with the request in the browser address bar I get the 50.

How to give Httpful a String with double quotes in it?

The idea is to send terms like bob & \\apples to a server. I get this error:
Fatal error: Maximum execution time of 30 seconds exceeded
public function index($request, $response)
{
$uri = 'http://example.org/folder?key=["bob", "\\apples"];
$content = \Httpful\Request::get($uri)->send();
return $content;
}
As user RWC pointed out, it seems to be the library which causes this error.
The mysterious thing is, that it works with only one term (?key="bob") but with two it doesn't. When I put the URL into my browser, I get back the proper results (JSON response) with one and with two terms. So at the end it works but Httpful does something I don't know of yet.
The question you are asking is very specific for the libary you are using, Httpful, and is not a pure PHP question.
This is a valid URL:
https://www.google.com/search?q=\
and so 'http://server/search?key=\' is a valid URL.
If the following command does not work
Request::get($uri)->send()
you have to blame the library (Httpful). You provided a valid URL, so in my opinion if the libary was well written, it should work.
So you have to do something to make it work. What? For that you have to read the documentation of the library.
Good to hear that you find a solution, but is has nothing to do with normal PHP.
ini_set('max_execution_time', 500);
in your script,place this one on the top,its increase the execution time of your code.

Passing arguments to WordPress feed URL

As we all know the WordPress' feed url is www.mysite.com/feed.
I have edited the feed-rss2.php file to show thumbnails if a certain GET parameter is passed. See the code below:
<?php if($_GET['c'] == 'detailswiththumb') echo the_post_thumbnail( array(100,100) ); ?>
But when I open the feed address like this:
www.mysite.com/feed?c=detailswiththumb
The code doesn't work. Can the arguments be passed this way? Am I missing something? Please help.
Firstly, the function is get_the_post_thumbnail() not the_post_thumbnail().
Then, their is one more problem in your code, that you have to pass the post id to get its thumbnail (for more info see http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail).
So, first you will have to extract the post id from somewhere and then only you would be able to get the thumbnail. But, I think it would be very tough, so try giving up this thought, for it would take you a lot of time and no living being is going to access that path.
There were browser cache issues. Even with Google Chrome's incognito window. Had to test it with passing fake arguments like...
www.mysite.com/feed?c=detailswiththumb&fakearguments=123
...to clear the cache. And the code is fine.
Sorry for wasting your time guys.

updated_{$meta_type}_meta not firing, but updated_post_meta is

I'm not sure if I'm using it correctly, but I can't get the updated_{$meta_type}_meta hook to work. There is a updated_post_meta hook which runs when you save a posts meta (and possibly other times, I haven't checked). I can't find much reference to updated_{$meta_type}_meta apart from here, so I don't really understand if I am even hooking it correctly, because I didn't read it properly at first and so thought it should be used like: updated_CPT_meta, but that didn't work, so I tried a meta key instead of the CPT.
My question is, what should $meta_type be ?
Of course I found this straight after I posted
As the page at https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/meta.php#L0 states, $meta_type Type of object metadata is for (e.g., comment, post, or user).
So, you should just use it as updated_post_meta for any CPTs also.
duh.

Wordpress : Url Rewrite

I am trying to rewrite a url in wordpress so that I can serve up dynamic content based on variables that are passed. I have a plug in that needs variable data passed into it. Currently I have:
http://xyzsite.com/page/?var1=something
this works fine and passes in a $_GET var. So my next step is to clean up the variable so that it looks like
http://xyzsite.com/page/something
I have done a few google searches and come accross some site that looked promising but I cannot get any of them to work. From what I have read, i need to use
add_rewrite_tag and add_rewrite_rule
After reading through the articles I have added this to my functions.php page:
add_rewrite_tag('%var1%','([^&]+)');
add_rewrite_rule('^page/([^&]+)/?','index.php?p=1141&var1=$matches[1]','top');
when i navigate to the page http://xyzsite.com/page/something i get a 404 error. When i navigate the to http://xyzsite.com/page/?var1=something it is still working fine. So it looks as if my rewrite is not registering or working correctly.
Can someone help me to achieve the above rewrite. FYI my permalink settings is set to post name if that matters at all. Thank you.
I̶'̶m̶ ̶n̶o̶t̶ ̶a̶ ̶r̶e̶g̶e̶x̶ ̶p̶r̶o̶,̶ ̶b̶u̶t̶ ̶I̶ ̶s̶u̶s̶p̶e̶c̶t̶ ̶a̶n̶ ̶i̶s̶s̶u̶e̶ ̶i̶n̶ ̶y̶o̶u̶r̶ ̶r̶e̶w̶r̶i̶t̶e̶ ̶r̶u̶l̶e̶:̶
a̶d̶d̶_̶r̶e̶w̶r̶i̶t̶e̶_̶r̶u̶l̶e̶(̶'̶^̶p̶a̶g̶e̶/̶(̶[̶^̶&̶]̶+̶)̶/̶?̶'̶,̶'̶i̶n̶d̶e̶x̶.̶p̶h̶p̶?̶p̶=̶1̶1̶4̶1̶&̶v̶a̶r̶1̶=̶$̶m̶a̶t̶c̶h̶e̶s̶[̶1̶]̶'̶,̶'̶t̶o̶p̶'̶)̶;̶
̶
̶N̶o̶t̶e̶ ̶t̶h̶e̶ ̶/̶?̶ ̶y̶o̶u̶'̶v̶e̶ ̶a̶d̶d̶e̶d̶ ̶a̶t̶ ̶t̶h̶e̶ ̶e̶n̶d̶.̶ ̶T̶h̶u̶s̶ ̶y̶o̶u̶ ̶s̶h̶o̶u̶l̶d̶ ̶a̶c̶c̶e̶s̶s̶ ̶y̶o̶u̶r̶ ̶p̶a̶g̶e̶ ̶w̶i̶t̶h̶ ̶h̶t̶t̶p̶:̶/̶/̶x̶y̶z̶s̶i̶t̶e̶.̶c̶o̶m̶/̶p̶a̶g̶e̶/̶s̶o̶m̶e̶t̶h̶i̶n̶g̶/̶?̶ ̶a̶n̶d̶ ̶n̶o̶t̶ ̶x̶y̶z̶s̶i̶t̶e̶.̶c̶o̶m̶/̶p̶a̶g̶e̶/̶s̶o̶m̶e̶t̶h̶i̶n̶g̶.̶ ̶H̶a̶v̶e̶ ̶y̶o̶u̶ ̶t̶r̶i̶e̶d̶ ̶i̶f̶ ̶t̶h̶a̶t̶ ̶w̶o̶r̶k̶s̶?̶
The stroked out text above is wrong, as Gustavo Straube pointed out in the comments. Please disregard that proposed solution.
My only last advice is to try adding a flush_rules(); after your last add_rewrite_rule, as stated in http://codex.wordpress.org/Rewrite_API/flush_rules.
Note that you should be accessing your query vars with get_query_var('var_name') instead of trying to access $_GET directly.

Resources