Pass shortcode attributes and extra data to JS with wp_localize_script
-
I wanted to add extra data to shortcode attributes array and pass the array to js with wp_localize_script, the data is added and the array is sent, but it’s also printed as :
<script>var custom = {"status":"new","type":"depend","url":"https:\/\/www.exemple.com"};</script>
My code in functions.php :
function resources() { wp_register_script('custom_js', get_stylesheet_directory_uri().'/assets/js/custom.js', array('jquery'), '', TRUE); wp_register_style( 'custom_css', get_stylesheet_directory_uri() . '/assets/css/custom.css', array(), '1', 'all' ); } add_action( 'init', 'resources' ); function new_shortcode($atts){ if (!empty($atts)) { $atts['url'] = MAIN_URL; wp_localize_script('custom_js', 'custom', $atts); } wp_enqueue_script("custom_js"); wp_enqueue_style("custom_css"); } add_shortcode('custom', 'new_shortcode');
Is there another way to do this without printing the data in script tag ?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Pass shortcode attributes and extra data to JS with wp_localize_script’ is closed to new replies.