Enqueue script in header

You just have your arguments lined up wrong. Change the line to wp_enqueue_script( ‘ajax-products’, plugins_url( ‘/js/ajax.js’, __FILE__ ), array(‘jquery’), ‘1.2’, false ); You can use plugins_url() with the second parameter of __FILE__ to get the directory of the specific pluign, and in the first argument you pass the relative location of your script. The rest … Read more

Create Page With wp_insert_post() and AJAX

Ok, so after some fiddling, I’ve got things working. It turns out that I was setting the add_action hooks incorrectly for the AJAX part. The hooks need to be: add_action( ‘wp_ajax_nopriv_MYFUNCTION’, ‘MYFUNCTION’ ); and add_action( ‘wp_ajax_MYFUNCTION’, ‘MYFUNCTION’ ); …in my case and original code the function (‘MYFUNCTION’) in question was to create a post via … Read more

WordPress Ajax Callback

It looks odd to me that your ajax_shortcode_function() registers and enqueues your ajax script. According to the codex, you should register/enqueue in functions.php. Additionally, if you only need the url to ajax-admin.php, the tutorial says you can use the global ajaxurl variable and therefore you don’t need to localize your script. The other thing that … Read more

doing an ajax request always outputs 0

Your custom functions should always go in your theme’s functions.php file (if they are not within plugin territory). You should never try to modify any core files outside the wp-content folder (except wp-config.php) as they will be overridden by future updates. Now, there are 2 ways to do what you want. 1 – Admin-ajax This … Read more

wp_insert_post() is returning the correct post ID, no failure, but the post content does not get updated

Don’t bother fixing your AJAX callback, there’s already a REST API endpoint that’s well tested and does all of this out of the box: example.com/wp-json/wp/v2/zen_page So lets enqueue a helper script to give us the URL and a security token/nonce: wp_enqueue_script( ‘wp-api’ ); You will must enable the REST API for your custom post type … Read more

How to Fetch values from WP database based on selected option value by onchange using PHP?

You can use below example to use Ajax in wordpress. This article explains it quite nicely. Main jquery function to use wp_enqueue_script( ‘ajax-scripts’, url-to-your-file/plugin-ajax.js’, __FILE__ ) , array( ‘jquery’ ), ‘1.0.0’, true ); jQuery(function($) { $(document).ready(function(){ $(‘#mySelect’).on( ‘change’ , function(){ var newValue = $(this).val(); $.ajax({ type: ‘POST’, url: ajaxurl, // use ajax_params.ajax_url if using in … Read more