I need to create a column-system for WordPress with shortcodes, which is not a problem, but I’m trying to make it with less code.
I have an array with the data needed, I loop through it, create a unique-named function and set it as shortcode-function. The third step is a mystery. How can I create a function from a variable.
Here’s an example, how it should 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 seems that it’s not possible to make it work like that in PHP. Is there any way to make this work, as I would not want to write all the (identical) functions separated. I could make the shortcode something like [col first]text[/col]
but the client wants to have different names for every one of them.
you can use the double dollar syntax to use the value of a variable as a variable identifier,
Example:
I have never tried but you way want to try:
or a function like create_function
if your using PHP 5.3 or greater then you can do something like so:
which should work fine
I’m not sure how WP invocates the functions, but if it uses
call_user_func
then you might cheat by using an object with virtual methods: