actions - why require - does not load filter

I am using the boilerplate for WordPress plugin : but I don't know if it's because of there coding this is ha

I am using the boilerplate for WordPress plugin : /

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;
}

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;
}
Share Improve this question edited Aug 12, 2019 at 15:36 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Aug 12, 2019 at 1:34 user145078user145078
Add a comment  | 

1 Answer 1

Reset to default 1

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248980a4618560.html

相关推荐

  • actions - why require - does not load filter

    I am using the boilerplate for WordPress plugin : but I don't know if it's because of there coding this is ha

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信