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

How to load wp_editor() through AJAX/jQuery

To get the quicktags to show up, you need to re instantiate them within your ajax oncomplete handler. quicktags({id : ‘editorcontentid’}); My ajax success handler looks like this; success: function(data, textStatus, XMLHttpRequest){ //append editor to dom $(‘#container’).append($(data).html()); //init quicktags quicktags({id : ‘editorcontentid’}); //init tinymce tinymce.init(tinyMCEPreInit.mceInit[‘editorcontentid’]); } I’ve managed to get the editor to load by … Read more

Best way to end WordPress ajax request and why?

Using wp_die() is the best of those options. As others have noted, there are many reasons to prefer a WordPress-specific function over the plain die or exit: It allows other plugins to hook into the actions called by wp_die(). It allows a special handler for exiting to be used based on context (the behavior of … Read more

How to check if I am in admin-ajax.php?

Check the constant DOING_AJAX. Its definition is the first working code in wp-admin/admin-ajax.php. Some very weird plugins, like Jetpack, are defining that constant in unexpected places, so you might include a check for is_admin() as well. Example: if ( is_admin() && defined( ‘DOING_AJAX’ ) && DOING_AJAX ) { // do something } I have asked … Read more

Ajax takes 10x as long as it should/could

Yep, this is nasty issue that to have full WordPress environment you need to spend considerable time loading it. I’ve needed much better performance (for very dynamic incremental search feature) for work and what I went with is: Custom file as Ajax handler. SHORTINIT constant for limited WP core load. Very selectively loaded parts of … Read more

error code: 523