You can’t return the include statement. And you shouldn’t use include template parts.
And here’s how to do it correctly:
function custom_code() {
ob_start();
get_template_part( 'custom-template.php' );
return ob_get_clean();
}
add_shortcode( 'custom_shortcode', 'custom_code' );
So what we’re doing here is:
- Start output buffering.
- Include given template part using
get_template_partfunction. - Return all the output generated by that template and delete the buffer.
Related Posts:
- Using locate-template & shortcodes doesn’t appear to work
- Make “sidebar template” the default template for new pages
- How to extract the variables out from “add_shortcode” function?
- Overwriting TwentyTwelve template file with child theme template, but lower in the hierarchy
- Load templates, pass arguments, and render output from functions.php
- Shortcode to output get_header not working
- Creating mixture of shortcodes to use in the visual/text editor
- get_template_directory_uri pointing to parent theme not child theme
- How to customize the_archive_title()?
- How to override parent functions in child themes?
- Solution to render Shortcodes in Admin Editor
- remove_action on after_setup_theme not working from child theme
- Adding a second email address to a completed order in WooCommerce [closed]
- Child theme – Overriding ‘require_once’ in functions.php
- Use AJAX in shortcode
- Include files in child theme functions file
- How to redirect to post if search results only returns one post
- Redeclare a function in a child theme
- Include PHP file in Content using [shortcode]
- How to override a function when isn’t at functions.php
- Child theme functions.php do I use php open and close tags?
- How to override admin-bar style
- Declaring an instance of class included in parent theme from child theme functions.php
- One Child Functions.php for Multiple Child Themes
- Shortcode producing headers already sent error
- How to add attributes to a shortcode
- Removing custom background and header feature in child theme
- How to display random users with avatars
- How to override function in functions.php of parent theme?
- wp_get_attachment_image returns different image size
- Enabling shortcodes for custom fields
- Implementing DNS Prefetching with WordPress
- Passing variables to templates (alternatives to globalizing variables)
- Load post with a different template?
- override parent theme configuration in child functions.php
- Child theme – overriding add_image_size by a child theme [duplicate]
- syntax for remove_filter in parent theme with class
- Is it possible to override this function/class in a child theme?
- How to customize search result page title?
- How to override functions.php in child theme?
- How to use get_media_embedded_in_content function
- Can shortcodes contain conditional statements? Even without them my shortcode renders blank page
- Overriding core functions in child theme
- Shortcode with parameters inside parameters
- Creating a table from shortcode avoiding wpautop for each row
- How to fix a theme with page.php Default Template that accidentally deleted?
- Changing a function in function.php to a shortcode – for listing categories of only a certain post type
- Override parent theme function that is not hooked or in the functions.php file
- Stripping shortcode from custom excerpt function
- WordPress Change Post Templates,but not drop down, but Image selection
- how to create shortcode in wordpress
- What is wrong with this Shortcode? I get it in a wrong place inside the content
- Function in Child Theme not overriding Parent Theme function [duplicate]
- Display random text from a file with the WP built-in AJAX API
- Change parent theme file function in child themes functions.php
- Set first oembed in post to a global variable or function
- Adding extra SVGs to TwentyNineteen child theme using class TwentyNineteen_SVG_Icons
- Formatting post content to exclude gallery
- Remove images from get_the_excerpt
- Reuse variable in hook callback
- Child theme – copied some files from parent to child website still uses parent files
- Shortcode with custom content attribute?
- Problem with extract() with custom shortcode
- How to override filter in child theme?
- I created a child theme and it doesn’t work for some of the css files
- Shortcode to insert menu in page body?
- add sidebar area to header of child theme
- Displaying a random user with a shortcode
- Unregister Nav Menu with fallback?
- Child Theme Not Overriding Parent Theme
- Passing HTML in WordPress Shortcode arguments
- Adding body class when post contains a specific shortcode
- Can I use require() function in a template file?
- How to set global variables in template page?
- Why isn’t is_page_template() adding a body class?
- Changes to functions.php not working
- Overriding methods in a child theme
- How to override enqueued styles using a child theme
- Should I ask a theme developer to use locate_template rather than require_once
- Dequeue script in template isn’t working
- Function to show only first instance of shortcode
- Display a text message if the shortcode is found?
- Query children and parent title
- Help to resolve Syntax error, unexpected ‘endwhile’ (T_ENDWHILE) [closed]
- Adjust which tempalte a page uses with a function?
- How to change menu icon which is overriden (i.e. by WooCommerce) [closed]
- Using the child theme functions.php to change the customizer.php on parent theme
- Child theme functions.php not executing
- How to store / access files in child theme folder
- override parent theme configuration in child functions.php
- wp_enqueue JavaScript in child-theme (ReferenceError) using Search & Go
- creating shortcode to pull json array
- Overwrite Parent Theme add_image_size in Child Theme
- Retrieve post modified date for specific post by post ID
- Custom Shortcode, functions PHP WP_Query loop
- Proper way of using functions in action hook?
- Pass Shortcode Attribute to footer Script
- Super simple shortcode not working
- Twenty Eleven Child theme error when attempting to use a rewritten function
- How do I get a child theme to load scripts from the parent theme?