2

I'm working with a service that automatically registers my user's devices with Onesignal.

I call the function on login by calling gonative_onesignal_info(); inside script tags (full function will be below). That registers devices perfectly fine with Onesignal.

Now, according to the service's documentation, I can POST it to my server via AJAX, which is what I'm struggling with. From the documentation for the service, if you call gonative_onesignal_info() like this:

function gonative_onesignal_info(info) {
    console.log(info);
}

... info will look like this:

{
    oneSignalUserId: 'xxxxxxx',
    oneSignalPushToken: 'xxxxxx',
    oneSignalSubscribed: true,
}

And here's my full function:

function onesignal_mobile_registration( $user_login, $user ) {

    // Get user data
    $user_id = $user->ID;
    $user_email = $user->user_email;

    ?>

        <script>
            gonative_onesignal_info(info);
        </script>

    <?php

    $oneSignalPushToken = ???;
    update_user_meta( $user_id, 'oneSignalPushToken', $oneSignalPushToken);

}
add_filter( 'wp_login', 'onesignal_mobile_registration', 10, 2 );

So, how can I extract the oneSignalPushToken from that Javascript option, and store it in $oneSignalPushToken to be saved to my user? I think I need to use AJAX to pull it out, right? How would I do that?

1
  • In which platform you are working on? Commented Dec 5, 2019 at 10:22

2 Answers 2

3
+200

You can't assign a php variable from javascript because php run in server but javascript run in browser. You must get $oneSignalPushToken value from a php source OR call a ajax from browser when send js data to php variable:

Script place:

<script>
    var data = gonative_onesignal_info(info);
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('GET', 'test.php?oneSignalPushToken=' + data.oneSignalPushToken, true);
    xmlhttp.send();
</script>

test.php

function onesignal_mobile_registration( $user_login, $user ) {

    // Get user data
    $user_id = $user->ID;
    $user_email = $user->user_email;

    $oneSignalPushToken = $_GET['oneSignalPushToken'];
    update_user_meta( $user_id, 'oneSignalPushToken', $oneSignalPushToken);

}
add_filter( 'wp_login', 'onesignal_mobile_registration', 10, 2 );
Sign up to request clarification or add additional context in comments.

Comments

1

It is important to understand what your code does:

  • The PHP part will render an HTML page
  • The JavaScript part will execute in the browser once the page is fully rendered and served

That means you won't be able to retrieve a JavaScript variable in the PHP thread that generates the page, for two main reasons:

  • JavaScript and PHP execution contexts are not shared at all
  • JavaScript will execute once the PHP thread has ended

What you have to do is expose an endpoint on your PHP server, let's say POST on a /token URL. You will call this endpoint from your JavaScript with some code like fetch('/token', { method: 'POST', body: info });, then retrieve the token from the $_POST global variable, and then execute your update_user_meta( $user_id, 'oneSignalPushToken', $oneSignalPushToken); line

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.