Automatically Remove Short Words From The URL
Adding the following snippet to your theme’s functions.php file will automatically remove short words (less than three characters) from your post title url.
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: Kevin Chard
Conditional Tag For Blog-Related Pages
Adding the following snippet to your functions.php file will let you add conditional tag for blog-related pages.
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; } |
Once done, you can use the function to detect if you are currently on a blog related page:
1 2 3 4 5 6 7 8 9 |
<?php if(is_blog()) { //the page is blog related } ?> |
Source: Nathan
Check Whether A Plugin Is Active
The following snippet 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: Jean Galea
Using WordPress Shortcodes In Theme Files
The following snippet will let you use WordPress shortcodes in theme files.
1 |
<?php echo do_shortcode("[your_shortcode]"); ?> |
Checking If The Current Post Is A Custom Post Type
The following snippet will let you check if the current post is a custom post type.
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; } } } |
Once done, you can use the function as below:
1 2 3 4 5 |
if (is_custom_post_type()) { //Current post is a custom post type } |
Source: Jonas Ethomsen
Force Specific Pages To Be SSL Secure
Adding the following snippet to your theme’s functions.php file, you will able to force specific pages to be SSL secure.
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: Kevin Chard
Check If A Post Contains Gallery
Adding the following functions.php file will let you check if a post has a gallery.
1 2 3 4 5 6 7 |
function hasgallery(){ global $post; return (strpos($post->post_content,'[gallery') !== false); } |
Source: WordPress Stackexchange