In the example below, $shortcodes
is a multidimensional array with shortcode data.
foreach( $shortcodes as $shortcode ) {
// Here we have the shortcode name, could be 'custom_person_john'.
$shortcode_name = $shortcode['slug'];
// Anonymous function should be referred to as 'custom_person_john'.
$shortcode_name = function( $shortcode ) {
// Build shortcode attributes.
$atts = shortcode_atts( array(
'name' => 'john',
'type' => 'mammal'
), $atts );
$markup = '<div class="'.$atts['name'].'">';
$markup .= '<div>';
// $shortcode should contain custom markup from the array.
$markup .= $shortcode['html'];
$markup .= '</div>';
$markup .= '</div>';
return $markup;
};
// Trying to register the shortcode
add_shortcode( $shortcode['slug'], $shortcode_name( $shortcode ) );
};
So this is not working, but I can't grasps why. If I dump all the registered shortcodes, they don't show up in the list. Trying to load them simple echo's [custom_person_john]. I don't have errors either.
Edit: If anyone has other ideas on how to create them dynamically out of an array, feel free to suggest :)
functions.php
I suggest maybe adding it intoinit
to see if that helps.$shortcode_name
the second time you use this variable. changing the name will to go a step ahead but in the shortcode function, i think you will not have access to$shortcode['html']
so you have to do this on a other way$shortcode
inside the function. I dumped it by doing var_dump($shortcode), returns all the correct values.$shortcode_name( $shortcode )
construct a HTML string butadd_shortcode
need a function - from where come informations of $shortcodes ? you need to access theses informations inside the shortcode functions, I prepare a answer with a example