WordPress.org

Ready to get started?Download WordPress

Forums

Twitter Widget Pro
Possible to exclude tweets with certain keywords/hashtags? (3 posts)

  1. Number 6
    Member
    Posted 1 year ago #

    Hi,
    Just wondering if anyone knows where in the PHP for Twitter Widget Pro I should be concentrating on to alter the code to exclude tweets based on a keyword or hashtag?

    I use TWP on the homepage of a website and it would be great if I could have things so that if I tweet something with a particular word or hashtag, it doesn't show up on the website (similar to how @replies can be excluded).

    Thanks

    http://wordpress.org/extend/plugins/twitter-widget-pro/

  2. Number 6
    Member
    Posted 1 year ago #

    Been doing some digging in the code, it looks like it's maybe doable via the filter widget_twitter_content

  3. Number 6
    Member
    Posted 1 year ago #

    Well, I sort of got things working by adding a new filter which I called "widget-twitter-filter-tweets" to keep it in the same naming convention as the ones native to TWP.

    Bear in mind that this is for version 2.5.3 and the line numbers could well be different in other versions.
    Also remember that since this is modifying the plugin code, the modifications will be lost when you upgrade or reinstall TWP.

    In the file wp-twitter-widget.php:

    On line 264 I added:
    add_filter( 'widget-twitter-filter-tweets', array( $this, 'filter_tweets' ) );
    (this was after add_action( 'widgets_init', array( $this, 'register' ), 11 ); and before add_filter( 'widget_twitter_content', array( $this, 'linkTwitterUsers' ) );)

    On line 863 I added:
    $tweets = apply_filters( 'widget-twitter-filter-tweets', $tweets);
    (this was after $tweets = $this->_getTweets( $args ); and before if ( false === $tweets )

    then in my functions.php I can write a bit of code to remove the required tweets from the displayed feed like so:

    function mysite_filter_tweets($tweets) {
        $filteredTweets = array();
        foreach ($tweets as $tweet) {
    	/* Decide which tweets to keep/get rid of here.
    	 * e.g. to exclude all tweets beginning with "RT ":
    	 */
    	$excludeThis = 'RT';
    
    	$result = substr_count($tweet->text, $excludeThis, 0, strlen($excludeThis));
    	if ($result == 0) {
    	    $filteredTweets[] = $tweet;
    	}
        }
        return $filteredTweets;
    }
    add_filter('widget-twitter-filter-tweets', 'mysite_filter_tweets');

    The "RT" stuff is just an example since TWP already provides an option to remove retweets, but hopefully it gives you the idea.

    This works, however let's say you've got TWP set to display 5 tweets, and this filter removes 2 of them - you will only see 3 tweets displayed, because the filtering is being doing after the tweets are retrieved from Twitter. I don't know of a way to get Twitter to then retrieve an additional 2 tweets and add them onto the end of the array, to make up the difference for the filtered out tweets.

    Hopefully this will help somebody anyway.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic