0

The shortcode I'm using is:

[survey_records id=number qid="3" aid="selected" data="answer" uid="68" session="last"]

I want the id to be variable, like:

[survey_records id="$variable" qid="3" aid="selected" data="answer" uid="68" session="last"]

How can I change the id parameter to a dynamic variable?

5
  • Where are you using the shortcode? Commented Dec 28, 2022 at 12:38
  • 2
    Is this your own custom shortcode? If so, it would be helpful to include the shortcode function in your question. Commented Dec 28, 2022 at 14:23
  • 2
    Where/how would the variable be get its value? Commented Dec 29, 2022 at 0:43
  • its shortcode from plugin and i need change id from number to variable Commented Dec 30, 2022 at 10:03
  • I believe to do what you're asking you'd have to, instead of dropping the shortcode into the content, add it via code so that you can inject the variable. Commented Jan 2, 2023 at 16:18

2 Answers 2

1

Rather than dropping the shortcode into the content the way you normally would, you'd instead need to add it via code with an echo.

So instead of adding [survey_records id=number qid="3" aid="selected" data="answer" uid="68" session="last"] to the content/block editor, you'd want this instead:

<?php
    echo do_shortcode( '[survey_records id="' . $variable . '" qid="3" aid="selected" data="answer" uid="68" session="last"]' );
?>

Now, how you get that into the content, that depends on a lot of different factors. You can use a plugin that lets you add PHP to your content, or write a meta box for posts/pages that allows you to add variables where, if they are present, will execute this sort of function... ...you can hard code it into a specific template and then add some other method of attaching the variable, etc. Lots of ways to do it.

1
  • 1
    Good 👍. This solution has always worked for me. Commented Sep 7, 2023 at 16:43
0

The shortcode function behind that shortcode will need to be able to accept an argument so it can return your variable in the expected place.

If this is a custom shortcode function you wrote, you'll have to make that adjustment. If its a third party shortcode, you'll need to look at their documentation and see if its possible.

1
  • its a third party shortcode Commented Dec 31, 2022 at 8:02

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.