Reputation: 31
I have made a simple WP plugin, that will get some documents from calameo.com
and present them in a nice manner. I have made a custom shortcode for it. Let's call it "Shortcode"…
I will have [Shortcode vendor=vendor1]
to show only the documents related to
a vendor and I know how to do that.
What I need to do is to pass arguments values from the url to the shortcode but I didn't found yet the way to do it.
Any help is appreciated.
Upvotes: 3
Views: 7346
Reputation: 253968
To pass some variables from an URL, you will use $_GET
in your shortcode like in this example:
if( ! function_exists('this_is_my_shortcode') ) {
function this_is_my_shortcode( $atts, $content = null ) {
// Attributes
$atts = shortcode_atts( array(
'vendor' => '',
'thekey1' => isset($_GET['thekey1']) ? sanitize_key($_GET['thekey1']) : '',
'thekey2' => isset($_GET['thekey2']) ? sanitize_key($_GET['thekey2']) : '',
), $atts, 'my_shortcode' );
// Variables to be used
$vendor_value = $atts['vendor'];
$value1 = $atts['thekey1']; // the value from "thekey1" in the url
$value2 = $atts['thekey2']; // the value from "thekey2" in the url
// Your code … / …
if( ! empty( $value1 ) )
$value1 = ' | Value 1: ' . $value1;
if( ! empty( $value2 ) )
$value2 = ' | Value 2: ' . $value2;
// Output: Always use return (never echo or print)
return '<p>Vendor: ' . $vendor_value . $value1 . $value2 . '<p>';
}
add_shortcode("my_shortcode", "this_is_my_shortcode");
}
Code goes in function.php file of your active child theme (or theme). tested and works.
USAGE:
http://www.example.com/your-page/?thekey1=document1&thekey2=document2
&
character)[my_shortcode vendor="vendor1"]
echo do_shortcode( "[my_shortcode vendor='vendor1']" );
You will get as generated html output:
<p>Vendor: vendor1 | Value 1: document1 | Value 2: document2</p>
Upvotes: 3
Reputation: 41
Agreed, with Derek, the question is quite unclear.
To what I understand, you want to extract the parameters passed on the URL of the page that contains your shortcode (say a 'vendor' parameter), and make that the shortcode parameters can take this value dynamically ?
If it's the case, that does not make sense : shortcodes are used to generate your page's code (HTML, JavaScript, .. whatever runs in the browser), and have completely disappeared in the resulting page, meaning that you can't behave differently upon the value of the URL parameter.. unless your shortcode generates code (JavaScript) that contains some 'vendor' variable that could take its value from the parameter, and generate in its turn something (HTML, SVG….), in brief, some kind of tricky code..
Upvotes: -1