A WordPress blogs, hundreds of thousands of plugins options available and what more, thousands of ways available to use these plugins but what better, if you use some good short codes which are good enough to offer you the kind of functionality that you are looking for and that in one-to-many cases, these easily available free (or premium) plugins not able to offer you. In this article, you will find ten of best WordPress snippets which not only help you but also make your job easy.
Turn Over Address Components to an Array
The snippet will help you breaking up an address into components while it also sorts a multidimensional array of location alphabetically by state.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function break_address( $loc ) { $components = explode( ',' , $loc ); $state_zip = explode( ' ' , trim( $components[2] ) ); $components_array = array( 'street' => trim( $components[0] ), 'city' => trim( $components[1] ), 'state' => $state_zip[0], 'zip' => $state_zip[1], 'country' => trim( $components[3] ) ); return $components_array; } $locations = array(); $location = get_field( 'location_field' ); $location = $location[ 'address' ]; $locations[ 0 ] = break_address( $location ); usort( $locations , function( $a , $b ) { return $a[ 'state' ] - $b[ 'state' ]; }); var_dump( $locations ); |
Source: Snipplr
WordPress is_child() and is_ancestor()
To use the following snippet, simply add it in your theme’s functions.php template.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Check if page is direct child function is_child($page_id) { global $post; if( is_page() && ($post->post_parent == $page_id) ) { return true; } else { return false; } } // Check if page is an ancestor function is_ancestor($post_id) { global $wp_query; $ancestors = $wp_query->post->ancestors; if ( in_array($post_id, $ancestors) ) { return true; } else { return false; } } |
Source: CodeByte
WordPress Admin Area Functions
The snippet will allow to accomplish different WordPress functions, including after registration redirect, remove generator, admin footer, remove default dashboard items, hoook into the ‘wp_dashboard_setup’ action to register our function, remove unnecessary page/post meta boxes, screen options – remove columns and remove WP logo.
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 68 69 70 71 72 73 74 75 76 77 78 79 |
// ======================== // = Admin Area Functions = // ======================== require_once(TEMPLATEPATH . '/help_page.php'); // After Registration Redirect function __my_registration_redirect() { return home_url( '/thank-you-for-registering' ); } add_filter( 'registration_redirect', '__my_registration_redirect' ); // Remove Generator remove_action('wp_head', 'wp_generator'); // Admin Footer function remove_footer_admin () { echo "<strong>CTRM Center</strong> created by <a href=\"http://www.andrewcolby.com\">Andrew Colby</a>"; } add_filter('admin_footer_text', 'remove_footer_admin'); // Remove Default Dashboard Items function example_remove_dashboard_widgets() { // Globalize the metaboxes array, this holds all the widgets for wp-admin global $wp_meta_boxes; // Remove the incomming links widget unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']); // Plugins unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']); // EasymMail unset($wp_meta_boxes['dashboard']['normal']['core']['alo-easymail-widget']); // Remove right now //unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']); unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']); } // Hoook into the 'wp_dashboard_setup' action to register our function add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' ); // remove unnecessary page/post meta boxes function remove_meta_boxes() { // posts remove_meta_box('postcustom','post','normal'); remove_meta_box('trackbacksdiv','post','normal'); remove_meta_box('slugdiv','post','normal'); //remove_meta_box('commentstatusdiv','post','normal'); //remove_meta_box('commentsdiv','post','normal'); //remove_meta_box('categorydiv','post','normal'); //remove_meta_box('tagsdiv-post_tag','post','normal'); //remove_meta_box('authordiv','post','normal'); // pages remove_meta_box('postcustom','page','normal'); remove_meta_box('commentstatusdiv','page','normal'); remove_meta_box('trackbacksdiv','page','normal'); remove_meta_box('commentsdiv','page','normal'); remove_meta_box('slugdiv','page','normal'); //remove_meta_box('authordiv','page','normal'); } add_action('admin_init','remove_meta_boxes'); // Screen Options - Remove Columns function my_remove_columns( $posts_columns ) { unset( $posts_columns['comments'] ); return $posts_columns; } add_filter( 'manage_post_posts_columns', 'my_remove_columns' ); add_action( 'admin_menu', 'my_remove_meta_boxes' ); // Remove WP Logo function annointed_admin_bar_remove() { global $wp_admin_bar; /* Remove their stuff */ $wp_admin_bar->remove_menu('wp-logo'); $wp_admin_bar->remove_menu('comments'); } add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove', 0); |
Source: Snipplr
WP – Roles and Capabilities
The snippet will let you define different roles and capabilities.
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 |
[edit_post] => "edit_{$capability_type}" [read_post] => "read_{$capability_type}" [delete_post] => "delete_{$capability_type}" [edit_posts] => "edit_{$capability_type}s" [edit_others_posts] => "edit_others_{$capability_type}s" [publish_posts] => "publish_{$capability_type}s" [read_private_posts] => "read_private_{$capability_type}s" [delete_posts] => "delete_{$capability_type}s" [delete_private_posts] => "delete_private_{$capability_type}s" [delete_published_posts] => "delete_published_{$capability_type}s" [delete_others_posts] => "delete_others_{$capability_type}s" [edit_private_posts] => "edit_private_{$capability_type}s" [edit_published_posts] => "edit_published_{$capability_type}s" edit_members read_members delete_members edit_memberss edit_others_memberss publish_memberss read_private_memberss delete_memberss delete_private_memberss delete_published_memberss delete_others_memberss edit_private_memberss edit_published_memberss |
Source: Snipplr
WP Menu custom walker
The snippet will allow you to create a menu.
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 |
class premierMenu 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 ) . '"'; $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>'; $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : ''; $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : ''; $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : ''; $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : ''; $prepend = '<strong>'; $append = '</strong>'; $description = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : ''; if($depth != 0) { $description = $append = $prepend = ""; } $item_output = $args->before; $item_output .= '<a'. $attributes .'>'; $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append; $item_output .= $description.$args->link_after; $item_output .= '</a>'; $item_output .= $args->after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } } |
Source: Snipplr
WordPress – get an RSS feed, display items with a loop
The snippet will allow you to retrieve a feed into WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php include_once(ABSPATH . WPINC . '/rss.php'); $feed = 'http://dangayle.com/feed/'; $rss = fetch_feed($feed); if (!is_wp_error( $rss ) ) : $maxitems = $rss->get_item_quantity(3); $rss_items = $rss->get_items(0, $maxitems); if ($rss_items): echo "<ul>\n"; foreach ( $rss_items as $item ) : //instead of a bunch of string concatenation or echoes, I prefer the terseness of printf //(http://php.net/manual/en/function.printf.php) printf('<li><a href="%s">%s</a><p>%s</p></li>',$item->get_permalink(),$item->get_title(),$item->get_description() ); endforeach; echo "</ul>\n"; endif; endif; ?> |
Source: Dangayle
Clear out jquery to prevent conflicts
The snippet will let you either link a script with a handle previously registered using the wp_register_script() function, or provide this function with all the parameters necessary to link a script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/* * WordPress Sample function and action * for loading scripts in themes */ // Let's hook in our function with the javascript files with the wp_enqueue_scripts hook add_action( 'wp_enqueue_scripts', 'wpcandy_load_javascript_files' ); // Register some javascript files, because we love javascript files. Enqueue a couple as well function wpcandy_load_javascript_files() { wp_deregister_script( 'jquery'); wp_register_script( 'jquery', get_template_directory_uri().'/js/jquery-1.8.2.min.js', array(), '1.8.2', false ); wp_enqueue_script('jquery'); wp_register_script( 'jquery.flexslider', get_template_directory_uri().'/js/jquery.flexslider.js', array('jquery'), '1.7', false ); wp_enqueue_script('jquery.flexslider'); } Source: WordPress Codex Easy Wordpress Drop Downs using CSS The snippet will help you creating an easy WordPress drop downs using CSS. ul.menu, ul.menu li { float: left; } ul.menu li { position: relative; margin: 0 15px; list-style: none; } .sub-menu { position: absolute; float: none; width: 100px; padding: 0; left: 0; } .sub-menu li { display:none; margin: 0; padding: 0; } ul.menu li:hover li { float: none; display:block; clear: both; } |
Source: AmberWeinberg
WordPress Typography Formatting
The snippet lets dummy content to format typographic/html tags for WordPress copy.
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 |
/* NAVIGATION ALIGNMENT & STYLING: ADD TO style.css #nav{ border:1px solid #ccc; border-width:1px 0; list-style:none; margin:0; padding:0; text-align:center; } #nav li{ display:inline; } #menu-nav a{ display:inline-block; padding:10px; }*/ <style> h1{} h2{} h3{} h4{} h5{} p{} br{} strong{} blockquote{} a{} a:hover{} a:visited{} ul{} li{} </style> <h1>Header H1<h1> <h2>Header H2<h2> <h3>Header H3<h3> <h4>Header H4<h4> <h5>Header H5<h5> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. <br>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p> <strong>strong text</strong> <blockquote>block quote</blockquote> <a href="#">Link / Hover / Visited</a> <hr> <ul> <li>list item 1</li> <li>list item 2</li> <li>list item 3</li> </ul> <ol> <li>list item 1</li> <li>list item 2</li> <li>list item 3</li> </ol> |
Source: Snipplr
NO Follow WordPress Tag Cloud
The following snippet will result in no following WordPress Tag cloud. All you have to do is to add the snippet to your theme’s functions.php.
1 2 3 4 |
function add_nofollow_tag($sLink) { return str_replace('<a href=', '<a rel="nofollow" href=', $sLink); } add_filter('wp_tag_cloud', 'add_nofollow_tag'); |
Photo Credit: thisismyurl via photopin cc