1

I've Developed Plugin for using all WordPress Themes. Now depending on Themes requirements I need to change some files on some files. But I don't wants to touch my Plugin. I've used

add_theme_support('jeweltheme-core');
I've copied Plugins files and Folders the same way on Plugin. I need to Edit "Widget" files. I cann't find any way to resolve this problem.

Example: We overrides "WooCommerce" Templates using "templates" directory copying on our Theme and renamed it to "woocommerce". I want exact like this solution.

Thanks

  • 1
    totally unclear question to me. – Arsalan Mithani Feb 25 '17 at 19:28
  • Updated with an example. I want to do something like "wocommerce" plugin does with template overrides. Hope it's clear now. – Liton Arefin Feb 27 '17 at 7:04
  • 1
    I also have no idea what is it that you want to do, and what actual problems you are facing. – Mark Kaplun Feb 27 '17 at 7:50
1

If you've written the plugin, then I'd suggest you add something like this:

    // Define these constants once in your main plugin init file
    define( 'YOUR_PLUGIN_SLUG', 'your-plugin-slug' );
    define( 'YOUR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    define( 'YOUR_PLUGIN_TEMPLATE_DIR', trailingslashit( YOUR_PLUGIN_DIR ) . 'templates' );
    define( 'YOUR_PLUGIN_THEME_TEMPLATE_OVERRIDE_PATH_DIR', trailingslashit( get_stylesheet_directory() . '/' . YOUR_PLUGIN_SLUG );

    // Define a function to locate template files
    function your_plugin_name_get_template_part_location( $part ) {
      $part = $part '.php';

      // Look in the user's theme first for the file
      if ( file_exists( YOUR_PLUGIN_THEME_TEMPLATE_OVERRIDE_PATH_DIR . $part ) ) {
        $file = YOUR_PLUGIN_THEME_TEMPLATE_OVERRIDE_PATH_DIR . $part;
      } 
      // Otherwise use the file from your plugin
      else {
        $file = YOUR_PLUGIN_TEMPLATE_DIR . $part;
      }
    }

First, you define some constants (do this once in your main plugin init file), which you might already have.

Then the function allows you to call files (in my example, template files inside /plugins/your-plugin-name/templates).

For example, you could use

get_template_part(your_plugin_name_get_template_part_location('widget'));

Your plugin will first look in the user's theme to see if an override is in place.

If this DOESN'T exist, then it will look in your plugin.

Of course, you can modify this to fit your own directory structure and needs.

  • Thanks a lot for your help. My problem is solved. You've understood my problems and given the correct solution. – Liton Arefin Feb 27 '17 at 10:41
  • Great news - glad I could help :-) Would you mind accepting the answer as the correct solution? – Doug Belchamber Feb 27 '17 at 15:10
1

I've made my solution like this:


    function jeweltheme_core_get_template_path($template){
        $located = '';

        $template_slug = rtrim( $template, '.php' );
        $template = $template_slug . '.php';

        $this_plugin_dir = WP_PLUGIN_DIR.'/'.str_replace( basename( __FILE__), "", plugin_basename(__FILE__) );

        if ( $template ) {
            if ( file_exists(get_stylesheet_directory() . '/jeweltheme-core/' . $template)) {
                $located = get_stylesheet_directory() . '/jeweltheme-core/' . $template;
            } else if ( file_exists(get_template_directory() . '/jeweltheme-core/' . $template) ) {
                $located = get_template_directory() . '/jeweltheme-core/' . $template;
            } else if ( file_exists( $this_plugin_dir .  $template) ) {
                $located =  $this_plugin_dir . $template;
            }
        }

        return $located;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.