Find answers, ask questions, and connect with our
community around the world.

Activity Forums Web Design WordPress How to autogenerate shortcodes from an array of strings?

  • How to autogenerate shortcodes from an array of strings?

    updated 3 weeks ago 0 Member · 1 Post
  • Deborah

    Member
    October 29, 2019 at 9:47 am

    I have to create dozens of shortcodes of the form function foobar_sc( $atts ) { remove_filter( ‘the_content’, ‘wpautop’ ); $content = apply_filters( ‘the_content’, ‘[block slug=foobar]’ ); 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, function ( $atts ) { remove_filter( ‘the_content’, ‘wpautop’ ); $content = apply_filters( ‘the_content’, ‘[block slug=’ . $name . ‘]’ ); add_filter( ‘the_content’, ‘wpautop’ ); return $content; }); } but then by writing in the editor [foo] [bar] the output is [block slug=] [block slug=] Moreover the plugin Query Monitor prints a notice (PHP errors) Undefined variable: name at line $content = apply_filters( ‘the_content’, ‘[block slug=’ . $name . ‘]’ ); So it seems that there is something wrong in ‘ . $name . ‘, what is the correct syntax? – by hq overview oshitwaddup1 – –

Reply to: Deborah
Your information:

Cancel
Original Post
0 of 0 posts June 2018
Now