自定义小工具在放置在其自己的PHP文件中时创建PHP异常

时间:2015-07-24 作者:user1889580

我创建了一个自定义插件和一个小部件。目前,该代码包含在插件的基本php文件中,并且工作正常。我想将小部件和所有未来的小部件移动到一个小部件中。php文件,并将其包含到主插件文件中。当我这样做时,我得到以下php错误。

PHP致命错误:调用/var/www/wordpresstest/wp includes/capabilities中未定义的函数wp\\u get\\u current\\u user()。php在线1387

这是小部件的代码。当我将此代码放在主插件php文件中时,它可以完美地工作,但当我使用require“widgets.php”时,我会出现错误。

<?php 
class nb_game_info_widget extends WP_Widget 
{
    function __construct() 
    {
        parent::__construct(\'nb_game_info_widget\', __(\'Test Widget\', \'nb_game_info_widget_domain\'), array( \'description\' => __( \'Game IDS Widget\', \'nb_game_info_widget_domain\' )));
    }

    public function widget( $args, $instance ) 
    {
        if(is_single())
        {
            $title = apply_filters( \'widget_title\', $instance[\'title\'] );
            echo $args[\'before_widget\'];
            if ( ! empty( $title ) )
            echo $args[\'before_title\'] . $title . $args[\'after_title\'];

            global $post;
            $postid = $post->ID;

            $gameids = get_post_meta( $postid, \'nb_gameids_key\', true );
            $platformids = get_post_meta( $postid, \'nb_platformids_key\', true );
            echo "GAME IDS ! = ". $gameids;

            //required for the theme to do whatever it does
            echo $args[\'after_widget\'];
        }
    }

    // Widget Backend 
    public function form( $instance ) {
        if ( isset( $instance[ \'title\' ] ) ) 
        {
            $title = $instance[ \'title\' ];
        }
        else {
            $title = __( \'New title\', \'nb_game_info_widget_domain\' );
        }
        // Widget admin form
        ?>
        <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </p>
        <?php 
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) 
    {
        $instance = array();
        $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
        return $instance;
    }
} // Class nb_game_info_widget ends here

// Register and load the widget
function nb_loag_gameinfo_widget() {
    register_widget( \'nb_game_info_widget\' );
}
add_action( \'widgets_init\', \'nb_loag_gameinfo_widget\' );

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

I have no idea why this should solve the issue but I can solve it. Use a different file name. For example: include(\'my_widgets.php\');

I get the error you describe with include(\'widgets.php\'); but not with a different file name. I don\'t know why. I do know that WordPress has a Core file named widgets.php but I can\'t work out why there should be a conflict. It must have to do with the way that include crawls for files but I\'d have to do some research-- get under the hood, so to speak. Moral of the story: Don\'t use generic file names :)

结束