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?
- Return HTML Template Page with PHP Function
- Encode text string being appended as query to URL [closed]
- How do I use Shortcodes inside of HTML tags?
- Is there a way to parse shortcodes in PHP?
- Shortcode to insert
- Shortcode putting html such as
- Display sorting options dropdown when using WooCommerce product category shortcode
- Retrieve multiple values passed to a single attribute in a shortcode
- WordPress Shortcode and Dynamic CSS
- ob_get_clean returns empty string, ob_get_flush outputs string
- Display a text message if the shortcode is not found?
- Export User Info to CSV from Front End
- Counting number of posts in a category and its sub categories and displaying result using shortcode
- Shortcode created to check language not works
- How to return a string that has a variable inside in a shortcode?
- Run shortcode at certain resolution
- Using Echo in ShortCode – Stuck
- 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
- Add/echo div with Analytics-Code to function in functions.php
- Force ‘permanent’ post cache of shortcode results
- Short code into PHP
- How can i iterate through this shortcode array?
- Add two or multiple functions in WordPress Post or Page
- Shortcode Works for Logged in Users but Not Working for Guest
- Wrapping shortcode content in a span or link
- perform mathematical operation on shortcode
- Update same content in multiple posts at once?
- Shortcode in a $output statement
- Need help with PHP functions
- Custom fields changes the formatting of metabox input
- Shortcode is not outputting all of the desired markup
- When using shortcode, content stops generating after the_content
- Formatting Shortcode to Display in Visual Mode
- PHP Shortcode – 500 error
- Use html tags in shortcode_atts, is it real?
- Run PHP Results inside WP Shortcode
- problem multisite with blog id
- How to pass parameter that ends up being part of a class name with wordpress shortcode
- Shortcode At top of Page – PHP 5.5 vs 5.4 – ob_start();
- Contact form field in wordpress menu
- Serializing URL via wp add_shortcode to auto select from option list onload
- php string inside shortcode does not work
- Create dynamic content from one WP page to multiple other pages
- Limit the number of successful logins
- Shortcodes (with a space) added to php Sample
- How to have Function of a plugin using global vars into a shortcode?
- Shortcode Display From WordPress Dashboard Page Without Using post_id
- On selecting shortcode generate selected shortcode table inside page editor worrdpress
- Show contact 7 form in popup
- Do_shortcode on PHP file outside theme ALMOST works
- Using the same shortcode to show any table from the database
- Overwriting a Shortcode
- How can I put a custom field as the link of a button shortcode?
- Shortcoding with Divs
- Where to place PHP for shortcodes
- Custom field within shortcode
- How to wrap a text with shortcode into php file?
- PHP Widget and do_shortcode
- Problem using explode inside add_shortcode() callback funciton [closed]
- Custom Shortcode with Dynamic Button
- Shortcode not passing variable to included file
- Php inside the shortcode is getting commented
- Whats wrong with my code? Need To add String to shortcode? [closed]
- My title is showing after the shortcode
- A next page function with shortcode?
- Shortcode to include PHP file, pass various parameters to include?
- using a shortcode in a hyperlink
- Shortcode for Visual Composer Grid fails turning string to integer
- Display Current Time using shortcode
- Set variable in javascript and using in my shortcode
- How to create shortcode of this PHP code
- Is there a better way to output HTML as a shortcode?
- WP Custom tables query
- Display total count of products in orders of a specific order status
- Display terms on product page with shotrcode
- Gutenberg – render_callback does not pass ToggleControl value on frontend
- HTTP 500 Error after Creating Shortcode
- Redirect loop upon installation of my plugin
- why css file not link?how to create permalink?
- Custom plugin with shortcode not working
- How call WordPress shortcode inside function file
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- How to edit a widget code?
- Shortcode Displays 2 times
- How to execute a shortcode within a custom field?
- Calling a function via a shortcode in javascript
- 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)
- Woocomerce custom add to cart button edit functionality [closed]
- I have a php function with a shortcode to add share buttons, but I’m having an error!
- Nav Menu Short code Not working When non Login
- 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
- Update shortcode values using ajax call
- How to add a shortcode for an image in the following function
- Automatically populating a date parameter within a shortcode