Skip to main content

simplest working ajax code in wordpress

in file anywhere in script tag
var data = {
                            'action': 'staple_dist',
                            'wpcf-preis-km-8-meter-bus': values
                        };
                       
                        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
                        $.post(ajaxurl, data, function(response) {
                           
                            alert(response);
                            if(response === 'success'){
                                alert('Message Sent Successfully!');
                            }
                        }); 


in functions , just replace highlited words

add_action('wp_ajax_staple_dist', array('CVF_staple_dist', 'staple_dist') );
add_action('wp_ajax_nopriv_staple_dist', array('CVF_staple_dist', 'staple_dist') );
class CVF_staple_dist {
    public static function staple_dist() {
        print_r($_POST);
        /* foreach ( $_POST as $key => $value ){
            update_user_meta( get_current_user_id(), $key, $value );
        }
        exit(); */
    }
}


Comments

Popular posts from this blog

add custom post type

/**  *  * add custom post type  *  */ function my_custom_post_product() {   $labels = array(     'name'               => _x( 'Products', 'post type general name' ),     'singular_name'      => _x( 'Product', 'post type singular name' ),     'add_new'            => _x( 'Add New', 'book' ),     'add_new_item'       => __( 'Add New Product' ),     'edit_item'          => __( 'Edit Product' ),     'new_item'           => __( 'New Product' ),     'all_items'          => __( 'All Products' ),     'vi...

Toolset conditional

[wpv- if evaluate= "'[wpv-current-user info='id']' = '[wpv-post-author format='meta' meta='ID']' OR '[wpv-current-user info='role']' = 'administrator'" ]      //the content you want to hide goes here [/wpv- if ]