2

Hello am trying to print the user id inside an JavaScript

<script type="text/javascript">
  Li.re('ab12','$user_id');  
  </script>

i wanna output like

<script type="text/javascript">
  Li.re('ab12','12');  
  </script>

12 is the number of the login in user.

Is possible inside shortcode?

<?php echo do_shortcode('[mycred_link href="'.get_field('incentivised_url', $taxonomy . '_' . $termId).'subid1='.$get_current_user_id.'[/mycred_link]'); ?>

The .$get_current_user_id. inside the shortcode not works.

Any help?

3
  • Li.re( 'ab12', '<?php echo get_current_user_id(); ?>' ); Commented May 2, 2019 at 23:09
  • Thank you @SallyCJ It works :) Commented May 2, 2019 at 23:13
  • @SallyCJ can you post answers as answers instead of using the comments? Commented May 2, 2019 at 23:50

2 Answers 2

3

You can use get_current_user_id() to get the ID of the current user, and in your case, you can do so to print the user ID in the JavaScript Li.re() call:

<script type="text/javascript">
  Li.re( 'ab12','<?php echo get_current_user_id(); ?>' );
</script>

And yes, you can include the user ID as part of a shortcode: (note that I omitted the href parameter for simplicity)

<?php echo do_shortcode( '[mycred_link subid1="' . get_current_user_id() . '"][/mycred_link]' ); ?>

PS: Your shortcode was missing the closing ] tag, so I added it above. All shortcode parameter values should also be wrapped in single/double quotes.

4
  • Im confused, what's Li.re? Commented May 3, 2019 at 3:46
  • 1
    Check the question, @Vishwa. It's a method call in JavaScript (just like Li::re() or $Li->re() in PHP). But regarding what the method actually does, you can ask the question author.. Commented May 3, 2019 at 4:55
  • Oh, sorry Sally, I just woke up and got it mixed up with something else. thanks a lot Commented May 3, 2019 at 5:17
  • 1
    No problem, @Vishwa. :) Commented May 3, 2019 at 5:35
2

Another way to approach it, is by using "wp_localize_script()" to create objects from php that you can access from anywhere.

//Get User ID
$user = wp_get_current_user();
$userID = $user->roles[0];

//Enqueue script 
wp_register_script( 'script_handle', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ), '', true );

// Localize the script with new data
$dataArray = array(
    'userID' => $userID,
    'anyother_value' => 'more data'
);
wp_localize_script( 'script_handle', 'object_name', $dataArray );

// Enqueued script with localized data.
wp_enqueue_script( 'script_handle' );

In your separate javascript file you can simply call the object like below.

Li.re( 'ab12', object_name.userID );

And your shortcode call is not working because ".$get_current_user_id." is not a variable but a function. The correct way is ".get_current_user_id().". And I think your calling it wrong. You need to create a new WP_user object.

 <?php
    $user = new WP_User(get_current_user_id());
    $userID = $user->roles[0];
   ?>
   <?php echo do_shortcode( '[mycred_link subid1="' . $userID . '"][/mycred_link]' ); ?>

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.