Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This widget code is causing my WP site to white screen. I've re-factored the code about 5 times trying to solve it, but no luck.

<?php

    class latest_mag_issue_widget extends WP_Widget {

        function latest_mag_issue_widget() {
            $widget_ops = array('classname' => 'widget_latest_issues', 'description' => 'Latest Mag Issues' );
            $this->WP_Widget('latest_issues', 'Latest Issues', $widget_ops);
        } //end constructor

        function widget( $args, $instance ) {
            extract( $args );

            // widget options
            $title = apply_filters( 'widget_title', $instance['title'] );
            $pastissues = $instance['pastissues'];
            $thisissue = $instance['thisissue'];

            echo $before_widget;

            echo '<div class="widget widget-latest-issue">';

            if ( $title ) {
                echo $before_title . $title . $after_title;
            }

            wp_reset_query();
            $wp_query = new WP_Query();
            $wp_query->query(array( 'post_type' => 'mag-issue', 'post_status' => 'publish', 'paged' => 1, 'posts_per_page' => 1));

            if ( have_posts() ) : while ( have_posts() ) : the_post();

            $image_url = get_post_meta($post->ID, 'press_smart', true);
            $image_title = the_title();
            $image_thumbnail = the_post_thumbnail( array(125,166) );

            echo '
                <div id="latest-issue">
                    <h3>Latest Issue</h3>
                    <a class="fl" href="' . $image_url . '" title="' . $image_title .'">' . $image_thumbnail . '</a> <br/>
                </div>
                ';

                if ( $thisissue == true) { 

                echo '
                    <div id="this-issue">
                        <h3>In This Issue</h3>
                        <ul>';

                            for ($i=1; $i < 10; $i++) { 
                                $headline_meta = get_post_meta($post->ID , 'headline_' . $i  , true);
                                if ( $headline_meta ) { echo '<li>' . $headline_meta . '</li><hr>';}
                            } 

                            endwhile; endif; wp_reset_query();
                    echo '
                        </ul>
                    </div>
                    ';

                }

                if ( $pastissues == true ) { 

                    $wp_query->query(array( 'post_type' => 'mag-issue', 'post_status' => 'publish', 'paged' => 1, 'posts_per_page' => 9, 'offset' => 1 ));

                    if ( have_posts() ) : while ( have_posts() ) : the_post();

                    $post_url = get_post_meta( $post->ID, 'press_smart', true );
                    $post_title = the_title();

                echo '
                    <div id="past-issues">
                        <h3>Past Issues</h3>
                        <ul>        
                            <li>
                                <a href="' . $post_url  . '">' . $post_title . '</a>
                            </li>';

                    endwhile; endif; wp_reset_query();

                    echo '
                        </ul>
                    </div>';

                } 
            echo '</div>';

            echo $after_widget; 


        } //end function widget()

        function update( $new_instance, $old_instance ) {
            $instance = $old_instance;
            $instance['title'] = strip_tags( $new_instance['title'] );
            $instance['thisissue'] = strip_tags( $new_instance['thisissue'] );
            $instance['pastissues'] = strip_tags( $new_instance['pastissues'] );
            return $instance;
        } //end function update()


        function form($instance) {

            $title = esc_attr( $instance['title'] );
            $thisissue = esc_attr( $instance['thisissue'] );
            $pastissues = esc_attr( $instance['pastissues'] );

            ?>

            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget 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 $title; ?>" />
            </p>

            <p>
                <input id="<?php echo $this->get_field_id('thisissue'); ?>" name="<?php echo $this->get_field_name('thisissue'); ?>" type="checkbox" value="1" <?php checked( '1', $thisissue ); ?>/>
                <label for="<?php echo $this->get_field_id('thisissue'); ?>"><?php _e('Display headlines?'); ?></label>
            </p>

            <p>
                <input id="<?php echo $this->get_field_id('pastissues'); ?>" name="<?php echo $this->get_field_name('pastissues'); ?>" type="checkbox" value="1" <?php checked( '1', $pastissues ); ?>/>
                <label for="<?php echo $this->get_field_id('pastissues'); ?>"><?php _e('Display past issues?'); ?></label>
            </p>

            <?php
        } //end function form()

    }

    // Register the widget
    add_action( 'widgets_init', 'register_mag_issues_widget' );
    function register_mag_issues_widget() {
        register_widget( 'latest_mag_issues_widget' );
    }

?>

Thanks,

Dom

share|improve this question
    
Nothing in the error logs to start from? –  Scott Hack Oct 18 '12 at 7:26
    
@Scott Hack Welcome to WPSE. Sorry to nag, but be so kind as to only answer a question with an answer to the OPs problem. Use the comments, if you want to ask for more detail. This is a Q&A site and not a forum. Thank you. –  Johannes Pille Oct 18 '12 at 7:30
add comment

1 Answer

up vote 0 down vote accepted
register_widget( 'latest_mag_issues_widget' );

should be

register_widget( 'latest_mag_issue_widget' );


Update
After fixing that though, there are a couple of other errors. Here's the complete updated copy of your code that I could get working:

class latest_mag_issue_widget extends WP_Widget {

    function latest_mag_issue_widget() {
        $widget_ops = array( 'classname' => 'widget_latest_issues', 'description' => 'Latest Mag Issues Widget for Media24 Business & Customs' );
        $this->WP_Widget( 'latest_issues', 'Latest Issues', $widget_ops );
    } //end constructor

    function widget( $args, $instance ) {
        extract( $args );

        // widget options
        $title = apply_filters( 'widget_title', $instance['title'] );
        $pastissues = $instance['pastissues'];
        $thisissue = $instance['thisissue'];

        echo $before_widget;

        echo '<div class="widget widget-latest-issue">';

        if ( $title ) {
            echo $before_title . $title . $after_title;
        }

        $wp_query = new WP_Query();
        $wp_query->query( array( 'post_type' => 'mag-issue', 'post_status' => 'publish', 'paged' => 1, 'posts_per_page' => 1 ) );

        if ( have_posts() ) : while ( have_posts() ) : the_post();
            $image_url = get_post_meta( $post->ID, 'press_smart', true );
            $image_title = the_title();
            $image_thumbnail = the_post_thumbnail( array(125,166) );

            echo '
                <div id="latest-issue">
                    <h3>Latest Issue</h3>
                    <a class="fl" href="' . $image_url . '" title="' . $image_title .'">' . $image_thumbnail . '</a> <br/>
                </div>
            ';

            if ( $thisissue == true ) {
                echo '
                    <div id="this-issue">
                        <h3>In This Issue</h3>
                        <ul>';

                        for ( $i = 1; $i < 10; $i++ ) {
                            $headline_meta = get_post_meta( $post->ID , 'headline_' . $i  , true );
                            if ( $headline_meta ) { echo '<li>' . $headline_meta . '</li><hr>';}
                        }

                echo '
                        </ul>
                    </div>
                ';
            }

            if ( $pastissues == true ) {

                $wp_query->query(array( 'post_type' => 'mag-issue', 'post_status' => 'publish', 'paged' => 1, 'posts_per_page' => 9, 'offset' => 1 ));

                if ( have_posts() ) : while ( have_posts() ) : the_post();

                    $post_url = get_post_meta( $post->ID, 'press_smart', true );
                    $post_title = the_title();

                    echo '
                    <div id="past-issues">
                        <h3>Past Issues</h3>
                        <ul>
                            <li>
                                <a href="' . $post_url  . '">' . $post_title . '</a>
                            </li>';
                    echo '
                        </ul>
                    </div>
                    ';

                    endwhile; endif; wp_reset_postdata();
            }
        echo '</div>';

        echo $after_widget;

        endwhile; endif; wp_reset_query();
    } //end function widget()

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['thisissue'] = strip_tags( $new_instance['thisissue'] );
        $instance['pastissues'] = strip_tags( $new_instance['pastissues'] );
        return $instance;
    } //end function update()


    function form($instance) {

        $title = esc_attr( $instance['title'] );
        $thisissue = esc_attr( $instance['thisissue'] );
        $pastissues = esc_attr( $instance['pastissues'] );

        ?>

        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget 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 $title; ?>" />
        </p>

        <p>
            <input id="<?php echo $this->get_field_id('thisissue'); ?>" name="<?php echo $this->get_field_name('thisissue'); ?>" type="checkbox" value="1" <?php checked( '1', $thisissue ); ?>/>
            <label for="<?php echo $this->get_field_id('thisissue'); ?>"><?php _e('Display headlines?'); ?></label>
        </p>

        <p>
            <input id="<?php echo $this->get_field_id('pastissues'); ?>" name="<?php echo $this->get_field_name('pastissues'); ?>" type="checkbox" value="1" <?php checked( '1', $pastissues ); ?>/>
            <label for="<?php echo $this->get_field_id('pastissues'); ?>"><?php _e('Display past issues?'); ?></label>
        </p>

        <?php
    } //end function form()

}

// Register the widget
add_action( 'widgets_init', 'register_mag_issues_widget' );
function register_mag_issues_widget() {
    register_widget( 'latest_mag_issue_widget' );
}
share|improve this answer
    
In case anyone missed the error in the function name initially it was the case of a rogue s. @stealthyninja do be sure to list the other errors you found in his code so that it may assist others if ever applicable... –  userabuser Oct 18 '12 at 8:18
    
Thanks. Still getting the white screen though. Could it be because I am using require_once to pull the widget in? –  Dom Oct 18 '12 at 8:25
    
I think the best would be to temporarily activate WP_DEBUG in your wp-config.php (set define('WP_DEBUG', true);). You'll see a bunch of notices, just pay attention to the ones which point or refer to the file(s) you're working on. –  stealthyninja Oct 18 '12 at 8:30
    
Okay will do, thanks. I noticed I also used wp_reset_query() where wp_reset_postdata is more appropriate. –  Dom Oct 18 '12 at 8:31
1  
Okay, so it seems it came down to the rogue 's' and the way I was including the widget. Thanks for your help, would throw you an upvote if I had enough rep :) –  Dom Oct 18 '12 at 8:41
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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