0

I need a shortcode to link the current logged in user to their author page but I couldn't make the function workable. Found this code based on another similar question here:

<?php
    if ( is_user_logged_in() ) {
        echo '<a href="' . home_url() . '/author/' . get_the_author_meta( 'user_login', wp_get_current_user()->ID ) . '"> My personal page</a>';
    } else {
        echo '<a href="' . wp_login_url() . '" title="Login">Login</a>';
    }
?>

But it gets "syntax error, unexpected 'href' (T_STRING), expecting ';'" What is the error here?

I also found this code which doesn't get an error:

<?php 
global $current_user;
get_currentuserinfo();
echo get_author_posts_url($current_user->ID); 
?>

But if I put this into a shortcode using add shortcode function, it doesn't display anything.

This has been asked many times here and I have tried those answers but none worked for me. I'm lost and I'm not an expert coder so any help will do. Thank you!

EDIT including shortcode:

/**
*Shortcode for user's author link
*/

function my_users_shortcode( $atts, $content) {
return '<?php
    if ( is_user_logged_in() ) {
        echo '<a href="' . home_url() . '/author/ ' . get_the_author_meta( 'user_login', wp_get_current_user()->ID ) . '"> My personal page</a>';
    } else {
        echo '<a href="' . wp_login_url() . '" title="Login">Login</a>';
    }
?>'
;}

add_shortcode( 'my_shortcode', 'my_users_shortcode' );
6
  • for giggles, try adding a space after /author/ so you would have '/author/ '
    – rudtek
    Aug 24, 2020 at 2:55
  • 1
    I don't get how is that the fix. I did it but still gets syntax error. Any other methods?
    – gjwrites
    Aug 24, 2020 at 4:33
  • For the code I'd vote for a combination of the two - the first's is_user_logged_in and wp_get_current_user()->ID is sensible, but I'd use get_author_posts_url to construct the URL not trying to assemble it yourself. You may also need to esc_url the get_author_posts_url as you echo it - get_the_author_posts_link() does.
    – Rup
    Aug 24, 2020 at 9:38
  • 1
    However I've tried copy & pasting your first block of code into my site and it works fine: I can't reproduce the syntax error. Can you show us your complete shortcode handler please?
    – Rup
    Aug 24, 2020 at 9:39
  • I wasn't sure it was the fix, but I'm not getting a syntax with what you posted so I was just guessing. I second the request for full code.
    – rudtek
    Aug 24, 2020 at 14:45

1 Answer 1

0

You're trying to return a <?php block as text, which you don't need to do, and is the wrong thing to do - it won't get executed, even if you do return it correctly. Your specific syntax error is this bit:

function my_users_shortcode( $atts, $content) {
return '<?php
    if ( is_user_logged_in() ) {
        echo '<a href="' . home_url()

where you have the ' from the echo inside a string that you're delimiting with 's too, and you can see from the syntax highlighting here that it thinks the string ends with echo ' and that the <a href is now PHP code again. If you did want to do that, you'd need to escape the 's with backslashes to make them part of the string and not the ends of the string, i.e.

return '<?php  echo \'a href="\' . home_url(); ?>';

But you don't have to do that: since we're currently running PHP code in the my_users_shortcode handler you can just echo the link from there, rather than trying to return a PHP snippet to try and execute later (which won't work), i.e.

function my_users_shortcode( $atts, $content ) {
    if ( is_user_logged_in() ) {
        echo '<a href="' . home_url() . '/author/ ' .
             get_the_author_meta( 'user_login',  wp_get_current_user()->ID ) .
             '"> My personal page</a>';
    } else {
        echo '<a href="' . wp_login_url() . '" title="Login">Login</a>';
    }
}
add_shortcode( 'my_shortcode', 'my_users_shortcode' );

That said, as discussed in the comments I would use get_author_posts_url to generate the author page URL rather than trying to construct it yourself, and I think you ought to have some esc_urls too:

function my_users_shortcode( $atts, $content ) {
    if ( is_user_logged_in() ) {
        echo '<a href="' . esc_url( get_author_posts_url( wp_get_current_user()->ID ) ) .
             '">My personal page</a>';
    } else {
        echo '<a href="' . esc_url( wp_login_url() ) . '" title="Login">Login</a>';
    }
}
add_shortcode( 'my_shortcode', 'my_users_shortcode' );

(and if for the future you want the text to be translatable you need to wrap those parts in __('') too)

1
  • Oh, now I see. Thank you so much!! Learned a lot as well. You're awesome man. It works!!
    – gjwrites
    Aug 26, 2020 at 4:01

Your Answer

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

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