Articles of ajax

Jquery ajax to custom php file: returning blank data

I created a custom php file, put it in my folder. The files does insert some data to the database to a custom table i created. require wp_path() . “/wp-load.php”; global $wpdb; $favorite_table = $wpdb->prefix . “fav”; $wpdb->insert( $favorite_table, array( ‘link’ => $_POST[‘fav_link’], ‘title’=> $_POST[‘title’]) ); $lastid = $wpdb->insert_id; echo $lastid; function wp_path() { if […]

Add a counter for mouseovers (custom field)

I am trying to count mouseovers on the div for each post. The div is inside the loop. I am almost there, I think you might have to just help me correct some mistake. The div and jquery. Notice that I am using the the_ID() to give the div an CSS id, which this allows […]

WP_query is not returning the expected result

I have 2 dropdown lists, when a user select value in the first list, there is an ajax call to a function using WP_query which send results to populate the second dropdown. My WP_query is not returning any result. I’m new to WP_query so I should have made a mistake somewhere. The query should return […]

i'm trying to get all my media query attachments via ajax in wordpress

i’m trying to load all my media files via ajax: $.ajax({ url: ‘/wp-admin/admin-ajax.php’, type: ‘post’, data: { ‘action’: ‘query-attachments’, ‘post_id’: 0, ‘query[orderby]’: ‘date’, ‘query[order]’: ‘DESC’, ‘query[posts_per_page]’: -1, ‘query[paged]’: 1 }, }); as you can see i’m trying to get ALL my media files: ‘query[posts_per_page]’: -1, in my template, i’m trying to get and display all […]

Call Ajax URL in Plugin

Plugin Class File: function __construct() { add_shortcode(‘user_registration_form’, array($this, ‘shortcode’)); } public function hook(){ add_action( ‘wp_ajax_get_product_serial_callback’, ‘get_product_serial_callback’ ); add_action( ‘wp_ajax_nopriv_get_product_serial_callback’, ‘get_product_serial_callback’ ); } public function product_serial_ajax() { ?> <script type=”text/javascript”> jQuery(document).ready(function(){ alert(‘Hello World!’); jQuery.ajax({ type: ‘GET’, url: “<?php echo admin_url(‘admin-ajax.php’); ?>”, //url: ajaxurl, dataType : “JSON”, data : {action: “get_product_serial_callback”}, //cache: false, success: function(data){ alert(‘Eureka’)’; } […]

PHP session when called wp_ajax_nopriv

I’m trying to work with sessions using wordpress ajax call api, but unsuccessfully. In begining of functions.php writed: if (!session_id()) { session_start(); } When doing ajax call for example to function add_action( ‘wp_ajax_nopriv_compare_objects_request’, ‘compare_objects_request’ ); Session in ‘compare_objects_request’ function is not available. Besides, when in function ‘compare_objects_request’ putting session_start(), then every time when call is […]

Deny dashboard access breaks ajax requests

I need to restrict the access to the dashboard fon non-admin. Therefore i’m using this function in the functions.php: function ggp_restrict_dashboard_access_function() { if (!current_user_can(‘administrator’)){ wp_redirect(home_url()); } } add_action(‘admin_init’, ‘ggp_restrict_dashboard_access_function’, 1); Problem is that because of this, Ajax calls are returning a 301 for non logged in users as well as for logged in non-admin users. […]

Prevent wp_debug from polluting ajax within plugin

I’ve ran into an issue here where wp_debug notices foul up my ajax response. The codex suggests using ob_clean just before returning or echoing: To parse AJAX, WordPress must be reloaded through the admin-ajax.php script, which means that any PHP errors encountered in the initial page load will also be present in the AJAX parsing. […]

Display random text from a file with the WP built-in AJAX API

I want to use a solution by @toscho to display and rotate some quotes. The unchanged code works very well, but if I replace the protected function get_rand() { return rand( 1, 1000 ); } with (from here) protected function get_rand() { $array = file(“wp-content/plugins/quotes-rotator/quotes.txt”); $r = rand(0,count($array)-1); return $array[rand(0,count($array)-1)]; } the code works well […]

click event to unhide something after ajax not firing

I am working in wordpress and my ajax function result showed one button whose html is mentioned below. Now I want to run a click event on that button but when I click it nothing happens. Below is the html and jquery code of the button I am using and kindly note that this button […]