WordPress AJAX with Axios

you can use FormData an example: let form_data = new FormData; form_data.append(‘action’, ‘myAction’); form_data.append(‘first_name’, ‘my first name’); form_data.append(‘phone’, ‘my phone’); axios.post(myVars.ajax_url, form_data).then(function(response){ console.log(response.data); })

Why not register shortcodes if is_admin dashboard?

I have observed the same issue with contact-form-7 a while back. But note that registering shortcodes based on is_admin is doing_it_wrong (see gmazzap`s answer) There are the two reasons that seem legitimate at first sight (and why they are wrong): (Unlikely) The plugin author tried to optimize the script to only register shortcodes when they … Read more

Initialize TinyMCE editor / visual editor after AJAX insert

Did you manage to get the editor working without your javascript included? If so try to create a template what includes a working tinymce editor and then rewrite your javascript to copy that template using the WithDataAndEvents argument of the jQuery clone() function set to true. For example: $(‘.cloner’).click(function(){ $(‘.container’).append($(‘.template’).clone(true).removeClass(‘hidden’)); });

Open a Thickbox with content trough AJAX

The second parameter for tb_show is the URL, so you’ll want to use something like.. <?php $ajax_url = add_query_arg( array( ‘action’ => ‘getTheContent’, ‘query_var1’ => ‘value1’, ‘query_var2’ => ‘value2’ ), admin_url( ‘admin-ajax.php’ ) ); ?> tb_show(tag, ‘<?php echo $ajax_url; ?>’ ); I’d guess you need to pass the action and any additional query vars manually(as … Read more

How to get a unique nonce for each Ajax request?

Here’s a very lengthy answer of my own question that goes beyond just addressing the question of generating unique nonces for subsequent Ajax requests. This is an “add to favorites” feature that was made generic for the purposes of the answer (my feature lets users add the post IDs of photo attachments to a list … Read more

Why use admin-ajax.php and how does it work?

1) Why use admin-ajax.php instead of encoding your json in a separate file like themes/example/json.php and encode your data there? Using admin-ajax.php means that the WordPress Core is loaded and available. WIthout that, you would need to hand load the files you need, which is a complicated process and prone to failure if you don’t … Read more

Why use wp_send_json() over echo json_encode()?

wp_send_json() handles all parts of returning content in an AJAX call. First off, it sets the content type of the returned content to application/json with the proper charset. Secondly, it automatically calls wp_die() after sending the JSON result, which is necessary in an AJAX call in WordPress. You could consider using wp_send_json_success() for successful requests … Read more

Load minimum WordPress environment

Disabling plugins entirely means you loose many advantages. There are distributions of wordpress that go further and rip out posts and links etc, but they’ll always lag behind WordPress core and tend not to survive as long. Here are some things that could be done Short Init Putting this in your wp-config.php: define( ‘SHORTINIT’, TRUE … Read more