Yesterday, I received a support request asking about how somebody could parse a URL (Uniform Resource Locator) out of their post title and post that as the shortened URL attached to their Tweet. They always used the same format to add the URL — <a href="">The Title</a>.

Well. Not a standard use, certainly — but it is something that you can do using WP to Twitter, with a little custom code.

So this is a good time to write up the custom template tag filter in WP to Twitter. It’s designed to take the value of a custom field and modify it; but it can be used to add almost any custom data to your Tweet.

The filter is wpt_custom_shortcode, and accepts three arguments: the original value coming from the custom field defined in your custom template tag (such as [[custom_field]]), the ID of the current post, and the name of the custom field.

So, if you’re adding a custom template tag like [[custom_field]], the default is to simply grab the singular value of that custom field and add it to your Tweet. if the field is an array, this breaks. If the field doesn’t exist, nothing happens – normally.

However, you can use this filter to custom define almost anything custom field and make it produce the desired result, even if no custom field by that name exists.

In this case, the total code (placed in your theme’s functions.php file) is this:

add_filter( 'wpt_custom_shortcode', 'my_url', 10, 3 );
function my_url( $value, $post_ID, $field ) {
    if ( $field == 'my_url' ) {
        $title = get_the_title( $post_ID );
        preg_match( '/<a href="(.+)">/', $title, $match );
        $url = $match[1];
        return apply_filters( 'wptt_shorten_link', $url, strip_tags( $title), $post_ID, false );
    return $value;

Line by line, this adds the filter with the function my_url as a callback. That function, when it detects a field named ‘my_url’, parses the title, gets the URL, then sends that URL through the shortener filter.

This isn’t exactly a richly featured regex – but regex is not exactly ideal for parsing HTML (HyperText Markup Language), and this particular user only needed to be able to isolate a URL between double quotes with no other attributes, so it worked for that.

The point, however, is that you can return just about any data — or customize the format of your data — using this filter.

I described another example of this filter in a WordPress support forums thread that you may find interesting.

Another simple example somebody requested via support was to generate a timestamp. WP to Twitter includes a date field by default, but they needed the actual time — so I suggested this, for a custom timestamp:

add_filter( 'wpt_custom_shortcode', 'my_timestamp', 10, 3 );
function my_timestamp( $value, $post_ID, $field ) {
    if ( $field == 'timestamp' ) {
        return date( 'h:i a' );
    return $value;

Using custom filters in WordPress is a great way to extend and expand the capabilities of many plugins and themes — hopefully, you’ll be able to benefit from this tip for WP to Twitter!