Shortcodes should always return
data rather than echo
ing it. That way it appears in the right place on the page.
A couple of ways you can do this:
Option 1 – add to variable
function my_output() {
$output="<div>" . $myvariable . '</div>';
return $output;
}
Option 2 – use the object buffer
function my_output() {
// Start the object buffer, which saves output instead of outputting it.
ob_start();
?>
<div>
<?php echo $myvariable; ?>
</div>
<?php
// Return everything in the object buffer.
return ob_get_clean();
}
Related Posts:
- Calling PHP Titles inside Javascript Markup
- How do I use Shortcodes inside of HTML tags?
- WordPress Ajax always returns a 404 error
- WordPress Shortcode and Dynamic CSS
- Calling a method from functions.php on a click of a button
- Run shortcode at certain resolution
- Nice scroll to wordpress
- Javascript code inside “” in core WordPress files .php
- Minify HTML, CSS, JS with PHPWee?
- Placing raw HTML inside a WordPress shortcode
- Slide in Panel WordPress Post
- Shortcode content output but not in correct place
- Show post in slider
- Trigger popup in a php if/else statement
- Populate dropdown with Child Pages based on Parent Page chosen
- search and replace preword from author
- How to change redirection route to a php page for making it only accessible by logged-in members?
- Creating own shortcode – echoeing php variable based on JS variable
- parts of url disappear when using # inside href=”” [closed]
- How create a Shortcode with hover and complex options
- Noscript not working as it should in wordpress site
- Add Codepen animation as Preloader to WordPress
- HTML escaping data with ajax requests
- Using ob_start and ob_get_clean with wordpress shortcode
- Override user-edit.php to design own profile page
- Handling PHP/HTML inside the output of a shortcode function
- Link to page with posts of specific type
- Replacing WordPress Icons (menu,icons32, etc)?
- Shortcode displaying at the top of the page [duplicate]
- Is it possible to isolate and use WordPress functions outside of wordpress
- Time Delay a URL Redirect for Specific Page
- Cascading dropdown select search based on Parent Page & Child Page
- WordPress cascading dropdown selection search based on Parent Page & Child Page
- Display WordPress search results based on the selected Sub-Category
- html form: redirect page after form completes submit function and posts data
- using shortcodes inside gravity forms
- WordPress User Meta value variable into Google Gauge Chart
- Search bar for wp menu
- Adding HTML Code to Replace Text in PHP
- Executing php on button press
- Add other class content with reference class value
- Shortcode in Shortcode/HTML Graphics and Pictures
- Using Google Analytics in an enqueued JS file
- couldnt show short code atts in category
- separate list of programs in html/javascript coming from php
- How to use wp_ajax_set_post_thumbnail?
- Passing a value from an input field to a php function in WordPress?
- How do I disable cache for a certain page?
- How can I edit the final HTML structure of the whole web page
- WordPress Slider is not working
- How to return html as a string from php for WordPress
- Show subcategory name selected in specific category woocoommerce
- How can I create a shortcode from an html and php code written together
- How to output values from a loop into a javascript array
- sessionStorage saves input value in browser but it is not loaded in the form field
- Collapsible menu on post sidebar only expands and does not collapse
- Replacing entire tag with shortcode – JavaScript [duplicate]
- Target a specific container to style on WordPress storefront theme
- scroll scrpit to particular button position to next id when click button
- Bootstrap Carousel Indicators Won’t Cycle On Custom WordPress Theme
- Force ‘permanent’ post cache of shortcode results
- How can i iterate through this shortcode array?
- Where to find the html for WordPress site? [closed]
- I need help implement a Javascript code into the PHP file
- Show contact 7 form in popup
- Trying to customize wordpress drop down categories
- Set variable in javascript and using in my shortcode
- Load wp_editor on button click
- Slick slider not working on wordpress
- Show Login Errors In WordPress/Elementor (Code “works”, but breaks site)
- Shortcode putting html such as
- How to find the output of contact form 7 shortcode? [closed]
- Use WordPress’s Media Uploader/ Manager in non WordPress php application
- How can I output a php value into a JS file within WordPress?
- Slider loading issue
- Getting a specific post values to another div or modal
- WP grandchild categories in nested ul li
- Settings API callback function inserts html quotes from nowhere
- Add / Update Custom Fields After Select Pictures in Media Window
- echo something inside a shortcode
- Where can I find the inline-css of my theme header?
- How do I get my Javascript scripts working?
- Shortcode for Listing Users from Meta Value?
- Website completely messes up when logging out
- How can I use custom menus with a Bootstrap WordPress theme?
- How to redirect non logged in mobile users to page on same site?
- Show Featured Image Dimensions and size frontend with shortcode
- HTML Form Question
- Decode and Decrypt Azure B2C OpenID Authorization Token, Use Response in API Call (Example Token Within)
- Exclude ipads and tablets form wp_is_mobile code
- Adding variable in WordPress shortcode
- Execute Shortcode in Textfile
- custom taxonomies not working
- Make a full-width wordpress page
- Bring Font Awesome icons inside menu A tags
- Automatically refresh page if widget is added to page?
- number of posts with “Load More”
- Shortcode At top of Page – PHP 5.5 vs 5.4 – ob_start();
- Using the same shortcode to show any table from the database
- Load Meta box value into div AJAX [duplicate]