I'm trying to add the current username in an embedded 3rd party shortcode

Here's what I've got:

<?php echo do_shortcode('[table id=2 /]'); ?> 

I'm trying to replace "2" with the current user who is logged in.

Thank you for your help.


1 Answer 1


If it is the user ID, that you need to pass to the shortcode, then you can first use get_current_user_id() to get the ID. After that, use it int the shortcode string. Depending on the shortcode and your exact use case either check, if the ID is 0, or don't.

With check,

// user ID or 0
$current_user_id = get_current_user_id();
// if ID exists
if ( $current_user_id ) {
  // use ID in shortcode string
  echo do_shortcode('[table user_id="' . $current_user_id . '"]');

or without,

echo do_shortcode('[table user_id="' . get_current_user_id() . '"]');

For username, get the current user object with wp_get_current_user() and read the username from the user_login property.

$current_user = wp_get_current_user();
// Existing user
if ( ! empty( $current_user->user_login ) ) {
  echo do_shortcode('[table username="' . $current_user->user_login . '"]');

N.B The shortcode attribute names above are examples. Please check the actual name from the 3rd party's documentation so that you use the correct one.

Your Answer

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

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