AJAX not Working with php as supposed

I am creating a like system in PHP . My code looks like this . <?php class Like_System { private $userid; private $postid; private $user_ko_like_count; private $post_ko_like_count; private $user_ko_dislike_count; private $post_ko_dislike_count; private $user_ip; public function __construct(){ } public function our_ajax_script(){ wp_enqueue_script( 'sb_like_post', get_template_directory_uri().'/data/js/post-like.min.js', false, '1.0', 1 ); wp_localize_script( 'sb_like_post', 'ajax_var', array( 'url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'like_system' ) ) ); } public function load_our_ajax_script(){ add_action( 'init', array($this,"our_ajax_script") ); add_action( 'wp_ajax_nopriv_like_system',array($this,"like_dislike_kernal")); add_action( 'wp_ajax_like_system',array($this,"like_dislike_kernal")); } public function verify_nonce($nonce){ if …
Category: Web

How to Use JSON With AJAX?

I've defined a PHP callback function to handle the communication between server-side and client-side: function json_render_modal_body() { check_ajax_referer( THEME_PREFIX . '-modals', 'nonce' ); $response = array( 'text' => 'no' ); $modal_id = null; if ( isset( $_GET['modal_id'] ) && ! $modal_id = filter_var( $_GET['modal_id'], FILTER_SANITIZE_STRING ) ) { wp_send_json_error( $response ); } if ( ! $form = render_form( $modal_id ) ) { wp_send_json_error( $response ); } $response['text'] = 'yes'; wp_send_json_success( $response ); } I've told WordPress about this function, to …
Category: Web

WordPress Export table as csv using Ajax

Hello guys i'm trying to export my plugin table in the same page, i tried to do hidden iframe but it's still not works, can you kindly check my code please? thanks. PHP: add_action( "wp_ajax_export_plugin", "export_plugin" ); //update_records is action add_action( "wp_ajax_nopriv_export_plugin", "export_plugin" ); function export_plugin(){ global $wpdb; $wpdb->show_errors = TRUE; $wpdb->suppress_errors = FALSE; $plugin_name = $_POST['plugin-name']; // PLUGIN NAME $plugin_export_type = $_POST['export-type']; // PLUGIN EXPORT TYPE as CVS/SQL/XML $plugin_name; // ?? not defined in original code $results = $wpdb->get_results("SELECT …
Category: Web

How to use wp_send_json() and output the value with ajax?

Hello Wordpress Developers. I create my own plugins, and I'm stuck about how to call php functions inside class, into javascript ajax functions. For example we have button with ID name clickme: <button id="clickMe">Click Me</button> And then I make a class with functions, example like this: <?php class myFunctions{ function a(){ $test = "Hello World from wp_send_json()"; wp_send_json($test); } } ?> and then I want to output the test variable, using ajax. Example like this: import $ from "jquery"; class …
Category: Web

Ajax not working to insert, query and result data

On my site, through a form I send/register same information in database, do a SELECT/query and return it! Return the last table saved in database, just that user just entered on the form (along with some more information coming from the database). How I want to display these values coming from databse in a modal bootstrap it's necessary that the page doesn't give the refresh. For this, I inserted the AJAX as follows: $(document).ready(function(){ $('#enviar').click(function(){ $.ajax({ //CAAL AJAX IN WORDPRESS …
Category: Web

WP Cron as Fast as WordPress AJAX?

I have a question about performing a very long and resource intensive task within WordPress. Here is the scenario. I have a plugin that allows users to run a task that takes a very long time and once completed will return the result back to the user. The first thing I did was run the task via WordPress AJAX functions. This did not work since the site locked up to that user. Meaning that if the task was started then …
Category: Web

Shortcodes not working in an AJAX call

I am using AJAX to load next set of posts on homepage. The posts load fine but the shortcodes are not rendered. I am trying to use the do_shortcode function on the content the ajax call fetches (although experts have suggested not to use do_shortcode) but apparently it is failing. Here's my function that processes the request: function theme_load_more_posts() { check_ajax_referer( 'theme-load-more-posts-nonce', 'nonce' ); $args = isset( $_POST['query'] ) ? array_map( 'esc_attr', $_POST['query'] ) : array(); $args['post_type'] = isset( $args['post_type'] …
Category: Web

AJAX action through direct link

I have an existing AJAX action that's already defined by the theme I'm using. The action deletes a search item request, and I'd like to use it as a direct link - like an Unsubscribe link - emailed to the user along with the search results... Something like the following URL: https://domain.tld/wp-admin/admin-ajax.php?action=delete_saved_search_item=&search_item_id=17 but I get a 0 on the screen and a 400 Bad Request in the Headers. Basically in the JS it's defined like this: $.post(ajaxurl, { action: 'delete_saved_search_item', …
Topic: ajax Wordpress
Category: Web

Get posts by category via ajax

In my page-members.php I output my list of categories <?php $args = array( 'orderby' => 'slug' ); $categories = get_categories($args); foreach ($categories as $category) { echo '<a href="' . get_category_link($category->term_id) . '"><div class="category-block">' . $category->name . '</div></a>'; } ?> I'd like for a click on those links to fill the right column with the posts of that category. I have a js file loaded for this page; my functions.php: if (is_page()) { global $post; if ($post->post_name == 'members') { wp_enqueue_script('jquery', …
Topic: ajax Wordpress
Category: Web

How can I run two AJAX requests simultaneously in WordPress?

Kinda stuck here working with AJAX and trying to show posts based on user interaction. Right now I have it working where user makes a category dropdown selection and the posts load accordingly. What I would like to do is take it one step further and populate another drop-down with subcategories based on the parent category that was selected. View the site here. jQuery $('#ajaxFilter select').on('change', function(event) { if(event.preventDefault) { event.preventDefault(); } $this = $(this); $value = this.value; $page = …
Topic: ajax Wordpress
Category: Web

how load content as pop-up using ajax

There's a way to use ajax to load the content from single.php or a custom .php like ajax-single.php like this page does it: https://yellowimages.com/all/objects/apparel-mockups/ also like them show the URL from the post when you click on the link, I hope somebody can help me with this because I can't find a way to do it yet. EDIT 1: I use the next code on my index.php to load the post titles with the permalink: <div id="single-post-container"></div> <div class="wrap"> <?php …
Category: Web

Display full WordPress post under search form using AJAX

Hi I am currently creating a site that shows interactions between different pharmaceutical drugs. I originally started with Live search on JSON Objects Data Using jQuery (originally based on this article: https://www.js-tutorials.com/jquery-tutorials/live-search-json-objects-data-using-jquery/ but modified slightly below) however the client must be able to add constant content and a JSON file was too difficult for them to edit. I am now creating a page for each drug as a WordPress post (and perhaps use a tag to distinguish it for search) …
Category: Web

Ajax call from Plugin using Class

I'm making a plugin with a class and methods. This class gets called through the theme ( hard coded ) to enqueue and localize my js. it also prepares it for ajax. However, i keep getting ERROR: 400 POST http://www.devsite.local/wp-admin/admin-ajax.php 400 (Bad Request) I've ran ajax outside of a class in multiple plugins and at the functions.php with no issues, but i can't get it to work inside a class. This is my code: class CCYTFeatured { public function __construct(){ …
Category: Web

wp_query->max_num_pages always returns 0 on custom post type

Hi there I am developing a theme where I have to load some posts with ajax, in my index page everything works properly, the problem is when I need to use it on custom post type, I pass the values for a jquery file using wp_localize_script, it pass the correct value when I have regular posts, but it always pass 0 when I have a custom post type. Can you help me? Many thanks. Code to pass arguments to my …
Category: Web

change attachment custom field onChange event

The form is in post creating window. Ajax is posting values, but the values doenst renews. Where can be problem ? Ajax <script> function dynamic_Select(field, aid, value) { console.log(field, aid,value); jQuery.ajax({ type: "POST", url: ajaxurl, data:{ field: field, aid: aid, value: value, }, error: function(){alert('Error!')}, success: function(){alert(value)} }); } </script> functions.php add_action('wp_ajax_dynselect', 'update_dynamic_select'); add_filter("attachment_fields_to_save", " update_dynamic_select", null , 2); function update_dynamic_select($field, $aid, $value) { update_post_meta($aid, $field, $value); } HTML: <select aid="104" name="_image_matmenys" onchange="dynamic_Select(this.getAttribute('name'), this.getAttribute('aid') ,this.value)"> <option value="10x10">10x10</option> <option value="20x20" selected="selected">20x20</option> …
Category: Web

AJAX success response is not working but it's saving my changes

I'm a beginner in jQuery-Ajax so please bear with me. My Ajax response does not seem to load but my changes are being saved. but gives me this error on admin-ajax.php "call_user_func() expects parameter 1 to be a valid callback, function '_update_post_ter_count' not found or invalid function name" Here is my function, could you point what i'm doing wrong? add_action( 'wp_ajax_nopriv_save_sort', 'ccmanz_save_reorder' ); add_action('wp_ajax_save_sort','ccmanz_save_reorder'); function ccmanz_save_reorder() { //checking //verify user intent check_ajax_referer( 'wp-job-order', 'security' ); // this comes from wp_localize_script() …
Category: Web

redirect does not work in ajax function

I'm using ajax to post the form data. In the end I want to redirect to homepage. I'm trying following code, it does not work. It returns error 302. How can I redirect in the following function? Thanks. add_action('wp_ajax_nopriv_custom_register', 'custom_register'); add_action('wp_ajax_custom_register', 'custom_register'); function custom_register(){ //process wp_redirect( home_url() ); exit; }
Category: Web

How to pass array from jQuery to PHP with AJAX in WordPress?

I have a HTML form with 2 select boxes with multiple selection. I get the selected values on form submit with jQuery and store them in 2 different arrays: $('#assign_categories_form').on('submit', function (event) { event.preventDefault(); var documents = []; $.each($("#documents_auth_select option:selected"), function(){ documents.push($(this).val()); }); if (documents.length === 0) { $('#modal_message_title').html( 'Nessun documento selezionato...' ); $('#modal_message_p').html( 'È necessario selezionare almeno un documento a cui assegnare delle categorie!' ); $('#modal_message_div').modal(); return false; } var categories = []; $.each($("#dl_categories option:selected"), function(){ categories.push($(this).val()); }); if …
Category: Web

jQuery ajax method does not return data

I am sending email with phpmailer, I receive the emails when I press the "send" button, but I wanted to stay on the same HTML page, so I used AJAX script with HTML and PHP. The problem is that I am not getting the JSON data back that I need to update my HTML page with the email status and PHP reponse, so : response_array status response_array message response_array success Thank you for your help AJAX IN HTML <script> $("#form").submit(function(e) …
Category: Web

Ajax 400 error when used inside a plugin

I'm really, really stuck. When I run this code I get a checkbox displayed. When I click the checkbox, the javascript is run. I get "test" in the console, followed by a 400 POST error. I can get this to work perfectly if I take it out of the plugin (and remove $this from the ajax actions), but I don't want to do that. Please can someone point me in the right direction! This is the code from my plugin …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.