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);

add_shortcode('custom', 'new_shortcode');

Is there another way to do this without printing the data in script tag ?

  • Sorry, I don't understand the question. It looks like everything is working as expected. That's how wp_localize_script() works. It prints a script tag that assigns the values to a global variable that you can access from your script. Jul 19, 2019 at 11:12
  • Thank you for replying me, I want to send the data to js file "custom_js" to handle it without printing it in html <script></script>
    – Hajar
    Jul 19, 2019 at 11:17
  • Well you can't. There has to be JS somewhere that's putting your data into a variable, so it's always going to be accessible. I don't see what the problem with the data being in the HTML Jul 19, 2019 at 11:22
  • Probably because if you remove that line there's nothing to output. Jul 19, 2019 at 11:28
  • is there another way to send shortcode attributes to js file without printing it ?
    – Hajar
    Jul 19, 2019 at 11:30

1 Answer 1


First, Thank you Jacob Peattie for your help.

I decided to solve this issue partly (the data being easily readable) with encoding the data using base64_encode() & json_encode() in php and decoding it in javascript with JSON.parse() & atob().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.