FriendRetweet: Automatically Retweet Popular Tweets

Posted on February 15, 2015 by David Hilowitz

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

  1. 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.
  2. php composer install
  3. cp config/example.config config/username.config
  4. Configure the app as follows:
    Config KeyDescription
    consumer_keyYour Twitter consumer key
    consumer_secretYour Twitter consumer secret
    access_tokenYour Twitter access token
    access_token_secretYour Twitter access token secret
    twitter_user_idYour numeric Twitter User id.
    memory_filenameA path to the file where your user’s data will be stored. These files don’t generally get very big.
    native_retweetsA 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

Posted on June 25, 2013 by David Hilowitz

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

Posted on April 25, 2013 by David Hilowitz

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