Theme Partial Shortcode

Have you ever been building a website using Divi, Elementor, or any of the other popular page builder tools and wished that you could just add some custom PHP without needing to go through the whole process of creating a custom module? Just me? Well, there are probably other uses for this snippet as well I guess. haha

I use a snippet similar to this in place of custom modules in much of my development. 

The following code snippet will allow you to add everything from custom queries, headers, or anything you can think of to any part of any page that supports shortcodes. 

We start with creating the shortcode. 

/************
 * Theme Partials Shortcode
 * -----------------
 * Built the navigation as a partial so that updates to Divi won't break.
 * This keeps us out of the header.php file and instead use the Theme Builder tool from Divi
 * Use [partial name='header']
 *  *******************/
add_shortcode('partial', 'template_partial');
function template_partial($atts) {
    extract(shortcode_atts(array(
        'dir' => 'partials',
        'name' => FALSE
    ), $atts));
    if ( $name ) {
        ob_start();
        get_template_part( $dir . '/' . $name );
        return ob_get_clean();
    }
}
 
This example was for a custom header years ago. We wanted to create a custom header that Divi wouldn't nuke on an update. Or to be more accurate, we wanted one that wouldn't nuke Divi. 
 
The accompanying code is just a php file called "header.php" in a folder called "partials" inside of the child theme. 
 
I then used the shortcode [partial name='header'] to display this. 
 
Hope this helps you in your Divi development! If all this seems like too much, consider reaching out to us. We love building websites that work!

Table of Contents

Stay In The Know!

Fill out this form to get blog posts similar to this one sent to your inbox at a frequency you're comfortable with!

Subscribe
We have to pay the bills too. Ads help a little. Thank you for lending us your eyes.