Adding Shortcuts to WordPress toolbar
Adding the following short code into your functions.php file will let you add shortcuts to your WordPress toolbar.
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 |
// add a link to the WP Toolbar function custom_toolbar_link($wp_admin_bar) { $args = array( 'id' => 'gmail', 'title' => 'Gmail', 'href' => 'https://mail.google.com/mail/#inbox', 'meta' => array( 'class' => 'gmail', 'title' => 'sales@digwp.com' ) ); $wp_admin_bar->add_node($args); } add_action('admin_bar_menu', 'custom_toolbar_link', 999); |
Source: Jeff Starr
Display pingbacks/trackbacks count within admin post columns
Adding the following code in your functions.php file will let you display pingbacks/trackbacks count within admin post columns.
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 80 81 82 83 84 85 |
function commentCount($type = 'comments'){ if($type == 'trackbacks'): $typeSql = 'comment_type = "trackback"'; $oneText = 'One :trackback'; $moreText = '% :trackbacks'; $noneText = 'No :trackbacks'; elseif($type == 'pingbacks'): $typeSql = 'comment_type = "pingback"'; $oneText = 'One :pingback'; $moreText = '% :pingbacks'; $noneText = 'No :pingbacks'; endif; global $wpdb; $result = $wpdb->get_var(' SELECT COUNT(comment_ID) FROM '.$wpdb->comments.' WHERE '.$typeSql.' AND comment_approved="1" AND comment_post_ID= '.get_the_ID() ); if($result == 0): echo str_replace('%', $result, $noneText); elseif($result == 1): echo str_replace('%', $result, $oneText); elseif($result > 1): echo str_replace('%', $result, $moreText); endif; } add_filter('manage_posts_columns', 'posts_columns_counts', 1); add_action('manage_posts_custom_column', 'posts_custom_columns_counts', 1, 2); function posts_columns_counts($defaults){ $defaults['wps_post_counts'] = __('Counts'); return $defaults; } function posts_custom_columns_counts($column_name, $id){ if($column_name === 'wps_post_counts'){ commentCount('trackbacks'); echo "<br />"; commentCount('pingbacks'); } } |
Source: InstantShift
Easily embed and share Github gists
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
wp_embed_register_handler( 'gist', '/https:\/\/gist\.github\.com\/(\d+)(\?file=.*)?/i', 'wp_embed_handler_gist' ); function wp_embed_handler_gist( $matches, $attr, $url, $rawattr ) { $embed = sprintf( '<script src="https://gist.github.com/%1$s.js%2$s"></script>', esc_attr($matches[1]), esc_attr($matches[2]) ); return apply_filters( 'embed_gist', $embed, $matches, $attr, $url, $rawattr ); } |
Source: Robert O’Rourke
Create a custom database error page
You often noticed the error “Error establishing a database connection.” Well the idea with this short code is to create a custom database error page. Copy and paste the code below in a new file name as db-error.php and save it on your wp-content directory and you are done.
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 |
<?php // custom WordPress database error page header('HTTP/1.1 503 Service Temporarily Unavailable'); header('Status: 503 Service Temporarily Unavailable'); header('Retry-After: 600'); // 1 hour = 3600 seconds // If you wish to email yourself upon an error // mail("your@email.com", "Database Error", "There is a problem with the database!", "From: Db Error Watching"); ?> <!DOCTYPE HTML> <html> <head> <title>Database Error</title> <style> body { padding: 20px; background: red; color: white; font-size: 60px; } </style> </head> <body> You got problems. </body> </html> |
Source: CSS Tricks
Add a “delete” button to WordPress admin bar
The following short code will let you add a “delete” button to WordPress admin bar. All you have to do is to copy and paste the code in your theme’s functions.php file.
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 |
<?php function fb_add_admin_bar_trash_menu() { global $wp_admin_bar; if ( !is_super_admin() || !is_admin_bar_showing() ) return; $current_object = get_queried_object(); if ( empty($current_object) ) return; if ( !empty( $current_object->post_type ) && ( $post_type_object = get_post_type_object( $current_object->post_type ) ) && current_user_can( $post_type_object->cap->edit_post, $current_object->ID ) ) { $wp_admin_bar->add_menu( array( 'id' => 'delete', 'title' => __('Move to Trash'), 'href' => get_delete_post_link($current_object->term_id) ) ); } } add_action( 'admin_bar_menu', 'fb_add_admin_bar_trash_menu', 35 ); ?> |
Source: WPEngineer
Displaying RSS Feed
Adding the following code will let you display RSS feeds on your WordPress blog.
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 |
<?php include_once(ABSPATH . WPINC . '/rss.php'); $feed = 'http://example.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 ) : echo '<li>'; echo '<a href="' . $item->get_permalink() . '">' . $item->get_title() . "</a>\n"; echo '<p>' . $item->get_description() . "</li>\n"; endforeach; echo "</ul>\n"; endif; endif; ?> |
Source: Dan Gayle
Automatically create meta description from content
On pasting the following code into your functions.php file, it will let you create meta description from content automatically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function create_meta_desc() { global $post; if (!is_single()) { return; } $meta = strip_tags($post->post_content); $meta = strip_shortcodes($post->post_content); $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta); $meta = substr($meta, 0, 125); echo "<meta name='description' content='$meta' />"; } add_action('wp_head', 'create_meta_desc'); |
Source: Paul
Testing if a plugin is active or not
The following short code will let you track that whether the plugin is active or not.
1 2 3 4 5 6 7 8 9 |
<?php if (is_plugin_active('plugin-directory/plugin-file.php')) { // the plugin is active } ?> |
Source: Cats Who Code
Change the “posts” label to “articles”
Open your functions.php file, paste the code and you will be able to change the “posts” label to “articles.”
1 2 3 4 5 6 7 8 9 10 11 |
add_filter('gettext', 'change_post_to_article'); add_filter('ngettext', 'change_post_to_article'); function change_post_to_article($translated) { $translated = str_ireplace('Post', 'Article', $translated); return $translated; } |
Source: Smashing Magazine
WordPress pagination without plugins
Simply add the code to the place where you want to display your pagination and you are done.
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 |
global $wp_query; $total = $wp_query->max_num_pages; // only bother with the rest if we have more than 1 page! if ( $total > 1 ) { // get the current page if ( !$current_page = get_query_var('paged') ) $current_page = 1; // structure of "format" depends on whether we're using pretty permalinks $format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/'; echo paginate_links(array( 'base' => get_pagenum_link(1) . '%_%', 'format' => $format, 'current' => $current_page, 'total' => $total, 'mid_size' => 4, 'type' => 'list' )); } |
Source: Smashing Magazine
Photo Credit: Flickr/mendhak