0

My theme includes a widget for showing tweets. I want to take this widget and create my own version of it. I have successfully created a new widget listing in the admin but the widget functions from the other file is still being called when the front end is rendered. I confirmed this by putting an echo in the widget() function of the new widget.

See links below for the code for each file:

Original Theme Widget: Full Code - http://pastebin.com/asprtK6h

add_action( 'widgets_init', 'penci_latest_tweets_load_widget' );

function penci_latest_tweets_load_widget() {
    register_widget( 'penci_latest_tweets_widget' );
}

class penci_latest_tweets_widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function penci_latest_tweets_widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'penci_latest_tweets_widget', 'description' => esc_html__('A widget that displays your latest tweets with a slider', 'soledad') );

        /* Widget control settings. */
        $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'penci_latest_tweets_widget' );

        /* Create the widget. */
        global $wp_version;
        if( 4.3 > $wp_version ) {
            $this->WP_Widget( 'penci_latest_tweets_widget', esc_html__('.Soledad Tweets Slider', 'soledad'), $widget_ops, $control_ops );
        } else {
            parent::__construct( 'penci_latest_tweets_widget', esc_html__('.Soledad Tweets Slider', 'soledad'), $widget_ops, $control_ops );
        }
    }

    /**
     * How to display the widget on the screen.
     */
    function widget( $args, $instance ) {
        extract( $args );

        /* Our variables from the widget settings. */
        $title    = apply_filters( 'widget_title', $instance['title'] );
        $date     = isset( $instance['date'] ) ? $instance['date'] : false;
        $auto     = isset( $instance['auto'] ) ? $instance['auto'] : false;
        $reply    = isset( $instance['reply'] ) ? $instance['reply'] : esc_html__( 'Reply', 'soledad' );
        $retweet  = isset( $instance['retweet'] ) ? $instance['retweet'] : esc_html__( 'Retweet', 'soledad' );
        $favorite = isset( $instance['favorite'] ) ? $instance['favorite'] : esc_html__( 'Favorite', 'soledad' );

        $tweets = getTweets(5);

        if( !empty( $tweets ) ):

            /* Before widget (defined by themes). */
            echo ent2ncr( $before_widget );

            /* Display the widget title if one was input (before and after defined by themes). */
            if ( $title )
                echo ent2ncr( $before_title . $title . $after_title );

            if( isset( $tweets['error'] ) ) {
                echo 'Missing consumer key - please check your settings in admin > Settings > Twitter Feed Auth';
            } else {
            ?>
            <div class="penci-tweets-widget-content">
                <span class="icon-tweets"><i class="fa fa-twitter"></i></span>
                <div class="penci-slider penci-tweets-slider" data-smooth="true" data-direction="horizontal" data-auto="<?php if( $auto ){ echo 'false'; } else { echo 'true'; } ?>" data-dir="true" data-control="false" data-autotime="5000" data-speed="500">
                    <ul class="slides">
                        <?php foreach( $tweets as $tweet ):
                        $date_array = explode( ' ', $tweet['created_at'] );
                        $tweet_id = $tweet['id_str'];
                        $tweet_text = $tweet['text'];
                        $urls = $tweet['entities']['urls'];

                        if( isset( $urls ) ) {
                            foreach ( $urls as $ul ) {
                                $url = $ul['url'];
                                if( isset( $url ) ):
                                    $tweet_text = str_replace( $url, '<a href="'. $url .'" target="_blank">'. $url .'</a>', $tweet_text );
                                endif;
                            }
                        }
                        ?>
                            <li class="penci-tweet">
                                <div class="tweet-text">
                                    <?php echo $tweet_text; ?>
                                </div>
                                <?php if( $date_array[1] && $date_array[2] && $date_array[5] && ! $date ): ?>
                                <p class="tweet-date"><?php echo $date_array[2] . '-' . $date_array[1] . '-' . $date_array[5]; ?></p>
                                <?php endif; ?>
                                <div class="tweet-intents">
                                    <div class="tweet-intents-inner">
                                        <span><a target="_blank" class="reply" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $reply ); ?></a></span>
                                        <span><a target="_blank" class="retweet" href="https://twitter.com/intent/retweet?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $retweet ); ?></a></span>
                                        <span><a target="_blank" class="favorite" href="https://twitter.com/intent/favorite?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $favorite ); ?></a></span>
                                    </div>
                                </div>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>

            <?php
            }
        endif; /* End check if array $tweets empty or null */

        /* After widget (defined by themes). */
        echo ent2ncr( $after_widget );
    }
}

New Widget: Full Code - http://pastebin.com/wQ839BNF add_action( 'widgets_init', 'sjcny_latest_tweets_load_widget' );

function sjcny_latest_tweets_load_widget() {
    register_widget( 'sjcny_latest_tweets_widget' );
}

class sjcny_latest_tweets_widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function sjcny_latest_tweets_widget() {
        /* Widget settings. */
        $widget_ops = array( 'classname' => 'sjcny_latest_tweets_widget', 'description' => esc_html__('A widget that displays your latest tweets with a slider', 'soledad') );

        /* Widget control settings. */
        $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'sjcny_latest_tweets_widget' );

        /* Create the widget. */
        global $wp_version;
        if( 4.3 > $wp_version ) {
            $this->WP_Widget( 'sjcny_latest_tweets_widget', esc_html__('SJCNY Tweets Slider', 'soledad'), $widget_ops, $control_ops );
        } else {
            parent::__construct( 'sjcny_latest_tweets_widget', esc_html__('SJCNY Tweets Slider', 'soledad'), $widget_ops, $control_ops );
        }
    }

    /**
     * How to display the widget on the screen.
     */
    function widget( $args, $instance ) {
    echo "asdf1234";
        extract( $args );

        /* Our variables from the widget settings. */
        $title    = apply_filters( 'widget_title', $instance['title'] );
        $date     = isset( $instance['date'] ) ? $instance['date'] : false;
        $auto     = isset( $instance['auto'] ) ? $instance['auto'] : false;
        $reply    = isset( $instance['reply'] ) ? $instance['reply'] : esc_html__( 'Reply', 'soledad' );
        $retweet  = isset( $instance['retweet'] ) ? $instance['retweet'] : esc_html__( 'Retweet', 'soledad' );
        $favorite = isset( $instance['favorite'] ) ? $instance['favorite'] : esc_html__( 'Favorite', 'soledad' );

        $tweets = getTweets(5);

        //if( !empty( $tweets ) ):
    if( true ) :

            /* Before widget (defined by themes). */
            echo ent2ncr( $before_widget );

            /* Display the widget title if one was input (before and after defined by themes). */
            if ( $title )
                echo ent2ncr( $before_title . $title . $after_title );

            if( isset( $tweets['error'] ) ) {
                echo 'Missing consumer key - please check your settings in admin > Settings > Twitter Feed Auth';
            } else {
            ?>
            <div class="penci-tweets-widget-content">
                <span class="icon-tweets"><i class="fa fa-twitter"></i></span>
                <div class="penci-slider penci-tweets-slider" data-smooth="true" data-direction="horizontal" data-auto="<?php if( $auto ){ echo 'false'; } else { echo 'true'; } ?>" data-dir="true" data-control="false" data-autotime="5000" data-speed="500">
                    <ul class="slides">
                        <?php foreach( $tweets as $tweet ):
                        $date_array = explode( ' ', $tweet['created_at'] );
                        $tweet_id = $tweet['id_str'];
                        $tweet_text = $tweet['text'];
                        $urls = $tweet['entities']['urls'];

                        if( isset( $urls ) ) {
                            foreach ( $urls as $ul ) {
                                $url = $ul['url'];
                                if( isset( $url ) ):
                                    $tweet_text = str_replace( $url, '<a href="'. $url .'" target="_blank">'. $url .'</a>', $tweet_text );
                                endif;
                            }
                        }
                        ?>
                            <li class="penci-tweet">
                                <div class="tweet-text">
                                    <?php echo $tweet_text; ?>
                                </div>
                                <?php if( $date_array[1] && $date_array[2] && $date_array[5] && ! $date ): ?>
                                <p class="tweet-date"><?php echo $date_array[2] . '-' . $date_array[1] . '-' . $date_array[5]; ?></p>
                                <?php endif; ?>
                                <div class="tweet-intents">
                                    <div class="tweet-intents-inner">
                                        <!--<span><a target="_blank" class="reply" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $reply ); ?></a></span>-->
                    <span><a target="_blank" class="reply" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo sanitize_text_field( $tweet_id ); ?>">Justin</a>
                                        <span><a target="_blank" class="retweet" href="https://twitter.com/intent/retweet?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $retweet ); ?></a></span>
                                        <span><a target="_blank" class="favorite" href="https://twitter.com/intent/favorite?tweet_id=<?php echo sanitize_text_field( $tweet_id ); ?>"><?php echo sanitize_text_field( $favorite ); ?></a></span>
                                    </div>
                                </div>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </div>
            </div>

            <?php
            }
        endif; /* End check if array $tweets empty or null */

        /* After widget (defined by themes). */
        echo ent2ncr( $after_widget );
    }
}
3
  • You can unregister the other widget. Although I don't quite get what you want to do.
    – Laxmana
    Jul 27 '16 at 20:02
  • That isn't a bad idea. I'm trying to customize that widget without overwriting that one. It's part of the theme the client purchased.
    – Fencer04
    Jul 27 '16 at 20:05
  • Ok I see. If your widget will be the main one and the other widget will never be used then I think you can safe unregister the widget after theme setup. Can't you disable the widget from the widget area inside admin panel or from Customizer ?
    – Laxmana
    Jul 27 '16 at 20:13
0

As I'm not able to comment yet, I can only answer. But I've got questions. How does the widget get used? Where is it called? is the ORIGINAL script you posted above in a file like /theme/twitter-widget.php? Or is it in your functions.php script for the theme?

Something wasn't configured to run YOUR widget, and WordPress is still using the original.

1
  • The original is added by the parent theme functions file and the new one is added by the child theme in the functions file. Both by a PHP include.
    – Fencer04
    Jul 27 '16 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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