Prevent / Disable cache in REST Wordpress - wordpress

I would like to know if there is a way to prevent cache in wordpress inside the REST API.I have this minimal example:
add_action('rest_api_init', 'get_wp_page_fn2' );
function get_wp_page_fn2(){
register_rest_route( '/wp/v2/tf_api/' ,'get_wp_page2',
array(
'methods' =>'GET',
'callback' => 'get_wp_page_callback2',
)
);
}
function get_wp_page_callback2( $request) {
$response = [
"response" => 1,
"url" => 33,
];
echo json_encode($response);
die();
}
If I change "response" => test, it takes about 5/10 minutes to update it. It's kind of a nightmare to do dev like that.
I try a lot of stuff but nothing seems to be working

Took me like a day.
Since I'm using Mamp pro, I have to deal with this https://documentation-4.mamp.info/en/MAMP-PRO-Mac/Languages/PHP/index.html

Related

how to make custom wordpress rest api and get posts results with arabic language parameter

I create API route for search
I try to get a parameter search in Arabic containing about (two words)
but it didn't work, I try almost of video, and not working
try Unicode and encode JSON also
Does anybody have a solution for it?
my API route
add_action('rest_api_init', function() {
register_rest_route( 'wm/v1', '/search/(?p<request>.+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'wm_search_post',
) );
enter code here
and the Endpoint callback function
function wm_search_post(WP_REST_Request $request){
$req=JSON_encode($request['request']);
return $req;
// here I try to test the results but get rest_no_route
//404 >>> but it is work if I remove parameter
}}
Your regex expression is not correct and also some of your closing braces missing. Let's see the below code, it's working on my end.
add_action('rest_api_init', function() {
register_rest_route( 'wm/v1', '/search/(?P<request>\w+)/', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'wm_search_post',
));
});
function wm_search_post(WP_REST_Request $request) {
$req= $request['request'];
return rest_ensure_response($req);
}
You can get a response http://localhost/elementskit/wp-json/wm/v1/search/<pass_string_here>
Hope that will work!

Wordpress custom endpoint returns 404

I followed this tutorial to create custom endpoints. But, it always returns 404.
I added code below to function.php
add_action('rest_api_init', function() {
register_rest_route('awesome/v1', '/awesomeparams', array(
'methods' => 'GET',
'callback' => 'get_awesome_params',
'args' => array(),
'permission_callback' => function () {
return true;
}
));
});
function get_awesome_params( $data ) {
return "aaaa";
}
When I trying to access:
http://smap.cas.mcmaster.ca/wp-json/awesome/v1/awesomeparams
It shows 404:
I checked many other posts, but still can't find a solution. Can I get some help?
Thanks!
Did you flush your permalinks?
Go to Wordpress admin -> Settings -> Permalinks and click save, It should flush your permalinks and then It should work.

Wordpress Custom Endpoint shows 404

I am stuck on creating custom endpoints. I followed this tutorial to create custom endpoints. But, it always returns 404.
https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
I added code below to function.php:
add_action('rest_api_init', function() {
register_rest_route('awesome/v1', '/awesomeparams', array(
'methods' => 'GET',
'callback' => 'get_awesome_params',
'args' => array(),
'permission_callback' => function () {
return true;
}
));
});
function get_awesome_params( $data ) {
return "aaaa";
}
When I trying to access: http://www.my-domain.com/wp-json/awesome/v1/awesomeparams
It shows 404:
I also tried to flush the permalinks by going to WordPress admin -> Settings -> Permalinks and click save.
Can I get some help?
Thanks a lot!
The solution is to use the url below:
http://www.my-domain.com/index.php/wp-json/awesome/v1/awesomeparams

wp_cronjob should run a special function

I am creating a new wordpress plugins and I want to use the cronjob system from wordpress. But I get a little bit confused: Everytime the cronjob gets executed I want to run a special function. But the action will not be done. No post will be inserted.
The cronjob itselfs works correct. Maybe I do something wrong. (I manually tested the cron by opening the url wp-cron.php?immo_import_check_import_folders, and it shows me blank page. (Seems this is normal)
Code:
register_activation_hook(__FILE__, 'immo_import_activation');
register_deactivation_hook(__FILE__, 'immo_import_deactivation');
function immo_import_activation() {
wp_schedule_event( time(), 'minutely', 'immo_import_check_import_folders' );
add_action( 'immo_import_check_import_folders', 'immo_import_check_import_folders2' );
}
function immo_import_deactivation() {
wp_clear_scheduled_hook('immo_import_check_import_folders');
}
function immo_import_check_import_folders2() {
$my_post = array(
'post_title' => wp_strip_all_tags( 'Test' ),
'post_content' => 'test .......',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array( 8,39 )
);
wp_insert_post($my_post);
}
Move your add_action out of immo_import_activation function. If you need one time cron action, look into wp_schedule_single_event

WordPress REST API not enabled on new account

Based on a quick research, WordPress REST API should be enabled after v4.7 (https://v2.wp-api.org/), however, I cannot access the REST API for my existing or new user by simply appending {name}.wordpress.com/wp-json, ie. https://steventsaotest.wordpress.com/wp-json does not return the expected JSON. Whereas sites like http://www.tribunemedia.com/wp-json works exactly as I wish, as well as my WordPress instance on Digital Ocean.
How can a user whose blog is registered with wordpress.com enable their REST API?
add_action( 'rest_api_init', function () {
$namespace = 'aroma_api';
register_rest_route( $namespace, '/get_signin', array (
'methods' => 'POST',
'callback' => 'get_singnin_data',
));
} );
function get_singnin_data($data)
{
global $wpdb;
$resultdata = array();
$resultdata['message'] = 'Login SuccessFull';
return $resultdata;
}

Resources