WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] After adding Widget in theme, getting warning (5 posts)

  1. shikhadas
    Member
    Posted 1 year ago #

    I am creating a theme and after adding a widget file to the theme, i am getting the below error.

    Warning: Cannot modify header information - headers already sent by (output started at C:\xampplite\htdocs\director\wp-content\themes\Director\director-widgets.php:71) in C:\xampplite\htdocs\director\wp-includes\pluggable.php on line 876

    My widget file is below -

    <?php
    class Director_Featured_Widget extends WP_Widget {
    
    	public function __construct() {
    		parent::__construct(
    	 		'director_featured_business', // Base ID
    			'Featured Business', // Name
    			array( 'description' => __( 'Displays the Featured Business')) // Args
    		);
    	}
    
    	public function update( $new_instance, $old_instance ) {
    		$instance = array();
    		$instance['title'] = strip_tags( $new_instance['title'] );
    
    		return $instance;
    	}
    
    	public function form( $instance ) {
    		$title = (isset( $instance[ 'title' ])) ? $instance[ 'title' ] : 'Featured Business';
    
    	?>
    		<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
    	}
    
    	public function widget( $args, $instance ) {
    		extract( $args );
    		$title = apply_filters( 'widget_title', $instance['title'] );
    
    		echo $before_widget;
    		if ( ! empty( $title ) ) echo $before_title . $title . $after_title;
    
    		$args= array(
    			'post_type' => 'businesses',
    			'posts_per_page' => 1,
    			'tax_query' => array(
    					array(
    						'taxonomy' => 'business-type',
    						'field' => 'slug',
    						'terms' => 'featured'
    						)
    					)
    			);
    
    	$featuredWidget= new WP_Query($args);
    
    	while ( $featuredWidget->have_posts() ) : $featuredWidget->the_post(); ?>
    
    	<div class="widget_featured">
    		<div class="thumb"><?php print get_the_post_thumbnail($post->ID); ?></div>
    
    		<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    		<?php the_excerpt(); ?>
    	</div>
    
    		<?php
    	endwhile;
    
    	wp_reset_postdata();
    
    		echo $after_widget;
    	}
    
    }
    register_widget( 'Director_Featured_Widget' );
    ?>

    and i am adding below line in function.php to call it

    require_once('director-widgets.php');

    I am not finding any solution anywhere and a temporary solution is to add below in wp-config.php which i don't want to use as i have to submit it to wordpress and can't ask users of theme to do so.

    ob_start();
    error_reporting(0);

    Please help me as this is my first theme.

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    Are you using this theme: http://www.directortheme.com/

  3. shikhadas
    Member
    Posted 1 year ago #

    Nope..mine is a different theme. I myself created it using whiteboard framework. I still have not submitted it.

    Everything went well, till adding of the widget file director-widgets.php

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    All I can suggest is that you try reviewing Solving "headers already sent" warnings.

  5. shikhadas
    Member
    Posted 1 year ago #

    Thanks for the suggestion. My theme is working fine after i did this in my director-widgets.php

    Place the cursor between the ? and >
    Now press the DELETE key on your computer
    Note to MAC users: The "DELETE" key on a PC deletes characters to the right of the cursor. That is the key noted here.
    Keep that key pressed
    For at least 15 seconds
    Now type > and
    save without pressing any other key at all.

Topic Closed

This topic has been closed to new replies.

About this Topic