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
- How to create shortcodes that pull custom field data from general settings
- post meta value as shortcode parameter
- How to protect own PHP code from WordPress updates
- Get list of shortcodes from content
- Loading page template into shortcode
- How to add Shortcode (font awesome) in widget title?
- 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
- 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?
- wordpress 4.4 self hosted video (html5) doesn’t show
- Shortcode returns values in the wrong order
- Embedding PHP in shortcode $content
- Shortcode with PHP issue “Undefined index”
- Excecuting php function in shortcode
- Returning data instead of echoing/printing
- ::before on open/close function [closed]
- Is There A Way To Make Theme Files Accept Shortcodes?
- How can I include shortcodes within PHP?
- Is it possible to define variables in a wordpress shortcode, and then call the shortcode using a specific variable?
- Create a WordPress shortcode using PHP [duplicate]
- do_action(), iFrame Gets Displayed Despite Password Protection [closed]
- Using Shortcode to Grab Archive Listing, Separate by Year
- Unable to process php via shortcode
- Help with WordPress function inside a shortcode
- calling a custom field value inside functions.php
- Using ob_start and ob_get_clean with wordpress shortcode
- List all blogs, but exclude the main site
- Cant display an image via PHP in wordpress
- PHP code inside Shortcode
- Action Hook Inside WordPress Plugin Shortcode
- How can I include the user id inside of a shortcode php output?
- PHP Helper Class to create shortcodes
- help with custom shortcode that stopped working
- AJAX function not working [closed]
- Can ‘Custom Field’ data be inserted into this Short Code?
- how to get sentence values from wordpress shortcode parameter?
- Updating Metadata with Shortcode
- Create shortcode for metabox gallery
- How do I reopen the (Popup Maker) after entering the correct password for a password protected page?
- Not able to remove caption shortcode from the content
- couldnt show short code atts in category
- Shortcodes not processing inside post content
- Gravity Forms: How to add PHP function to confirmation conditional shortcode?
- wpdb->query returns different value to phpMyAdmin
- Time Stamp In A WordPress Post Title That Does Not Keep Refreshing
- How can I edit the final HTML structure of the whole web page
- I am having an issue with this shortcode plugin.. Warning: Illegal string offset ‘title’
- I want to hide “sold by” on certain pages with id page 43
- Advanced Custom Forms PHP formatting for do_shortcode
- overwrite wordpress gallery with custom gallery shortcode
- How can I create a shortcode from an html and php code written together
- Replacing entire tag with shortcode – JavaScript [duplicate]
- CPT in a shortcode
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- Shortcode just with a php code
- Open/closed function [closed]
- What is wrong with this shortcode? to include external php file in WordPress posts
- Need to Echo A Url path to show on a wordpress page
- WordPress Shortcode based on other shortcode
- Force ‘permanent’ post cache of shortcode results
- Wrapping shortcode content in a span or link
- Custom fields changes the formatting of metabox input
- Formatting Shortcode to Display in Visual Mode
- Use html tags in shortcode_atts, is it real?
- How to pass parameter that ends up being part of a class name with wordpress shortcode
- Contact form field in wordpress menu
- How to have Function of a plugin using global vars into a shortcode?
- Overwriting a Shortcode
- Where to place PHP for shortcodes
- How to wrap a text with shortcode into php file?
- PHP Widget and do_shortcode
- Shortcode not passing variable to included file
- A next page function with shortcode?
- using a shortcode in a hyperlink
- Display Current Time using shortcode
- How to create shortcode of this PHP code
- WP Custom tables query
- Display total count of products in orders of a specific order status
- HTTP 500 Error after Creating Shortcode
- How to edit a widget code?