I have to produce a column-system for Wordpress with shortcodes, which isn't an issue, but I am attempting to make it with less code.
I've an assortment using the data needed, I loop through it, produce a unique-named function and hang it as being shortcode-function. The 3rd step is really a mystery. How do i produce a function from the variable.
Here's a good example, how it ought to be done:
$data[] = "first";
$data[] = "second";
foreach($data as $key => $value) {
function $value($atts,$content) {
return '<div class="'.$value.'">'.$content.'</div>';
}
add_shortcode($value,$value);
}
However, it appears that you cannot have great results like this in PHP. Can there be in whatever way to make this happen, as I wouldn't wish to write all of the (identical) functions separated. I possibly could result in the shortcode something similar to [col first]text[/col]
however the client really wants to have different names for each of them.
you should use the double dollar syntax to make use of the need for a flexible like a variable identifier,
Example:
$variable = "test";
$$variable = "value of test"
echo $test; //or echo $$variable;
I have not attempted however, you way would like to try:
foreach($data as $key => $value)
{
function $$value($atts,$content)
{
}
add_shortcode($value,$value);
}
or perhaps a function like create_function
in case your using PHP 5.3 or greater you'll be able to make a move like so:
$$value = function()
{
}
that ought to work fine
I am unsure how Wordpress invocates the functions, but when it uses call_user_func
then you definitely might cheat while on an object with virtual techniques:
class fake_functions {
function __call($name, $params) {
return '<div class="'.$name.'">'.$params[1].'</div>';
}
}
$obj = new fake_functions();
foreach ($data as $value) {
add_shortcode($value, array($obj,$value));
}