最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Print WordPress username id inside JavaScript

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited May 3, 2019 at 0:03 sot asked May 2, 2019 at 22:55 sotsot 451 silver badge9 bronze badges 3
  • Li.re( 'ab12', '<?php echo get_current_user_id(); ?>' ); – Sally CJ Commented May 2, 2019 at 23:09
  • Thank you @SallyCJ It works :) – sot Commented May 2, 2019 at 23:13
  • @SallyCJ can you post answers as answers instead of using the comments? – Tom J Nowell Commented May 2, 2019 at 23:50
Add a comment  | 

2 Answers 2

Reset to default 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.

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]' ); ?>
发布评论

评论列表(0)

  1. 暂无评论