ThemeFuse / Unyson

A WordPress framework that facilitates the development of WP themes
http://unyson.io
926 stars 220 forks source link

Ajax In Custom Shortcode Is Not Working #3878

Closed andreistez closed 4 years ago

andreistez commented 4 years ago

Hello. I have my own shortcode in '/theme/framework-customizations/...', and I need to get some info on frontend via ajax in it. But I'm always getting '400 Bad Request' code... All is working perfect if PHP function and actions replace to theme functions.php. Can anyone help me?

So, JS:

productId = $( this ).attr( 'data-id' );
var data = {
    action        : 'show_more_product_info',
    product_id : productId
};
$.post( cwpAjax.ajaxurl, data, function( data ) {
    // check data
} );

PHP:

add_action( 'wp_enqueue_scripts', 'cwp_products_slider_ajax', 99 );
function cwp_products_slider_ajax() {
    $uri = fw_get_template_customizations_directory_uri( '/extensions/shortcodes/shortcodes/cwp-products-slider' );
    wp_enqueue_script( 'cwpps-js', $uri . '/static/ajax/functions.min.js', array( 'jquery' ) );
    wp_localize_script( 'cwpps-js', 'cwpAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}

/**
 * Show product more info fields in Products Slider.
 */
add_action( 'wp_ajax_show_more_product_info', 'show_more_product_info' );
add_action( 'wp_ajax_nopriv_show_more_product_info', 'show_more_product_info' );
function show_more_product_info() {
    // do something
}
andreistez commented 4 years ago

Solved here, sorry! https://github.com/ThemeFuse/Unyson/issues/1125