Last active
August 29, 2015 14:18
-
-
Save fredbradley/04521a53a22594925ffd to your computer and use it in GitHub Desktop.
Useful Wordpress filter snippet for automatically finding Twitter handles in your content and formatting them as a link with Twitter Web Intents and Fontawesome icons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* How this works: While writing content in the Wordpress editor. Simply add a twitter handle without a link but with the @ symbol. | |
* EG: "Here is some content written by @FredBradley" would turn into "Here is some content written by <i class="fa fa-fw fa-twitter"></i><a href="https://twitter.com/intent/user/?screen_name=@FredBradley">@FredBradley</a>" | |
* | |
* Notes: For this snippet to work 100% as intended it requires two things. | |
* 1. You need to have Font Awesome working on your site. (http://fontawesome.io/) | |
* 2. You need to have called the Twitter Widgets.js javascript for Web Intents to work. More details here: https://dev.twitter.com/web/intents#user-intent | |
* | |
* How to implement: Simply add this snippet into your theme's functions.php file! | |
*/ | |
function fb_format_twitter_handle($content) { | |
$pattern= '/(?<=^|(?<=[^a-zA-Z0-9-_\.]))@([A-Za-z]+[A-Za-z0-9_]+)/i'; | |
$replace= '<i class="fa fa-fw fa-twitter"></i><a href="https://twitter.com/intent/user/?screen_name=$1">@$1</a>'; | |
$content= preg_replace($pattern, $replace, $content); | |
return $content; | |
} | |
add_filter( "the_content", "fb_format_twitter_handle" ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment