1

I have a custom plugin created and i have created a widget. At the moment this code is included in the base php file for the plugin and its working fine. I want to move the widget and all future ones into a widgets.php file and include them into the main plugin file. When i do this i get the following php error.

PHP Fatal error: Call to undefined function wp_get_current_user() in /var/www/wordpresstest/wp-includes/capabilities.php on line 1387

This is the code for the widget. This code works perfectly when i place it in the main plugin php file, but when i use require "widgets.php" i get the error.

<?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 Answer 1

2

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 :)

5
  • 1
    thank you sir! i appreciate you taking the time to test this one out. i have been trying to figure out why this happens as i always include my classes in external files. Commented Jul 24, 2015 at 14:27
  • 1
    Damn, this is freaking arbitrary. Commented Jul 24, 2015 at 14:28
  • Anybody have any ideas why this works, I'm all ears :)
    – s_ha_dum
    Commented Jul 24, 2015 at 14:33
  • 1
    Just a wild speculation based on taking a look at the PHP manual page for include: »Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.« So I'm guessing it is something about the include_path configuration. Commented Jul 24, 2015 at 14:46
  • 2
    Note: @Rarst Has given a plausible explanation on another appearance of this problem. Commented Aug 27, 2015 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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