The story about WordPress is simple – Code is Poetry – and why not it’s all about coding (or poetry, whatever you like to say). WordPress has been formed up with combining hundreds of thousands of short codes, all stringed together. What more? A base code is one which you see already come build up gives your WordPress blog a shape, but what about other “extra” things which you want to add-on yourself. This article speaks about those extra additional codes which you probably one-to-many times search for.
Change Default From Address
The following short code will let you change the default from addresses when WordPress sends out its emails. You need to add the snippet to functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php add_filter('wp_mail_from', 'new_mail_from'); add_filter('wp_mail_from_name', 'new_mail_from_name'); function new_mail_from($old) { return 'no-reply@example.com'; } function new_mail_from_name($old) { return 'Dave Thomas'; } ?> |
Source: WP-Snippets
Get Featured Image Source
Using the following WordPress function will let you get the source of the featured image easily.
1 2 3 4 5 6 7 |
<?php $imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "Full"); echo $imgsrc[0]; ?> |
Source: WordPress Codex
Exclude a custom post type from search
The short code will let you exclude a custom post type from search results.
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 36 37 38 39 40 41 |
<?php add_action('init', 'codex_custom_init'); function codex_custom_init() { $args = array( 'exclude_from_search' => false, // the important line here! 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => true, 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor','author','thumbnail','excerpt','comments') ); register_post_type('book',$args); } ?> |
Source: WordPress Codex
Add Additional Image Sizes
The short code will let you add an image size to your WordPress site. Make sure you place the code in your functions.php file to use in your theme.
1 2 3 4 5 6 7 8 9 |
<?php if ( function_exists( 'add_image_size' ) ) { add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height) add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped) } ?> |
Source: WordPress Codex
Comment Count Shortcode
The shortcode will let you display a specific post’s comment count.
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 |
<?php function comments_shortcode($atts) { extract( shortcode_atts( array( 'id' => '' ), $atts ) ); $num = 0; $post_id = $id; $queried_post = get_post($post_id); $cc = $queried_post->comment_count; if( $cc == $num || $cc > 1 ) : $cc = $cc.' Comments'; else : $cc = $cc.' Comment'; endif; $permalink = get_permalink($post_id); return '<a href="'. $permalink . '">' . $cc . '</a>'; } add_shortcode('comments', 'comments_shortcode'); ?> |
Source: WordPress Codex
Detect iPhone users
The shortcode will let you detect iPhone users.
1 2 3 4 5 6 7 8 9 10 11 |
<?php global $is_iphone; if($is_iphone) { // User in on an iPhone. } ?> |
Functions to Fetch Custom Field Values
Adding the shortcode in your functions.php file will let you fetch custom field values.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php /* Functions for processing custom fields */ function get_custom_field($field, $format = '', $date_format = 'F j, Y') { $fetch = $field; $fetch = get_post_custom_values($field); $fetch = $fetch[0]; // Date if ($format == 'date' & $fetch !='') $fetch = format_date($fetch, $date_format); // Text Block elseif ($format == 'text_block') $fetch = wpautop($fetch); // HTML elseif ($format == 'html') $fetch = html_entity_decode($fetch); // Google Map elseif ($format == 'google_map') $fetch = display_google_map($fetch); return $fetch; } function custom_field($field, $format = '', $date_format = 'F j, Y') { echo get_custom_field($field, $format, $date_format); } function format_date($date, $date_format) { $date = date($date_format, $date); return $date; } function display_google_map($code) { $code = html_entity_decode($code); // Remove the info bubble. Usually desirable, but use the html format if unwanted. $code = str_replace("output=embed", "output=embed&iwloc=near", $code); return $code; } ?> |
Source: WP-Snippets
Limit Search Results to Specific Post Types
On placing the following shortcode in your functions.php file, it lets you exclude your custom post types from search results.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php function SearchFilter($query) { if ($query->is_search) { $query->set('post_type',array('post','page')); } return $query; } add_filter('pre_get_posts','SearchFilter'); ?> |
Different admin and theme languages
The shortcode will define one language for admin and the other for theme.
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' ); ?> |
Remove theme editor submenu
Using the snippet to functions.php file will let you remove theme editor submenu.
1 2 3 4 5 6 7 8 9 10 11 |
<?php function remove_editor_menu() { remove_action('admin_menu', '_add_themes_utility_last', 101); } add_action('_admin_menu', 'remove_editor_menu', 1); ?> |
Source: WP-Snippet
Photo Credit: Flickr/Rosa Menkman