Cannot access $wpdb, comes back NULL

As stated in the Comments, do not access a plugin file directly. Instead, use other means that incorporate the REST API or the included Ajax functionality in WordPress. To learn how to use Ajax in your Plugin, have a look here: Ajax in Plugins

AJAX call to admin-ajax.php by subscriber returns home page

I have also faced same issue, after checking my functions.php codes,I found following code. add_action(‘init’, ‘blockusers_init’); function blockusers_init() { if (is_admin() && !current_user_can(‘administrator’)) { wp_redirect(home_url()); exit; } } This code redirects the ajax call to home page. To resolve this, add DOING_AJAX condition, add_action(‘init’, ‘blockusers_init’); function blockusers_init() { if (is_admin() && !current_user_can(‘administrator’) && !defined(‘DOING_AJAX’)) { … Read more

Get uploaded image and attach it to the new post

You have to use the javascript FormData object to upload files via ajax. A simple tutorial is provided here. In PHP the file will be available via the globals $_FILES and $_POST. Additional to the tutorial the WordPress specific data “action” (and maybe “nonce”) have to be added to the formData object: formData.append(‘action’, ‘add_your_action_here’); // … Read more

Send checkbox status with Ajax / JSON and save it

.prop() method return boolean type. True or false. You should send checked value and save it into database. And in HTML get your value and set it to your checkbox. An example you have <input type=”checkbox” name=”test” class=”checkbox” value=”checked”>. You should send to php $(‘.checkbox’).val(); and then place checked attr to html. <?php $checked = … Read more

Need help with ajax

The WordPress Plugin Developer Handbook has background info and code samples of how to use Ajax in your plugin (and themes). Give it a read and it should get you on the right path.

AJAX button with success callback. (Titan Framework)

So now I want to refresh the page using a success callback. I’ve enqueued a simple script via admin_enqueue_scripts: I believe in that script, you need a global JS function named ajax_success_refresh, as in the following example: // This is the ‘success_callback’ for the AJAX action of ‘yyy_reset_security_logs’. function ajax_success_refresh() { window.location.reload( false ); } … Read more

Output votes to div dynamically in wordpress

[EDIT] Provided better replacements. Try this: In template.php, replace: echo ‘<div id=”vote_counter”>’. $votes.'</div><br>’; ..with: echo ‘<div id=”vote_counter-‘ . $post->ID . ‘” class=”vote_counter”>’. $votes.'</div><br>’; In voteme.js, replace: jQuery(“#vote_counter”).html(response.vote_count); ..with: jQuery(“#vote_counter-” + post_id).html(response.vote_count); You should also declare post_id and nonce as local variables; i.e.: var post_id = $(this).attr(“data-post_id”), nonce = $(this).attr(“data-nonce”); Hope that helps.