FriendRetweet: Automatically Retweet Popular Tweets
FriendRetweet is a command-line PHP script that allows you to automatically retweet the most popular tweets of the people you’re following. Each time it is run, the app will scan your users home timeline and find the most popular tweet since the last time it was run. It will then retweet that tweet.
Download
Download the script here: https://github.com/Decidedly/FriendRetweet
Installation
- Create a Twitter app for yourself at https://apps.twitter.com. Make sure you create an access token and set your permissions to read-write as well.
php composer install
cp config/example.config config/username.config
- Configure the app as follows:
Config Key Description consumer_key
Your Twitter consumer key consumer_secret
Your Twitter consumer secret access_token
Your Twitter access token access_token_secret
Your Twitter access token secret twitter_user_id
Your numeric Twitter User id. memory_filename
A path to the file where your user’s data will be stored. These files don’t generally get very big. native_retweets
A value of true causes native tweets, a values of false causes us to simply tweet the same text as the source tweet.
Running the App
From the command-line, type this:
php FriendRetweet.php --config configs/username.config
PHP TIP: A quick, non-RegEx way of replacing an IMG tag’s SRC= attribute
It’s often useful to be able to swap out the src= attribute of an HTML IMG tag without losing any of the other attributes. Here’s a quick, non-regex way of doing this. It uses the PHP DOM API to create a tiny HTML document, then saves the XML for just the IMG element.
function replace_img_src($original_img_tag, $new_src_url) { $doc = new DOMDocument(); $doc->loadHTML($original_img_tag); $tags = $doc->getElementsByTagName('img'); if(count($tags) > 0) { $tag = $tags->item(0); $tag->setAttribute('src', $new_src_url); return $doc->saveXML($tag); } return false; }
Note: In versions of PHP after 5.3.6, $doc->saveXML($tag)
 can be changed to $doc->saveHTML($tag)
.
TIP: Turn WordPress Page Titles On or Off Using Custom Fields
Most WordPress themes display page titles for every page. This is usually what one wants, but sometimes it’s useful to be able to easily turn off WordPress page titles for individual pages but still keep them on by default. This is easy to do with custom fields. For simplicity’s sake, I am going to base these examples on the default WordPress Twenty Twelve theme although the same principle should apply to almost any theme.
The first step is to find where the page title is being generated. In the Twenty Twelve theme, this is in the content-page.php file. Go into content-page.php and find the block of code that looks like this:
<header class="entry-header"> <h1 class="entry-title"><?php the_title(); ?></h1> </header>
and change it to this:
<?php if (get_post_meta($post->ID, 'show_title', true) != "no") {?> <header> <h1><?php the_title(); ?></h1> </header> <?}?>
As you can see we’ve wrapped the <header> tag in an “if” statement. This checks for the presence of a “show_title” custom field for this particular page. If one is present and its value also equals “no,” we don’t show the title. Otherwise, the page title is shown.
The only step left is to open up the editor for the page you want to modify. Go to the bottom of the page to the “Custom Fields” section. (If there is no “Custom Fields” section, you may have to go up to the top, click “Screen Options” and check the “Custom Fields” box.) In the “Name” box, type “show_title.” In the “Value” box, type “no.” Hit “Update.”
That’s it! Your page title should be gone from that page.
–Dave Hilowitz