Joy
(@joyously)
You don’t “pass parameters” dynamically to a shortcode. The shortcode is a way for a user to indicate where the dynamic content produced goes, using static parameters.
But that doesn’t mean that the shortcode handler can’t use other information available (such as $_POST) as it executes.
If you are trying to use an existing shortcode for something it wasn’t designed for, you should just write your own shortcode.
I did write my own shortcode. The question is, how do I get form data into the call back function of the shortcode?
Joy
(@joyously)
The shortcode handler function can reference $_POST, but it is only run when displaying the page that the shortcode is on. If the form invokes some other page, then that is a different context that the shortcode knows nothing about.
You might need to have another, separate, function that is hooked to the init action, that checks for the $_POST variable and uses it to modify whatever comes next.
Here is the code from the wordpress page exactly how I tried using it.
Use this page to find all teachings with a specific word or phrase in the title. Type a word or phrase into the box below and click <span style="font-size: .8em;">SUBMIT</span>.
<div id="search"><form action="https://teachings.ssg.church/search-shortcode/" method="post">Search: <input name="term" type="text" /><input type="submit" value="Submit" /></form></div>
<div id="teachListBrief">
[addtitlesearch sterm=$_POST['term']]
I have tried using just the $_POST and that didn’t work.
The form is posting to the same page the shortcode is on.
So, what am I doing wrong.
If I put a literal string, such as “world”, it works.
dpimental
Joy
(@joyously)
I see you didn’t understand what I said before about not passing dynamic parameters.
The shortcode handler function should be outputting the <form> itself, and using the input as output in the field, to show what was searched for. The handler function doesn’t need a parameter in the shortcode. It simply uses the $_POST to get the value. And be sure you don’t use a variable name that is reserved for WordPress: https://codex.ww.wp.xz.cn/Reserved_Terms
Understood. So, all this is handled in the call back function of the shortcode, correct?
Joy
(@joyously)
Yes. The shortcode could have parameters for other options, like how many to show or how long the excerpt is or whatever, for the user to be able to control the output without coding. But the form itself should be all encapsulated in that function so that it works wherever you put the shortcode. It can also set a define() so that it is only output once on a page.
Thanks Joy.
Much appreciated. I got it to work 🙂
dpimental