Wordpress: Pass URL parameter to shortcode to create a dynamic page (without editing a template's functions.php)
Get the solution ↓↓↓Before you tell me this question has been asked dozens of times, let me say that I read more than thirty posts on here and elsewhere. Either the explanations are partial and vague, or the proposed code doesn't fit my needs, e.g. the suggestion is to edit the template (which I read wasn't best practice). I'll explain what I want to do and then show the code I already have. My experience with Wordpress is two days long, so bear with me.
I have started writing a plugin (so that I can use it regardless of the theme). I am using a class, not writing the functions directly in the plugin's php file.
The first function I have queries the database to get an array containing a list of translators. Then I pass this array to another function to process the array and make it into a list (with HTMLul
andli
). I add an anchor tag around the translator's name so that clicking it will open a dynamic page that will show full details about the translator.
To accomplish the above, I have two more functions: one to query the database to get all the relevant details, and another to process the resulting array and apply some basic formatting to it.
All of the above are inside a class. After initiating the class, I add two shortcodes: one is for the formatted translators list, which works as expected. The other one is for the translator details. This is where I can't figure out what to do and how to do it. I did succeed in manually passing the translator's ID to the page but I want to do this dynamically, so thattranslatorID
is passed to the shortcode.
class CT_translator {
private function get_translators_list() {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare("SELECT * FROM translators_details ORDER BY translatorID ASC")
);
}
public function format_translators_list() {
$results = $this->get_translators_list();
$list = "<ul>";
foreach($results as $result) {
$list .= "<li><a href=\"/prevodach-info/".$result->translatorID."\">".$result->firstName." ".$result->paternalName." ".$result->familyName."</a> — ".$result->city."</li>";
}
$list .= "</ul>";
return $list;
}
private function get_translator_details($translatorID) {
global $wpdb;
return $wpdb->get_results(
$wpdb->prepare("SELECT * FROM translators_details WHERE translatorID = %s", $translatorID)
);
}
public function format_translator_details($translatorID) {
$results = $this->get_translator_details($translatorID);
$list = "";
foreach($results as $result) {
$list .= "<p>".$result->firstName." ".$result->paternalName." ".$result->familyName." — ".$result->city."</p>";
$list .= "<p>".$result->email.", ".$result->phone.", ".$result->address."</p>";
}
return $list;
}
}
Initiate the class: $CT_translator = new CT_translator();
Add the two shortcodes:
add_shortcode("list_translators", array($CT_translator, 'format_translators_list'));
add_shortcode("view_translator", array($CT_translator, 'format_translator_details'));
This is how I call the translators' list in the page: [list_translators]
And this is the manual shortcode for viewing a translator's details: [view_translator translatorID="1"]
I read that in order for Wordpress to be able to recognise my variables (in this casetranslatorID
), I need a function to add this variable to Wordpress' existing variables, e.g.:
public function add_query_vars($aVars) {
$aVars[] = "translatorID";
return $aVars;
}
Then I would need to add another function to rewrite the URL so that the last part is understood as a variable:
public function add_rewrite_rules($aRules) {
$aNewRules = array('prevodach-info/([0-9]+)/?$' => 'index.php?pagename=prevodach-info&translatorID=$matches[1]');
$aRules = $aNewRules + $aRules;
return $aRules;
}
And when I add the two filters (outside the class):
add_filter('query_vars', 'add_query_vars');
add_filter('rewrite_rules_array', 'add_rewrite_rules');
everything goes bang! Nothing loads and there's a ton of errors.
Answer
Solution:
I received help from someone who is not a member of StackOverflow, and the problemw as solved.
It turns out finding a way to pass the URL parameter to the shortcode, then from the shortcode to the function was not necessary. (Even if possible, I still don't know how it's done.)
All that was necessary was to capture the URL parameter inside the function using $_GET or $_REQUEST. From my humble experience writing PHP code, I thought capturing URL parameters inside a function was a bad decision.
So, here's what the amended function looks like:
public function format_translator_details($translatorID = "") {
if(empty($translatorID)) {
$translatorID = isset($_GET['translatorID']) ? $_GET['translatorID'] : "";
}
$list = "";
if(!empty($translatorID)) {
$results = $this->get_translator_details($translatorID);
foreach($results as $result) {
$list .= "<p>".$result->firstName." ".$result->paternalName." ".$result->familyName." — ".$result->city."</p>";
$list .= "<p>Превежда от и на ".$result->langNameBG." език.</p>";
$list .= "<p>".$result->email.", ".$result->phone.", ".$result->address."</p>";
}
}
return $list;
}
And the shortcode is simply[view_translator]
.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.