• 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)
  • Evan Herman

    (@eherman24)

    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

    Thread Starter miketheteacher

    (@miketheteacher)

    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”

    Evan Herman

    (@eherman24)

    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.

    Thread Starter miketheteacher

    (@miketheteacher)

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

    Evan Herman

    (@eherman24)

    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;
    ?>
    Thread Starter miketheteacher

    (@miketheteacher)

    The second code returned the username.

    (Thanks for the continued help, BTW)

    Evan Herman

    (@eherman24)

    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.

    Thread Starter miketheteacher

    (@miketheteacher)

    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>
    Evan Herman

    (@eherman24)

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

    eg:

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

    Thread Starter miketheteacher

    (@miketheteacher)

    Unfortunately, yes πŸ™‚

    Evan Herman

    (@eherman24)

    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?

    Thread Starter miketheteacher

    (@miketheteacher)

    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.

    Thread Starter miketheteacher

    (@miketheteacher)

    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')}']");?>

    Evan Herman

    (@eherman24)

    Check the above code, does that work at all?

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

    Thread Starter miketheteacher

    (@miketheteacher)

    <sadness>Same error.</sadness>

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