0

I am using the boilerplate for WordPress plugin : https://wppb.me/

but I don't know if it's because of there coding this is happening.

What I am trying to do is use require and load the below code from a file.

require plugin_dir_path( __FILE__ ) . 'admin/partials/ggowl_template/ggowl_template_func.php';

if I am putting this code in the main file of the plugin it works (without require). Not sure where I am making the mistake


add_filter('template_include', 'ggowl_template_post_function',-20,1);

function ggowl_template_post_function($single) {
        //$ggowl_admin_options = get_option('ggowl_admin_options');
        // if( null !== get_option('ggowl_admin_options')){
        if( false !== get_option('ggowl_admin_options')){
            global $post;
            if( !is_object($post) ){
            return $single;
            }

            $ggowl_admin_options = get_option('ggowl_admin_options'); // Array of All Options
                $ggowl_admin_template_options = $ggowl_admin_options['ggowl_select_post_template_repeater'];
                foreach ($ggowl_admin_template_options as $key => $value) {
                    $posttype = $value[0];
                    // var_dump($posttype);
                    $ggowl_select_post_template_0 = (int)$value['template'][0][0];
                    if ( ($post->post_type == $posttype )&&($ggowl_select_post_template_0 != 0) ) {
                            if ( file_exists( plugin_dir_path( __FILE__ ) . 'includes/templates/ggowl-template-post.php' ) ) {
                                    return plugin_dir_path( __FILE__ ) . 'includes/templates/ggowl-template-post.php';
                            }
                    }
                }
        }
    return $single;
}
2

The filter itself probably works, but the file_exists() returns false.

So not sure if this answers the question, but one issue I see in your code is:

When you put the code in your-plugin/admin/partials/ggowl_template/ggowl_template_func.php, plugin_dir_path( __FILE__ ) from within the ggowl_template_post_function() would be your-plugin/admin/partials/ggowl_template/ — and not the main plugin file path, so I suggest you to define the main plugin file (its absolute path) in the main plugin file, like so:

<?php
/**
 * Plugin Name: My Plugin
 * Version: 1.0
 */

defined( 'ABSPATH' ) || exit;

define( 'MY_PLUGIN_PLUGIN_FILE', __FILE__ );

And from other PHP files than the main plugin file, you'd use MY_PLUGIN_PLUGIN_FILE and not __FILE__ when referencing to the main plugin file.

  • Works Perfectly :). I kept thinking that the problem was with filter – Latheesh V M Villa Aug 12 '19 at 15:43
  • 1
    Well, it worked when not being required, hence the problem shouldn't be that the filter is not being applied (i.e. the callback is not being called). And if you didn't check if the template file exists, you would've seen the problem.. :) – Sally CJ Aug 12 '19 at 16:09

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.