How do I override a template file in a Plugin?

I want to override a template file in WooCommerce products slider plugin. The path for the PHP-file is plugins/woocommerce-products-slider/templates/wcps-title.php

How can I do this?

I've tried to do the same path structure in my child theme, but it doesn't work.

This is the code from the original file:

<?php 

/** 
* @Author         ParaTheme 
* Copyright:     2015 ParaTheme 
*/ 

if ( ! defined('ABSPATH')) exit;  // if direct access  

  $title_text = apply_filters( 'wcps_filter_title', get_the_title(get_the_ID()) ); 

  $html.= '<div class="wcps-items-title" >
      <a style="color:'.$wcps_items_title_color.';font-size:'.$wcps_items_title_font_size.'" href="'.$permalink.'">
        '.$title_text.'
      </a>
  </div>'; 

And I want to add <span class="set-field">'. get_field('set') .'</span> to the code so it looks like:

<?php 

/** 
* @Author         ParaTheme 
* Copyright:     2015 ParaTheme 
*/ 

if ( ! defined('ABSPATH')) exit;  // if direct access  

  $title_text = apply_filters( 'wcps_filter_title', get_the_title(get_the_ID()) ); 

  $html.= '<div class="wcps-items-title" >
      <a style="color:'.$wcps_items_title_color.';font-size:'.$wcps_items_title_font_size.'" href="'.$permalink.'">
        <span class="set-field">
            '. get_field('set') .'
        </span>
        '.$title_text.'
      </a>
  </div>';

Upvotes: 0

Views: 1470

Answers (1)

Kevin Greene
Kevin Greene

Reputation: 830

I often use a function in my plugin file to handle specific templates to prevent it being overwritten.

add_filter( 'template_include', 'assign_template');

function assign_template($template){
    if($template == 'old_template.php'){
        $template = get_stylesheet_directory() . '/woocommerce-products-slider/wcps-title-custom.php';
   }
   return $template;
}

Upvotes: 0

Related Questions