Assuming the $row->internalFunction
is a function, or the name of a function which already exists, then you can do something like so:
foreach ( $dbQuery as $row ) {
add_shortcode( $row->formName, $row->internalFunction );
}
So if the formName
(shortcode tag) is foo
and internalFunction
is foo_func
, then [foo]
will be handled by the foo_func()
function:
// The standard/non-dynamic way.
function foo_func( $atts = array() ) {
return 'something good..';
}
add_shortcode( 'foo', 'foo_func' );
See the Codex for further details/guide.
Or here’s an example of using closure:
foreach ( $dbQuery as $row ) {
$func = $row->internalFunction;
add_shortcode( $row->formName, function ( $atts = array() ) use ( $func ){
return $func( $atts );
} );
}
Or did I misunderstand your concept?
UPDATE
is there a way I can pass parameters into the internal function
Yes, you can pass custom parameters to the internal function (internalFunction
); but you’ll do it via the closure:
foreach ( $dbQuery as $row ) {
$func = $row->internalFunction;
$params = json_decode( $row->json )->params;
add_shortcode( $row->formName, function ( $atts = array() ) use ( $func, $params ){
return $func( $params );
} );
}
Basically, use the use
keyword to make variables in the foreach
scope be available in the closure.
And you could even pass the entire $row
object..
foreach ( $dbQuery as $row ) {
add_shortcode( $row->formName, function ( $atts = array() ) use ( $row ){
$func = $row->internalFunction;
// Here you can pass $row->formName to the function.
return $func( json_decode( $row->json )->params );
} );
}