1
votes

I want to echo PHP variable value on any page using shortcode in WordPress.

I have created:

function custom_shortcode() {

    echo $unique;

}
add_shortcode( 'test', 'custom_shortcode' );

When I place shortcode [test] on any page, it returns nothing.

The $unique variable is to show the logged-in user's username in URL. For example: example.com/?username

The expected output on front-end need to be: example.com/?username

1

1 Answers

1
votes

try this:

function custom_shortcode( $atts, $content = null )  {
    global $unique; // if $unique is global var add this line too
    return $unique;
}
add_shortcode( 'test', 'custom_shortcode' );