Custom widget code generating exception when placed in an included php file
-
I have created a custom plugin. In the main plugin.php file i have been including files that contains code. so require “customPostTypes.php”; require “customMetaFields.php”; This is working fine for everything so far, but im not able to do this with a widget. The code works perfectly when used in the base plugin.php file, bu when i move the code to widgets.php and use a require to use it 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 only a basic plugin. It doesnt do anything fancy at the moment, I wanted to get the structure perfect before i start adding any fancy output. Here is the plugin code. This code works when in plugin.php, but if i add require”widgets.php” and put this code into that file it fails.
<?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’ );
- The topic ‘Custom widget code generating exception when placed in an included php file’ is closed to new replies.