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_url
s 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)