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;
});
}
Related Posts:
- How to manually fix the WordPress gallery code using PHP in functions.php?
- Encode text string being appended as query to URL [closed]
- Display a text message if the shortcode is not found?
- How to return a string that has a variable inside in a shortcode?
- Run shortcode at certain resolution
- Passing a shortcode attribute to a sub-function
- post meta value as shortcode parameter
- How to protect own PHP code from WordPress updates
- the_title() & the_content() are retrieving Post details inside a Page, after calling a shortcode
- Get list of shortcodes from content
- Audio tags around Mp3 URL in content
- Loading page template into shortcode
- How to add Shortcode (font awesome) in widget title?
- Wrap each shortcode in array to div
- Placing the_content inside shortcode not working
- Put php variable in shortcode
- Placing raw HTML inside a WordPress shortcode
- How do I add text in a shortcode?
- How to Insert Shortcodes into Theme?
- Template part inside shortcode, unexpected reult
- Shortcode Inside Class Not Working
- Shortcode API: How to get name that matched shortcode RegEx?
- Shortcode content filter?
- Custom Theme, Custom shortcode not working
- Why is file_get_contents returning page source?
- Plugin CSS not enqueing
- Showing content from one page on another
- Create own WordPress shortcode gallery
- Get value from shortcode to do something
- Colon is Missing In My Website Url in WordPress
- Custom shortcode outputs plain text instead of HTML at top of post
- How to use wp_add_inline_style for custom CSS added via shortcode?
- Only first shortcode gets executed
- echo something inside a shortcode
- displaying content that includes shortcodes within an echo
- List taxonomy term slugs within shortcode (do_shortcode)
- Display Data in Table from External Database in WP using Shortcodes
- anyway to put inside a $outprint=sprintf()
- Capitalize Shortcode Value on Output
- Why is my str_replace not working on short code submitted by front-end?
- Shortcode not staying inside div tag
- Line breaks inside shortcode variable
- Shortcode for Listing Users from Meta Value?
- Shortcode to pull posts
- Help Fallback (thumbnail)
- How to return a string that has a jQuery and Ajax inside in a shortcode?
- Shortcode to log user into current URL
- Shortcode function doesnt work without caption
- Calling PHP Titles inside Javascript Markup
- Create special button on WP Tiny MCE Posts Editor for Shortcodes
- Change Spaces to Dashes?
- Including Custom fields within the_content
- What is the difference between Null vs Empty (Zero Length) string?
- Show Featured Image Dimensions and size frontend with shortcode
- How to work Woocommerce pagination inside shortcode?
- using shortcodes inside gravity forms
- How to Create custom block for displaying information in content section which act like shortcode
- Display logged in user name and lastname on page
- Display Visual Composer shortcode if a post belongs in specific categories
- is it possible for a URL to filter by tag
- Trying to create a shortcode to display categories with a specific product tag
- How to pass parameters in wordpress shortcode?
- Replace background image with post thumbnail in shortcode
- How can I prevent a shortcode div from extending beyond its boundaries?
- Conditional formatting on data fetched from MYSQL
- Adding variable in WordPress shortcode
- Display a custom name when the user has no name settle in his account
- Having trouble creating two shortcodes, one for logged in user and one for visitors
- Execute Shortcode in Textfile
- custom taxonomies not working
- How to use Contact Form 7 shortcode value in a page?
- Display current user metadata on WordPress page
- Add Shortcode to ::before class
- Reload page with a different shortcode when a user selects from a dropdown
- How Can I Display the Last Modified Date for User Profiles on WordPress?
- How to output values from a loop into a javascript array
- Shortcode’s output to use as other shortcode’s parameter
- Show products per shortcode
- Short code into PHP
- Add two or multiple functions in WordPress Post or Page
- Shortcode Works for Logged in Users but Not Working for Guest
- perform mathematical operation on shortcode
- Update same content in multiple posts at once?
- problem multisite with blog id
- Shortcode At top of Page – PHP 5.5 vs 5.4 – ob_start();
- Create dynamic content from one WP page to multiple other pages
- Shortcode Display From WordPress Dashboard Page Without Using post_id
- Do_shortcode on PHP file outside theme ALMOST works
- Using the same shortcode to show any table from the database
- Shortcoding with Divs
- Custom Shortcode with Dynamic Button
- Whats wrong with my code? Need To add String to shortcode? [closed]
- Shortcode for Visual Composer Grid fails turning string to integer
- Is there a better way to output HTML as a shortcode?
- Gutenberg – render_callback does not pass ToggleControl value on frontend
- why css file not link?how to create permalink?
- How call WordPress shortcode inside function file
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- Shortcode Displays 2 times
- “Joined in” or “Member since” displayed on single store page and then using shortcode to add