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.
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI'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.
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.