I am creating some WordPress short codes aimed at providing internal navigation on a page (one page with a lot of content sections and it own menu).
This is what I have:
//menu
function internal_menu($atts) {
extract(shortcode_atts(array(
'href1' => '#jl1',
'href2' => '#jl2',
'href3' => '#jl3',
'href4' => '#jl4',
), $atts));
return '<div id="internalPageMenu">
<ul>
<li><a href="' . $href1 . '"><i class="fa fa-bars"></i>link 1</a></li>
<li><a href="' . $href2 . '">link 2</a></li>
<li><a href="' . $href3 . '">link 3</a></li>
<li><a href="' . $href4 . '">link 4</a></li>
</ul>
</div>';
}
add_shortcode('internal-menu', 'internal_menu');
//menu target
function internal_menu_target($atts) {
extract(shortcode_atts(array(
'id' => 'jl1',
'text' => '',
), $atts));
return '<h3 id="' . $id . '">' . $text . '</h3>';
}
add_shortcode('internal-menu-target', 'internal_menu_target');
And using this in my WordPress admin panel:
[internal-menu]
[internal-menu-target id="jl1"]
Some content
[internal-menu-target id="jl2"]
...etc...
How do I make the menu dynamic (not restricted to the number of items it can have)? For example the short code would be:
[internal-menu targets="jl1, jl2, jl3, jl4, jl5, ...etc..."]
foreach
would be your answer here. It will be the easiest and cleanest in my opinion. Before I give you a code example, lets analyze your code and look at all your flaws and how we will correct themFLAWS
Never ever use
extract()
.exctract()
creates variables on the fly which is problematic. You cannot properly debugextract()
(if you even can), so when it fails you really have your work cut out for you, unnecessarily. For these reasons, it was completely removed from core and the codex. See trac ticket 22400. You should have an evil list withquery_posts
andextract()
in the top two positions, that i how bad these two are.You are not sanitizing and validating input data which can lead to hacker injecting jquery into your code in order to hack your site. Never trust any data that comes from user side and URL’s, it might be infected.
As you already know, taken from your code, shortcodes cannot except array value, the values must be string. In your case, we need to create an array from string values. Again, because you can’t trust a user to not use spaces before or after commas, it is wise and recommended to remove all white spaces, if any, in order for your
explode
function to correctly create your arrayWith this new approach, you need to make sure that your values in your strings are the in the correct order and that the strings is the correct lenght. If not, you will get unexpected output
Lets tackle the first shortcode: (PLEASE NOTE: All the code below is untested. It might be buggy or have syntax errors)
internal-menu
You can then use the shortcode as follow
internal-menu-target
You can use this shortcode as follow:
Problems:
WordPress shortcodes have some painful restrictions in the format of the data which can be passed…
space-delimited variables:
[shortcode a="1 2"]
result:
$atts=['a'='"1', 0='2"']
‘]’ closes the shortcode:
[shortcode b=[yay]]
result:
$atts=['b'='[yay']
Solution:
You can get around this by using
urlencode()
:[shortcode atts=a=1+2&b=%5Byay%5D]
parse it like so:
parse_string($atts['atts'],$atts);
result:
$atts=['a'=>'1 2', b=>'[yay]']
That will give you the array passing you want.
Now as for building your menu(s):
and then feed it like so:
etc.
BTW
extract()
is perfectly fine if you use it responsibly:This imports $foo, $bar, and $baz from $attr into local scope in a readable and predictable manner, provides defaults for those variables if they were not passed, and prevents the creation of any unexpected variables.
There’s good ways and bad ways to use language features. Forbidding everyone from using a language feature because someone might use it poorly is like forbidding everyone from breathing because someone might try to inhale Jello.
Simple way I use:
in shortcode callback, just do:
I have an alternative way to do this in
$content
of short code instead.This solution increases the readability/relation of your data.
But it might not be a good way if you want to use your $content to do other things too.
shortcode used is like…
And here how to handle it intro your shortcode function