1

I have to create a lot of shortcodes of the form

function foobar_sc( $atts ) {
    remove_filter( 'the_content', 'wpautop' );
    $content = apply_filters( 'the_content', '<div class=con>[block slug=foobar]</div>' );
    add_filter( 'the_content', 'wpautop' );
    return $content;
}
add_shortcode( 'foobar', 'foobar_sc' );

whose names are listed in an array

$shortcodes = array("foo", "bar", ...);

I tried with

$shortcodes = array("foo", "bar");
foreach ($shortcodes as $name) {
    add_shortcode( '$name', '$name_sc' );
    function $name_sc( $atts ) {
        remove_filter( 'the_content', 'wpautop' );
        $content = apply_filters( 'the_content', '<div class=con>[block slug=$name]</div>' );
        add_filter( 'the_content', 'wpautop' );
        return $content;
    }
}

but I get the error Fatal error: syntax error, unexpected '$name_sc' (T_VARIABLE), expecting '(' on line function $name_sc( $atts ) {

Is it possible to solve it?

0

1 Answer 1

3

Your shortcode name ('$name' ) is invalid. If you want to use the variable, it should be add_shorcode( $name, .... Also, the function name is invalid. Since you want to make it "dynamic" (I'm assuming you want it to be {$name}_sc, then maybe you should use an anonymous function.

What you have is somewhat unconventional (IMO), so I don't know if this will work, but try this:

$shortcodes = array("foo", "bar");
foreach ($shortcodes as $name) {
    add_shortcode( $name, function ( $atts ) use ( $name ) {
        remove_filter( 'the_content', 'wpautop' );
        $content = apply_filters( 'the_content', '<div class=con>[block slug=' . $name . ']</div>' );
        add_filter( 'the_content', 'wpautop' );
        return $content;
    });
}
5
  • Thank you very much! This works in the sense that the shortcodes are generated, but it seems like that the only problem is the variable name in line 5. I tried by substituting '<div class=con>[block slug=' . $name . ']</div>' with '[slug=' . $name . ']' and the output is [slug=]. Moreover the plugin Query Monitor says there is a PHP error Undefined variable: name for each auto generated shortcode. Is there a way to solve?
    – sound wave
    Commented Oct 29, 2019 at 8:19
  • 1
    You need to use the use keyword so that the $name is available inside the closure (anonymous function): add_shortcode( $name, function ( $atts ) use ( $name ) {
    – Sally CJ
    Commented Oct 29, 2019 at 15:06
  • ahhh... good point. Or alternatively, you could make it global. I edited to apply use()
    – butlerblog
    Commented Oct 29, 2019 at 16:04
  • 2
    Note also that the shortcode tag is available as the third input argument of the callback.
    – birgire
    Commented Oct 29, 2019 at 16:10
  • i.e. add_shortcode( $name, function ( $atts, $content, $name ) {
    – birgire
    Commented Oct 29, 2019 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.