While there are 26,410 plugins available in WordPress plugins respiratory which have been downloaded 500,583,147 times and even still counting, but still what you in seek of from time to time is some short codes – snippets – which by adding your theme files will give the desired result. These short codes can be of anything, depends in particularly that what you are planning to achieve by this. To ease your job of finding the right “required” snippet for your WordPress site, we have collected the best those snippets for you. We have divided the article in three-part series so that you can have all in your collection equally.
To check if the current post is a custom post type
Simply paste the code into your functions.php file and you will be able to check that if the current post is a custom post type or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
function is_custom_post_type() { global $wp_query; $post_types = get_post_types(array('public' => true,'_builtin' => false),'names','and'); foreach ($post_types as $post_type ) { if (get_post_type($post_type->ID) == get_post_type($wp_query->post->ID)) { return true; } else { return false; } } } |
While add the function can be used outside the loop:
1 2 3 4 5 |
if (is_custom_post_type()) { //Current post is a custom post type } |
Source: Snipplr
View all WP query variables
On pasting the code below on any files, you will be able to display the WP Query variables.
1 2 3 |
global $wp_query; var_dump($wp_query->query_vars); |
Source: WP-Mix
Conditional tag for blog-related pages
Adding the code into your functions.php template will let you know that whether you are on blog-related pages or not.
1 2 3 4 5 6 7 8 9 |
function is_blog() { if (is_home() || is_singular('post') || is_post_type_archive('post')) return true; else return false; } |
Source: ClickNathan
Automatically remove short words from the URL
Adding the code into your functions.php file will let WordPress automatically remove short (less than 3 characters) words from the generated permalink.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
add_filter('sanitize_title', 'remove_short_words'); function remove_short_words($slug) { if (!is_admin()) return $slug; $slug = explode('-', $slug); foreach ($slug as $k => $word) { if (strlen($word) < 3) { unset($slug[$k]); } } return implode('-', $slug); } |
Source: WPSnipp
Check whether a plugin is active
Adding the short code in your theme file will let you check whether a plugin is active or not.
1 2 3 4 5 6 7 8 9 10 11 |
<?php include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); if(is_plugin_active('plugin-directory/plugin-file.php')) { //plugin is activated } ?> |
Source: WPMayor
Force specific pages to be SSL secure
Just add the following article to the functions.php file of your WordPress theme and specify the post or page ID desired.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) { if ( $post_id == 25 ) { return true } return $force_ssl; } add_filter('force_ssl' , 'wps_force_ssl', 10, 3); |
Source: WPSnipp
Crop uploaded images
Add the code below to your functions.php file will let you crop uploaded images.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// Standard Size Thumbnail if(false === get_option("thumbnail_crop")) { add_option("thumbnail_crop", "1"); } else { update_option("thumbnail_crop", "1"); } // Medium Size Thumbnail if(false === get_option("medium_crop")) { add_option("medium_crop", "1"); } else { update_option("medium_crop", "1"); } // Large Size Thumbnail if(false === get_option("large_crop")) { add_option("large_crop", "1"); } else { update_option("large_crop", "1"); } |
Source: WP-Snippet
Setting up different admin and theme languages
Simply set the desired locale followed by adding the code to your functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php // setup one language for admin and the other for theme // must be called before load_theme_textdomain() function set_my_locale($locale) { $locale = ( is_admin() ) ? "en_US" : "it_IT"; setlocale(LC_ALL, $local ); return $locale; } add_filter( 'locale', 'set_my_locale' ); ?> |
Source: WP-Snippet
Using WordPress Shortcodes In Theme Files
To use a shortcode in a theme file, simply use the do_shortcode() function.
1 |
<?php echo do_shortcode("[your_shortcode]"); ?> |
Linking Twitter usernames in WordPress
Simply add the short code in your functions.php template and it will let you automatically linked all twitter usernames in posts and comments to their Twitter profiles. Usernames have to be written under the form @username.
1 2 3 4 5 6 7 8 9 10 11 |
function twtreplace($content) { $twtreplace = preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/',"$1<a href=\"http://twitter.com/$2\" target=\"_blank\" rel=\"nofollow\">@$2</a>",$content); return $twtreplace; } add_filter('the_content', 'twtreplace'); add_filter('comment_text', 'twtreplace'); |
Source: Snipplr
Photo Credit: Flickr/Michael Himbeault
Note: Like the post, then make sure you do follow and like us on both Twitter and Facebook.