wordpress ajax upload media not working

This looks like the access to admin-ajax.php is forbidden (error 403). For some reason, many security plugins are preventing access to admin-ajax.php, even with this file being essential for WordPress to work correctly. If you use some security plugin, check out if it is maybe blocking admin-ajax.php. Check if admin-ajax.php is blocked in .htaccess file … Read more

wp_send_json erase last value

You don’t need to specify datatype in your AJAX call. Also, WP handles AJAX a bit differently, primarily: $(‘#filter_evenement’).submit(function(){ jQuery.ajax({ url:ajaxurl, type:”POST”, data : {action: “evenements_filter_function”, val1: “value1”, val2: “values2”}, success:function(data){console.log(data); var dataArray = jQuery.parseJSON(data);//parse JSON }, fail: function( jqXHR, textStatus, errorThrown ) { } }); }); data has to contain action which specifies the … Read more

Order result from ajax

So you can use the prepend method: <script> $(document).ready(function(){ var ajaxurl = “<?php echo admin_url( ‘admin-ajax.php’ ); ?>”; var dataArray = <?php echo json_encode($current_posts_ids) ?>; function getMyPosts() { data = { action: “update_news”, data: dataArray}; $.post(ajaxurl, data, function(response) { // reset array dataArray = []; // append the new news posts $(‘.latest-news .posts’).prepend(response); // create … Read more

Should I edit a user meta field with PUT, PATCH, or POST and WP::Editable

Okay, with some great examples from : https://restfulapi.net/resource-naming/ and http://www.restapitutorial.com/lessons/restfulresourcenaming.html I’m going to answer my own question. ESSENTIALLY, for my use case, I will NOT use PUT but rely on POST and DELETE to collection and singleton endpoints. Notes: I’ve omitted the wp-json/my-site-namespace/v1 prepending examples for clarity I’ve used query strings in POST requests but … Read more

How to use add_action(‘wp_ajax_[action name]’,…) for a specific page with condition?

As foo is a static function you don’t need to specify the object, so you can use it outside the condition to correctly load it like this : public function __construct(){ if( isset( $_GET[“page”] ) && $_GET[“page”] === ‘edit-foo’ ){ add_action( ‘admin_init’, array($this, ‘init’) ); } add_action(‘wp_ajax_foo’,array( ‘MyPlugin/Classes/MyClassWithFooFunction’, ‘foo’ ) ); } init(){ … <- … Read more