
Coding in wordpress is pretty state of the art and the code and logic behind the wp theme is much the same. So you can use many source codes and snippets to save your time while coding for wordpress. We are going to share with you some useful wordpress code snippets to save your time and you can design a great wordpress theme with these handy and ready to work code snippets. So build your wordpress plugins or theme with these ready made code snippets.
Show Tag Cloud
The tag cloud function could be used all around the theme: sidebar, footer and everywhere else. It just returns simple links, which can be styled in any way. Wrap the function in div’s or paragraphs to have the desired styling.
[sourcecode language='php']
‘smallest’ => 10, // size of least used tag
‘largest’ => 18, // size of most used tag
‘unit’ => ‘px’, // unit for sizing
‘orderby’ => ‘name’, // alphabetical
‘order’ => ‘ASC’, // starting at A
‘exclude’ => 6 // ID of tag to exclude from list
)); ?>
[/sourcecode]
Shorten The Excerpt
If you think the Excerpt is too long, it’s easy to shorten to a chosen number of words.
[sourcecode language='php']
function new_excerpt_length($length) {
return 20;
}
add_filter(‘excerpt_length’, ‘new_excerpt_length’);
[/sourcecode]
Separate Trackbacks
The trackbacks might be useful function for thanking the bloggers out there spreading your blogpost, but they are kinda ugly. It’s easy to separate them from the comments.
[sourcecode language='php']
- //Comment code goes here
endforeach;
?>
?>
- foreach ($comments as $comment) : ?>
endforeach;
?>
[/sourcecode]
Share Via Email Link
A simple link that opens users default email client to send an email to a friend or colleague.
[sourcecode language='php']
%20share%20this%20post%20with%20you%20from%20
&body= – ”
title=”Email to a friend/colleague”target=”_blank”>Share via Email”; ?>
[/sourcecode]
List Random Posts
You can display a random post on your blog to make sure none of them are buried and forgotten. Just add the following where you want to show it.
[sourcecode language='php']
-
A random selection of my writing
- $rand_posts = get_posts(‘numberposts=5&orderby=rand’);
- foreach( $rand_posts as $post ) :
- ?>
[/sourcecode]
Paypal Donation Shortcode
This code will make it simpler to accept paypal donations.
[sourcecode language='php']
function donate_shortcode( $atts, $content = null) {
global $post;extract(shortcode_atts(array(
‘account’ => ‘your-paypal-email-address’,
‘for’ => $post->post_title,
‘onHover’ => ”,
), $atts));
if(empty($content)) $content=’Make A Donation’;
return ‘‘.$content.’‘;
}
add_shortcode(‘donate’, ‘donate_shortcode’);
?>
[/sourcecode]
Include Any File
This snippet let’s you include any file you want to without having to print out the whole url yourself. Add this where you want to include the file.
[sourcecode language='php']
get_template_part( ‘filename’ );
[/sourcecode]
List Amount of Post Revisions
WordPress has a great revisioning system that auto-saves your post as you’re working with it. It also allows you to save different drafts and go back to an older one if you want to. Although this is great, the number of revisions often gets carried away, which takes up some database space.
You can easily limit the number of revisions allowed by adding this to wp-config.php:
[sourcecode language='php']
# Maximum 5 revisions #
define(‘WP_POST_REVISIONS’, 5);
# Disable revisions #
define(‘WP_POST_REVISIONS’, false);
[/sourcecode]
Image Custom Field
An easy way to call the a custom field for an image source. Includes IF statement to check if custom field is true.
[sourcecode language='php']
ID, ‘image’, TRUE); ?>
[/sourcecode]
Highlight Author Comment
So you want your comments to look different that the readers? No problem. Find the list-item where the comment content is located (usually in comments.php) and add the following in the class-tag:
[sourcecode language='php']
if (1 == $comment->user_id)
echo ‘author’;
[/sourcecode]
Get Post Category Name – ID
This gives you a way to display the current post’s Category ID and / or Category name. I needed this just for debugging and coding purposes but maybe someone can use it.
[sourcecode language='php']
foreach((get_the_category()) as $category) {
$postcat= $category->cat_ID;
$catname =$category->cat_name;
echo $postcat;
echo $catname;
}
?>
[/sourcecode]
Enable a Specific Custom Field
Enable a custom field of a specific key (custom field name). Just replace the text in the quotes with your custom field name.
[sourcecode language='php']
ID, ‘insert name here’, true); ?>
[/sourcecode]
Edit Navigation Output
This snippet gives you finer control of the output of WordPress navigation menus
Place the snippet in your themes function file and modify the PHP to get the output you want. For example in WordPress 3.0 in the Menu Builder you can add title’s to menu items. Customizing this output I was able to pass the title into the class for more specific CSS customization.
[sourcecode language='php']
class description_walker extends Walker_Nav_Menu
{
function start_el(&$output, $item, $depth, $args)
{
global $wp_query;
$indent = ( $depth ) ? str_repeat( “\t”, $depth ) : ”;
$class_names = $value = ”;
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$class_names = join( ‘ ‘, apply_filters( ‘nav_menu_css_class’, array_filter( $classes ), $item ) );
$class_names = ‘ class=”‘. esc_attr( $class_names );
$class_names .= ! empty( $item->attr_title ) ? ‘ ‘ . esc_attr( $item->attr_title ) .’”‘ : ‘”‘;
$output .= $indent . ‘
foreach ($comments as $comment) : ?>