I'm a frontender, so i'm not so used to work with php. But I need to solve something. I'm adding a shortcode within the single-product template, and inside this shortcode, I need to echo the slug from the product.

I've tried multiple things, but I cant get it to work.

    global $post;
    $post_slug = $post->post_name;
    echo $post_slug;
    echo do_shortcode('[sc name=" I NEED TO ECHO THE POST SLUG HERE "]');

Maybe some of you, could help me out :)

Best regards, Michael

  • Hello Michael - Welcome to the WordPress Development community! While your question is regarding an issue within the context of WordPress, it's not actually a question about working with WordPress - rather it's a question about generic PHP syntax. As such, it might be considered off-topic for our stack. Webmasters or Stack Overflow might be a more appropriate place to ask.
    – bosco
    Jun 12, 2021 at 16:09

1 Answer 1


There are a few to do this:

Using double quotes

Don't forget to escape the double quotes inside the string.

global $post;
echo do_shortcode("[sc name=\"$post->post_name\"]");

Using sigle quotes

global $post;
echo do_shortcode("[sc name='$post->post_name']");

Using sprintf

global $post;
echo do_shortcode(
  sprintf('[sc name="%s"]', $post->post_name)

Using wp_sprintf

global $post;
echo do_shortcode(
  wp_sprintf('[sc name="%s"]', $post->post_name)

Using heredoc

    <<< EOF
[add_to_cart_url id="$post->post_name"]

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.