I love learning about small hacks that can really tailor WordPress to fit my needs. Often times, plugins are full of extra junk I just don’t need!
Here are five pieces of code that will enable some really cool shortcodes that can be used in your posts and pages!
How awesome is that!?!
Plus, I’ll tell you how you can start taking those functions.php hacks we’ve been talking about, and wrap them into your very own personal plugin file.
1. Display a Snapshot of Any Website
I’m so going to be using this on my next DYECASTING site update! What a great way to render a portfolio?
This might also be interesting to implement into re-posting other blog posts, too.
Drop this into your functions.php:
[cc lang=”php”]function wpr_snap($atts, $content = null) {
extract(shortcode_atts(array(
“snap” => ‘http://s.wordpress.com/mshots/v1/’,
“url” => ‘https://churchm.ag’,
“alt” => ‘My image’,
“w” => ‘400’, // width
“h” => ‘300’ // height
), $atts));
$img = ‘‘;
return $img;
}
add_shortcode(“snap”, “wpr_snap”);[/cc]
Once you’ve dropped this in, you can drop it into any post or page with this shortcode:
[cc lang=”html”][snap url=”http://www.catswhocode.com” alt=”My description” w=”400″ h=”300″][/cc]
I love learning this kid-of stuff!
2. Add a PayPal Donation Link
Again, you’re adding this code to your functions.php file. Remember, when you do this, all of your changes are married to your current blog theme. So, if you change to a different one, copy and paste your changes over to your new theme!
[cc lang=”php”]function cwc_donate_shortcode( $atts ) {
extract(shortcode_atts(array(
‘text’ => ‘Make a donation’,
‘account’ => ‘REPLACE ME’,
‘for’ => ”,
), $atts));
global $post;
if (!$for) $for = str_replace(” “,”+”,$post->post_title);
return ‘‘.$text.’‘;
}
add_shortcode(‘donate’, ‘cwc_donate_shortcode’);[/cc]
Just add this shortcode:
[cc lang=”html”][donate][/cc]
Slick.
3. Obfuscate Email Addresses
I hate SPAM.
This code you drop into your functions.php file. One thing to keep in mind as you do this, those modifications that are locked into your theme, you should drop into your theme’s functions.php file. So, what about those features you want to always have? No matter the theme?
First, the code to obfuscate email addresses:
[cc lang=”php”]function cwc_mail_shortcode( $atts , $content=null ) {
for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';
return '‘.$encodedmail.’‘;
}
add_shortcode(‘mailto’, ‘cwc_mail_shortcode’);[/cc]
Then, drop in your shortcode like so:
[cc lang=”html”][mailto][email protected][/mailto][/cc]
Done.
4. Create Private Content
I bet you thought you needed a plugin to do this, right?
Kinda.
The plugins you use, many of them may simply be functions.php hacks that have been neatly packaged in a plugin. If you are adding a feature to your WordPress site that you want to use, no matter the theme being used, I suggest neatly packaging it in your very own custom plugin!
First, here’s the code to create private content within your site:
[cc lang=”php”]function cwc_member_check_shortcode( $atts, $content = null ) {
if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
return $content;
return ”;
}
add_shortcode( ‘member’, ‘cwc_member_check_shortcode’ );[/cc]
This is how you use the shortcode:
[cc lang=”html”][member]This text will be only displayed to registered users.[/member][/cc]
Pretty sweet, right? I’ve used this on Church websites for pages that were dedicated to downloadable forms and documents. Some of the forms were only available for leadership, so I used this to hide part of the page for those not logged in. 🙂
5. RSS Feed Only Content
You can add a little something special for your RSS subscribers! Just drop this into your … can you guess? … functions.php file!
Of course, something like this you’ll want to do no matter your theme, so this is an example of what you would want to package into your own personalized plugin.
Now for the code!
[cc lang=”php”]function cwc_feedonly_shortcode( $atts, $content = null) {
if (!is_feed()) return “”;
return $content;
}
add_shortcode(‘feedonly’, ‘cwc_feedonly_shortcode’);[/cc]
Drop this into your post …
[cc lang=”html”][feedonly]Dear RSS readers, please visit my website and click on a few ads[/feedonly][/cc]
… and only those reading the post in their Feed Reader will see it!
Functions Turned Custom Plugin
Like I said, there are some functions.php modifications you may want to consider separating out into your own plugin. I’ve now done this on my last three builds and I highly recommend it. Currently, I throw all my modifications into one plugin, while I’ve created a second to test new stuff. That way, I can turn it off and on easily.
I will admit, however, you will feel a mild rush of excitement when you see your own plugin listed along with the others in WordPress.
😉
Any questions?
[via Cats Who Code]
Matthew Snider says
Great tips brother
Eric Dye says
😀
Scott Smith says
Very cool tricks! Keep em coming!
Eric Dye says
Thanks! I plan on it!
James Cooper says
The screenshot one is sweet!
Eric Dye says
I know, right!
Mark Robinson says
that’s really cool stuff!
I would really like to see an simple, step by step guide in how to create plugins and theme options.
Still a tricky one for me to grasp.
Eric Dye says
Cool. I’ll see what we can do. 😉 Love the feedback, Mark!
bungeshea says
Great post. I really love the screenshots snippet; it’d be great for building a portfolio or website showcase.
If you find your functions.php file getting a bit long and messy, or are tired of it being overwritten when you upgrade/change your theme, check out my Code Snippets plugin. It provides an interface in the WordPress administration dashboard for you to add your snippets, and assign them a name and description, and then it actually runs them on your site, just as if they were in a functions.php file! If that sounds good to you, you can download it from WordPress.org, or learn more at the plugin’s website.
Eric Dye says
Or use your own functionality plugin. 🙂