0

I should attach javascript snipet after user is registered on my wordpress site, i user the user_register hook like this

do_action('user_register', $user_id );
add_action('user_register', 'MY_Callback');
function MY_Callback(){
  $data['user_id'] = $user_id;
  My_print_js($data);
}

Then in my My_print_js function

function My_print_js(){
   global $data;
   echo '<script>';
   echo 'dlayer.push('.json_encode($data).')'
   echo '</script>';
}

With that the script isn't printed after user_register, i have missed something? i note that i user woocommerce too.

Thank you guys and girls for helps :)

1
  • Update your question with what you want to ultimately achieve, as opposed to what you're trying/have tried. Sep 2, 2015 at 12:37

1 Answer 1

1

There's quite a few issues here:

do_action('user_register', $user_id );

No need to fire the action yourself (especially with an undefined variable $user_id) - the idea of actions & filters is that you "hook" your function to one, and then let WordPress trigger it when applicable.

function MY_Callback(){
  $data['user_id'] = $user_id;
  My_print_js($data);
}

Again $user_id is undefined (null) - you need to accept it as a parameter in your function:

function MY_Callback( $user_id ) {

Same issue again with your second function - you call it with a parameter (as above), but then completely ignore it and try to use the global $data - instead you want:

function My_print_js( $data /* Take the argument man! */ ) {
   echo '<script>';
   echo 'dlayer.push('.json_encode($data).')'
   echo '</script>';
} 

Now for the final catch: even if you apply everything above, your code is still unlikely to work, since there's no certainty as to when the user_register action will fire - your code simply prints a <script> tag whenever it runs, with no guarantee that it's within the context of a HTML document, or that it will even print (what if there's a redirect header immediately after?).

I would suggest the following approach, and then test that it works for the scenario in which user_register fires where you need it:

function wpse_200441_user_register( $user_id ) {
    global $wpse_200441_user_register;

    // Store the user ID that just registered in a global so that the script
    // function can access it later.
    $wpse_200441_user_register = $user_id;

    // Hook the script function
    add_action( 'wp_footer', 'wpse_200441_script' );
}

add_action( 'user_register', 'wpse_200441_user_register' );

function wpse_200441_script() {
    global $wpse_200441_user_register;

    if ( ! empty( $wpse_200441_user_register ) ) {
        $json = json_encode(
            array(
                'user_id' => $wpse_200441_user_register,
            )
        );

        echo "<script>dlayer.push( $json );</script>"; 
    }
}
4
  • That's the correct answer. It wasn't clear if the OP wants the js to be printed if the user is logged in or only after the registration for only one time (ie a thank you page and the user for some reason is not logged in after the registration). If it the first we can avoid the global variable and just check if the user is logged in and then take the user id and echo the js.
    – Laxmana
    Sep 2, 2015 at 11:58
  • Thank you guys, i tested the code and it not working, is because woocommerce redirect to my_account page after register complete? or i should not use user_register hook there ? someone has suggested me to use do_action( 'woocommerce_created_customer', $customer_id, $new_customer_data, $password_generated ); but i not know how to use it
    – user44321
    Sep 2, 2015 at 12:19
  • 1
    Would you mind explain better what you really want. You want the js code to be printed to every logged in user ? Only once after the registration and if yes where? Admin panel ? On the site ?
    – Laxmana
    Sep 2, 2015 at 12:24
  • Also tried these method but there are some redirects after and the javascript does not fire I have put a simple console.log and it never fires
    – benraay
    Dec 4, 2019 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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