Turn jQuery.ajax() request into XMLHttpRequest (vanilla JavaScript)

Here’s my take on it, although a bit late, but I am working on converting all jQuery.ajax() calls to vanilla JavaScript, so why not. // alpha JS request // no jQuery // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send#Example_POST // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest var request = new XMLHttpRequest(); request.open(‘POST’, fpm_ajax.ajax_url, true); request.setRequestHeader(‘Content-Type’, ‘application/x-www-form-urlencoded;’); request.onload = function () { if (this.status >= 200 && … Read more

How to “Load More” posts via AJAX?

You can use ajax to load more posts on your archive page. attach a js/jquery click event on the More link send ajax request to admin-ajax.php (use wp_localize_script to get the ajax url to front end) with page number (track this with js variable). handle ajax request in php. Add custom ajax actions with add_action( … Read more

Plupload in metabox – AJAX action not working in Class

Aha, the problem as that I was calling the wp_ajax_plupload_action from within my page conditional check like so: function __construct() { global $pagenow; $pages = array(‘post.php’, ‘post-new.php’); if (in_array($pagenow, $pages)) : add_action(‘add_meta_boxes’, array($this, ‘dhf_video_meta_box’)); add_action(‘admin_print_scripts’, array($this, ‘video_meta_js’)); add_action(‘admin_head’, array($this, ‘plupload_admin_head’)); add_action(‘admin_print_scripts’, array($this, ‘video_meta_css’)); add_action(‘wp_ajax_plupload_action’, array($this, ‘plupload_action’)); endif; } You don’t want to do it like … Read more

Vue.js + AJAX Shortcode

My solution was simpler than I thought…. Since I’m just trying to execute shortcodes in the frontend, I can just localize the script and pass the shortcodes as variables: functions.php wp_localize_script( ‘BUILD-script’, ‘sharedData’, array( ‘ajax_url’ => admin_url( ‘admin-ajax.php’ ), ‘mailchimpForm’ => do_shortcode(‘[mc4wp_form id=”100″]’), ‘contactForm’ => do_shortcode(‘[contact-form-7 id=”98″ title=”Contact form 1″]’) ) ); Basically I’m jus … Read more

Quick Edit: Selected Custom Taxonomy Not Refreshing After Save

Added this code to updated the selected field by identifying the appropriate value in the taxonomy column: jQuery(document).ready(function($) { // Create a copy of the WP inline edit post function var $wp_inline_edit = inlineEditPost.edit; // Overwrite the function with our own code inlineEditPost.edit = function( id ) { // Call the original WP edit function … Read more

Load custom formatted comment with AJAX: reply link isn’t rendered?

It appears you can get max_depth from the options via get_option(‘thread_comments_depth’): function prefix_submit_ajax_comment(){ $comment = wp_handle_comment_submission( wp_unslash( $_POST ) ); …handle errors and get $comment_depth… $GLOBALS[‘comment’] = $comment; $GLOBALS[‘comment_depth’] = $comment_depth; $args[‘max_depth’] = get_option( ‘thread_comments_depth’ ); // solution op_format_comment($comment, $args, $comment_depth); }

How to securely add an Ajax button to a WP Admin page?

In this answer I created 2 buttons which are visually disabled and get an invalid nonce before submitting. <?php /* Plugin Name: Ajax Example */ add_action( ‘admin_notices’, ‘my_action_button’ ); define( ‘MY_ACTION_NONCE’, ‘my-action-‘ ); function my_action_button() { $id = 4321; $data = array( ‘data-nonce’ => wp_create_nonce( MY_ACTION_NONCE . $id ), ‘data-id’ => $id, ); echo get_submit_button( … Read more

Error 400 bad request using admin-ajax.php

The hook prefix is wrong — it should be wp_ and not admin_: add_action( ‘admin_ajax_nopriv_pedido_clientes’, ‘enviar_pedido_clientes’ ); add_action( ‘admin_ajax_pedido_clientes’, ‘enviar_pedido_clientes’ ); So the correct code is: add_action( ‘wp_ajax_nopriv_pedido_clientes’, ‘enviar_pedido_clientes’ ); add_action( ‘wp_ajax_pedido_clientes’, ‘enviar_pedido_clientes’ );