• c_cav

    (@c_cav)


    Okay, so I need to enqueue a script that is dependent on jQuery.

    However the script needs to json_encode(wp_get_current_user()) so it can’t run until after WP is loaded.

    I put an init action in functions in the theme file which does the wp_enqueue_script, which I think should do the trick, however I get Fatal error call to undefined function wp_get_current_user() from the script.

    functions.php

    add_action('init', 'send_cur_user_script');
    function send_cur_user_script() {
    	wp_enqueue_script('curuser', get_template_directory_uri() . '/js/curuser.php', array('jquery') );
    }

    js/curuser.php

    <?php
    	$current_user = json_encode(wp_get_current_user());
    ?>
    // JavaScript Document
    
    var curuser = <?php echo $current_user;?>;
    jQuery(function() {
    	jQuery(curuser.market.join(", ")).removeClass("model-tab-gone");
    });

    the var curuser assignment is not working and throwing an error?

    Any help on how to do this?

Viewing 1 replies (of 1 total)
  • Thread Starter c_cav

    (@c_cav)

    Nevermind…

    Just found localize script…

    That should do it for me.

Viewing 1 replies (of 1 total)

The topic ‘Need help with sending js’ is closed to new replies.