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)
- Calling a function via a shortcode in javascript
- How to make this shortcode not refresh the whole page when i use it on frontend
- Is It Possible to Block Redirect Checker Tools from Knowing Where URLs are Redirected?
- How do i get (unique) page name?
- Archive dropdown styling not applied
- using woocommerce_template_single_add_to_cart in shop-loop – javascript issues [closed]
- Uncaught TypeError: Cannot read property ‘firstChild’ of null after upgrading to WordPress 5.5
- How to display a value from a radio button in the options menu in wordpress
- Display notification only once
- Header has extra HTML block at top before my code
- Access/update database with jQuery
- How to display something in a div when the user clicks on a text in another div – no page refresh [closed]
- Use WordPress’s Media Uploader/ Manager in non WordPress php application
- Listing and displaying WooCommerce Shipping Zones in the frontend? [closed]
- Get/Set wp.customize.previewer.previewUrl
- Can A Shortcode Get HTML/Text From Content And Return It Twice With Added HTML?
- Recent post display using shortcode
- javaScript in section of WP API
- Browser title script ignoring is_page /else conditional [closed]
- To echo or not to echo?
- How do I turn a shortcode into PHP code?
- Blank space at beginning of tag?
- How do I add HTML to a PHP function [closed]
- Nested shortCode functions in the functions.php file
- Placing the_content inside shortcode not working
- admin-ajax.php responding with 0
- Put php variable in shortcode
- Using wp_localize_script to store the template url into a variable to use in JS
- Custom image upload
- How to display recent posts on home page with custom HTML