You’re using your shortcode function to directly output something (using echo). Shortcodes are supposed to return the desired output rather than echo it directly. You could use output buffering to “catch” anything that’s outputted (for example, via echo) in an output buffer instead of directly outputting it, and retrieve the content of the buffer afterwards.
This would yield an implementation like this:
function wpse149825_my_shortcode( $atts, $content = NULL ) {
// Turn on output buffering, which sends output from "echo" to a "buffer" instead of directly outputting it
ob_start();
// Output shortcode content
echo 'Shortcode content';
// Get buffer contents
$contents = ob_get_clean();
// Return shortcode content
return $contents;
}
add_shortcode( 'my_shortcode', 'wpse149825_my_shortcode' );
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
- Autogenerate shortcodes from an array of strings
- 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
- is_page “range” for if statement?
- How can I generate random numbers inside my shortcodes on each implementation?
- 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
- Different loop output every x posts in custom post type
- Showing content from one page on another
- Showing a different gallery in a seperate post
- Create own WordPress shortcode gallery
- Get value from shortcode to do something
- WordPress Shortcodes.. printf is outputting a random number… Can’t figure out WHY?
- 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?
- Displaying recent posts on static page with template-part via shortcode
- Excecuting php function in shortcode
- Returning data instead of echoing/printing
- 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
- do_action(), iFrame Gets Displayed Despite Password Protection [closed]
- Using Shortcode to Grab Archive Listing, Separate by Year
- Capitalize Shortcode Value on Output
- Why is my str_replace not working on short code submitted by front-end?
- List all blogs, but exclude the main site
- Shortcode not staying inside div tag
- Shortcode to pull posts
- Help Fallback (thumbnail)
- How to return a string that has a jQuery and Ajax inside in a shortcode?
- Shortcode function doesnt work without caption
- Action Hook Inside WordPress Plugin Shortcode
- PHP Helper Class to create shortcodes
- Create special button on WP Tiny MCE Posts Editor for Shortcodes
- Change Spaces to Dashes?
- how to get sentence values from wordpress shortcode parameter?
- Updating Metadata with Shortcode
- using shortcodes inside gravity forms
- How to Create custom block for displaying information in content section which act like shortcode
- Create shortcode for metabox gallery
- couldnt show short code atts in category
- Time Stamp In A WordPress Post Title That Does Not Keep Refreshing
- Advanced Custom Forms PHP formatting for do_shortcode
- overwrite wordpress gallery with custom gallery shortcode
- How to use Contact Form 7 shortcode value in a page?
- How can I create a shortcode from an html and php code written together
- How Can I Display the Last Modified Date for User Profiles on WordPress?
- CPT in a shortcode
- PHP/CSS: Shortcode won’t display correctly, and only displays in the head (before content)
- Show products per shortcode
- 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
- 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
- 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
- Whats wrong with my code? Need To add String to shortcode? [closed]
- A next page function with shortcode?
- using a shortcode in a hyperlink
- WP Custom tables query
- Display total count of products in orders of a specific order status
- Display terms on product page with shotrcode
- Redirect loop upon installation of my plugin
- why css file not link?how to create permalink?
- Custom plugin with shortcode not working
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- Shortcode Displays 2 times
- Adding a css class to the gallery
- Strip and print only the numbers found in current’s post excerpt (even if they are without space)
- How to make this shortcode not refresh the whole page when i use it on frontend
- “Joined in” or “Member since” displayed on single store page and then using shortcode to add