Are there any security risks when submitting data-attribute data through AJAX?

When dealing with submit forms, even if they are sent with AJAX, you must play by the Never trust user’s input rule. Every data-attribute can be changed, edited via Inspector. Your only trusted validation should be on the server side, as you did with: if ( isset($_POST[‘author_id’]) || is_numeric($_POST[‘author_id’]) ) Personally, I would inverse the … Read more

echo custom fields with AJAX

I’m not sure Why you used loop to display your post meta. and you code are all ok but you have to change little-bit. Q. Why you used inline script ? On my opinion you should used external script file to do this. anyway that’s not the main issue here. In order to work with … Read more

Why does my jQuery AJAX return 0?

The problem lies here: add_action( ‘wp_ajax_action_varaa_AJAX’, ‘varaa_AJAX’ ); add_action(‘wp_ajax_nopriv_varaa_AJAX’, ‘varaa_AJAX’); Specifically: wp_ajax_action_varaa_AJAX You didn’t replace the word action with varaa_AJAX, it should instead be: add_action( ‘wp_ajax_varaa_AJAX’, ‘varaa_AJAX’ ); add_action(‘wp_ajax_nopriv_varaa_AJAX’, ‘varaa_AJAX’); However, I would recommend instead using the WP REST API and register_rest_route, which should simplify your code and eliminate these kinds of mistakes.

AJAX Load more start with x posts

You need to set the offset in the query. To get the right offset value, you will need to store and send back the offset value to your ajax script (data-offset in the input or an hidden field). $args = array( ‘posts_per_page’=> 10, ‘post_status’=> ‘publish’, ‘offset’=> $_POST[‘offset’] ); $_POST[‘offset’] is coming from your button with … Read more

use jQuery.load() to include a php file in a div, wp_query() is part of php file

Using AJAX in WordPress is fundamentally pretty simple. You need a function that handles the AJAX request, and a couple of add_action calls to map that function to the action you pass to admin-ajax.php, in this case, some_action: function wpd_ajax_function() { get_template_part( ‘my-content’ ); wp_die(); } add_action( ‘wp_ajax_some_action’, ‘wpd_ajax_function’ ); add_action( ‘wp_ajax_nopriv_some_action’, ‘wpd_ajax_function’ ); This … Read more

WordPress Ajax Always returning 0

Try die() function in the end of PHP function. It will helps. UPD: Ive made some simple version of your problem code to check it and it works. I think it will helps you. You are able to change inner PHP script to your logic and it wont returns 0. PHP: function more_post_ajax(){ echo “Hello”; … Read more

Retrieve JSON file from JS trough php

I’m not convinced this is the cleanest way to do it but at least it work until I find something more efficient: function json_gmap(){ $get_file = file_get_contents(get_template_directory_uri() . ‘/data/my.json’); $json_to_array = json_decode($get_file,true); die (json_encode($json_to_array)); } add_action(‘wp_ajax_json_gmap’, ‘json_gmap’); add_action(‘wp_ajax_nopriv_json_gmap’, ‘json_gmap’); Basically I decode my Json file to an array and the re-encode it to have it … Read more