How to make comments work for a post loaded per Ajax?

To quote the Codex on the have_comments function: This function relies upon the global $wp_query object to be set – this is usually the case from within The Loop The problem is that your ajax handler creates its own WP_Query object. Note that you are not calling the_post(), instead you are calling $posti->the_post(). Same logic … Read more

Using jQuery to delete data stored in wp_options

Ajax in WordPress works by sending an HTTP post to /wp-admin/admin-ajax.php (by default) that then fires the corresponding hook. So, you attach some jquery to an event triggered by your delete button, which then posts to admin-ajax.php, which has an action, say, delete_my_options(), which actually runs the php to delete. Then, you have a function, … Read more

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

ajax stopped working when not logged in?

Edit: I’ve kept my original answer below, however, I’m not sure what I was thinking… You should never need to trigger do_action( ‘wp_ajax…’ ). While I can’t be sure what the issue is, is the code in the question is roughly ok (I think $_POST should be $_GET with .getJSON). Try putting this at the … 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 would I get a taxonomy/category list inside a Gutenberg block?

Load the elements into a constant using a function like this: const postSelections = []; const allPosts = wp.apiFetch({path: “/wp/v2/posts”}).then(posts => { postSelections.push({label: “Select a Post”, value: 0}); $.each( posts, function( key, val ) { postSelections.push({label: val.title.rendered, value: val.id}); }); return postSelections; }); Then use postSelections as your element “options”. el( wp.components.SelectControl, { label: __(‘Select … 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