How can we get username into a shortcode

343 views Asked by At

How can we use $usernamein a shorcode

<?php echo do_shortcode('[education="$username"]'); ?>

But it doesn't work

I have a shortcode as [education="username"]. It works manually [education="john-doe"]

I want to get username into my shortcode on user profile page

<?php echo do_shortcode("[education="username"]"); ?>

But it's not working. What am I missing ?

2

There are 2 answers

1
Asaph On

In php, single quoted strings don't support variable interpolation. Change from single quotes to double quotes. The double quotes inside your string will need to be escaped.

Change:

<?php echo do_shortcode('[education="$username"]'); ?>

to

<?php echo do_shortcode("[education=\"$username\"]"); ?>
1
Scalable On

You are using single quote .. Use double quote and that should work.

 <?php echo do_shortcode("[education=". $username. "]"); ?>