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' );