Support » Fixing WordPress » Do shortcode and usernames

  • Resolved miketheteacher

    (@miketheteacher)


    Hello friends

    I am trying to add the username of the currently logged in user into the shortcode in place of XXXX.

    <?php echo do_shortcode("[listyofiles folder='/file/XXXX/']");?>

    The do_shortcode works perfectly with the relative path, but I am struggling with adding a username.

    Is this even possible?

Viewing 15 replies - 1 through 15 (of 19 total)
  • You can grab the username or any other piece of information for the currently logged in user, using the function : wp_get_current_user
    reference : https://codex.wordpress.org/Function_Reference/wp_get_current_user

    Something like this SHOULD give you something that your after:

    $current_user = wp_get_current_user();
    
    $current_username = $current_user->user_login;

    You can then use this variable inside of your shortcode:

    <?php echo do_shortcode("[listyofiles folder='/file/'.$current_username.'/']") ;?>

    Evan

    Thanks for the response and the help.

    <?php
      $current_user = wp_get_current_user();
      $current_username = $current_user->user_login;
    ?>
    <?php echo do_shortcode("[listyofiles folder='/file/'.$current_username.'/']") ;?>

    Results in “No folder specified”

    Well, first confirm that the username is returned.

    Try just echoing the current users username:

    <?php
      $current_user = wp_get_current_user();
      $current_username = $current_user->user_login;
    
      echo 'The Current User's Username Is : '.$current_username;
    ?>

    See if that returns properly.

    Parse error: syntax error, unexpected T_STRING, expecting ‘,’ or ‘;’

    oops, the ‘ wasn’t escaped properly, sorry about that.

    <?php
      $current_user = wp_get_current_user();
      $current_username = $current_user->user_login;
    
      echo "The Current User's Username Is : ".$current_username;
    ?>

    or

    <?php
      $current_user = wp_get_current_user();
      $current_username = $current_user->user_login;
    
      echo 'The Current User\'s Username Is : '.$current_username;
    ?>

    The second code returned the username.

    (Thanks for the continued help, BTW)

    Of course, Mike.

    If that returned the second username than you can use that within your shortcode i’d imagine.

    <?php echo do_shortcode(“[listyofiles folder=’/file/’.$current_username.’/’]”) ;?>

    Just make sure that the $current_username variable is set and has a value when its being used in the shortcode. If its empty, obviously you’ll get an error that no folder exists, as you did earlier.

    This is what I have, and the error still gets returned as “no folder specified”.

    <div class="entry_content">
    <?php
      $current_user = wp_get_current_user();
      $current_username = $current_user->user_login;
    
      echo 'The Current User\'s Username Is : '.$current_username;
    ?>
    <?php echo do_shortcode("[listyofiles folder='/file/'.$current_username.'/']") ;?>
    
            </div>

    If you hardcore the username does it return as you expected?

    eg:

    <?php echo do_shortcode("[listyofiles folder='/file/eherman24/']") ;?>

    Unfortunately, yes 🙂

    ahh, I see. Without color coding its hard for me to enter the proper syntax.

    <?php echo do_shortcode("[listyofiles folder='/file/'".$current_username."'/']"); ?>

    possibly?

    Sure! Take your time.

    The plugin doesn’t require the final / in the path

    <?php echo do_shortcode("[listyofiles folder='/file/eherman24<strong>/</strong>']") ;?>

    But even trying this <?php echo do_shortcode("[listyofiles folder='/file/'.$current_username']") ;?> it still isn’t working.

    And I don’t know if it helps, but I was able to get it to work in another part of my site using this:

    <?php echo do_shortcode("[listyofiles folder='/files//{$NTS_CURRENT_USER->getProp('username')}']");?>

    Check the above code, does that work at all?

    <?php echo do_shortcode("[listyofiles folder='/file/'".$current_username."'/']"); ?>

    <sadness>Same error.</sadness>

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Do shortcode and usernames’ is closed to new replies.