How can I call template partials inside my custom plugin

All the examples seem to be for including page and post templates in a custom plugin, but that’s not what I need.

I am trying to include template partials that will wrap and style the output from a function in my custom wordpress plugin. Here is my example:

$chunk = ['header'=>'Header','gallery'=>'Gallery','intro'=>'Intro','description'=>'Description','events'=>'Events','virtual_tour'=>'Virtual Tour','map'=>'Map', 'walkscore'=>'Walkscore','crea_disclaimer'=>'CREA Disclaimer'];

$tpl_data = array(
            'property'  => $property,
            'permalink' => get_post_permalink( $post->ID ),
            'post'      => $post
        );

        switch ($chunk) {
            case 'description':
                return '<p class="description">' . $tpl_data['property']['common']['PublicRemarks'] . '</p>';
            case 'header':
                return '
                <h1 class="dr-header">' .
                    $tpl_data['property']['address']['street'] .
                    '<span class="city">' . $tpl_data['property']['address']['city'] . '</span>' .
                '</h1>';
        }

Instead of returning $tpl_data values wrapped in markup, I want to pass $tpl_data into a template partial that theme builders can override.

I’ve tried to use get_template_part like this:

case 'description':
    return get_template_part( plugin_dir_path( __FILE__ ) . 'templates/er-listing-detail', $tpl_data);

I have a ‘templates’ directory in the plugin directory with the correctly named partial in it but it doesn’t return anything.

Is this possible to do in a wordpress plugin?

0

Leave a Comment