While working or setting up the WordPress blog it often comes the time when you got puzzled on thinking and even (most of the time) wondering that if I have some easy trick with me by which I can let mine work done instantly. At that time you would be requiring some short codes (or snippets) which can make your work easier and faster. With this article, I will be sharing a collection of 10 most required WordPress snippets that will work for you in making your life while using WordPress better.
Disable Google Analytics for logged in users
Like any other website on internet, you too want to use Google Analytics only for tracking real visitors and not your website’s authors or writers. Using the following snippet will automatically adds the Google Analytics code to your wp_footer() area in your functions.php file, means you don’t have to manually add up the Analytics code to all of your website files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php // function for inserting Google Analytics into the wp_head add_action('wp_footer', 'ga'); function ga() { if ( !is_user_logged_in() ) { // not for logged in users ?> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXXX']); // insert your Google Analytics id here _gaq.push(['_trackPageview']); _gaq.push(['_trackPageLoadTime']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> <?php } } ?> |
Include Future Posts in RSS Feed
Adding the snippet to your theme’s functions.php file will let you appear your future posts in your RSS feed.
1 2 3 4 5 6 7 8 9 10 |
<?php /* Include published and future posts in rss feed. */ function FuturePostsFeed($query) { if ($query->is_feed) { $query->set('post_status','publish,future'); } return $query; } add_filter('pre_get_posts','FuturePostsFeed'); ?> |
Remove Gallery Settings
Gallery setting often make you irritated. The following snippet will simply remove all the gallery settings from the media upload section. All you have to do is simple add the code in your functions.php file.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php add_action( 'admin_head_media_upload_gallery_form', 'mfields_remove_gallery_setting_div' ); if( !function_exists( 'mfields_remove_gallery_setting_div' ) ) { function mfields_remove_gallery_setting_div() { print ' <style type="text/css"> #gallery-settings *{ display:none; } </style>'; } } ?> |
Hide admin ‘help’ tab
Seeing the ‘help’ tab all the time in the top right corner of the WordPress admin area will be a devastating experience for your clients. In order to let avoid such conditions on not to happen for them, simply add the following snippet in your functions.php template.
1 2 3 4 5 6 7 8 9 |
<?php // Hide admin help tab function hide_help() { echo '<style type="text/css"> #contextual-help-link-wrap { display: none !important; } </style>'; } add_action('admin_head', 'hide_help'); ?> |
Get top level categories / taxonomy
The following code will allow you to get the top level categories and taxonomy.
1 2 3 4 5 6 7 8 9 10 11 |
<?php $args = array( 'type' => 'post', 'orderby' => 'term_group', 'hide_empty' => 0, 'hierarchical' => 0, 'parent' => 0, 'taxonomy' => '..if you are using a taxonomy instead of a category' ); get_categories( $args ); ?> |
Dynamic copyright text
Just on your way to launch your website and thinking on what would be the best copyrighted text to place in footer. The snippet below allows you to create an amazing copyright text for your footer by automatically updating the date using the the_date() function. Just add the code in to your footer.php file.
1 2 3 4 5 6 7 8 9 10 |
<b>(c) <?php echo date('Y'); ?> </b> | <a href=" <?php bloginfo('url'); ?> "> <?php bloginfo('name'); ?> </a> | <?php bloginfo('description'); ?> |
Redirect to URL from Title
Running a blog or a magazine and felt like a need arise in which you want to redirect your readers to a URL from title. You do think of a condition in which you can either use a plugin or simply use some code.
To help you out and reducing your workload over this, all you need to do is to add a custom filed called “redirect” with the URL you want to redirect at as value. This means now when your reader click on certain title on your page, they will be redirected to your desired URL.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// Search for something that looks similar to this in your index.php <a href=" <?php the_permalink(); ?> " rel="bookmark"> <?php the_title(); ?> </a> // and replace it with <a href=" <?php $redirect = get_post_meta($post->ID, 'redirect', true); if ($redirect != "") { echo $redirect; } else { the_permalink(); } ?> " rel="bookmark"> <?php the_title(); ?> </a> |
Remove Private/Protected from Post Title
The time when a post’s visibility is either Password protected or Private, the snippet removes the description before the post title.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php function the_title_trim($title) { $title = attribute_escape($title); $findthese = array( '#Protected: #', // Notice the blank space after Protected: '#Private: #' // Notice again, otherwise the title get pushed. ); $replacewith = array( '', // What to replace "Protected: " with '' // What to replace "Private: " with ); $title = preg_replace($findthese, $replacewith, $title); return $title; } add_filter('the_title', 'the_title_trim'); ?> |
Automatically Notify Your Members on New Posts
Placing the following snippet into your functions.php file will allow you to send an email to all registered users as soon as post is published.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php function email_members($post_ID) { $wp_user_search = new WP_User_Query( array( 'fields' => array('user_email') ) ); $usersarray = $wp_user_search->get_results(); $arrUsers = array (); for ($arr = $usersarray, $mU = count ($arr), $iU = 0; $iU < $mU; $iU++) { $arrUsers[] = $arr[$iU]->user_email; } // for $users = implode(",", $arrUsers); mail($users, "New post notification : " . get_bloginfo('name') , "A new post has been published on " . get_bloginfo('siteurl') ); return $post_ID; } add_action('publish_post', 'email_members'); ?> |
Login / Logout link
The following snippet can add login and logout link.
1 |
<?php wp_loginout( $redirect ); ?> |
Detect user from Google
The time when users found your WordPress site at Google, the following snippet will give special greetings to all the users.
1 2 3 4 5 |
<?php if (strpos($_SERVER[HTTP_REFERER], "google") == true) { echo "Hello Google User!"; } ?> |
Create a new Page
The snippet will allow you to create a new page on its own. The code could be used to create a POST, all require is to change the value for “post_type.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $new_page = array( 'slug' => 'this-is-my-new-page', 'title' => 'Write a Headline that Captivates', 'content' => "Enter the body Content for your Page here" ); $new_page_id = wp_insert_post( array( 'post_title' => $new_page['title'], 'post_type' => 'page', 'post_name' => $new_page['slug'], 'comment_status' => 'closed', 'ping_status' => 'closed', 'post_content' => $new_page['content'], 'post_status' => 'publish', 'post_author' => 1, 'menu_order' => 0 )); ?> |
Change Login Logo URL
The one brand advantage of using WordPress is that you can change the login logo from the default WordPress logo to your own brand, but with the use of following snippet, you will get more freedom by changing the link that the logo points to. Add the code to functions.php template.
1 2 3 4 5 6 |
<?php add_filter( 'login_headerurl', 'my_custom_login_url' ); function my_custom_login_url($url) { return 'http://www.example.com'; } ?> |
Show Tag Cloud
You can use the tag cloud to either of your sidebar, footer or elsewhere. Add the following snippet and you tag cloud is ready to display.
1 2 3 4 5 6 7 8 |
<?php wp_tag_cloud(array( 'smallest' => 10, // size of least used tag 'largest' => 18, // size of most used tag 'unit' => 'px', // unit for sizing 'orderby' => 'name', // alphabetical 'order' => 'ASC', // starting at A 'exclude' => 6 // ID of tag to exclude from list )); ?> |
Automatically Activate Plugins
The code can activate the plugins automatically via the wp-config.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php // get already activated plugins $plugins = get_option('active_plugins'); if($plugins){ $puginsToActiv = array('Plugin1', 'Plugin2', 'Plugin3'); foreach ($puginsTostActiv as $plugin){ if (!in_array($plugin, $plugins)) { array_push($plugins,$plugin); update_option('active_plugins',$plugins); } } } ?> |
Change version in WP-Admin footer
The snippet will let you change the version string in the bottom-right of the WP admin pages.
1 2 3 4 5 6 |
<?php function change_footer_version() { return 'Version 1.0.0'; } add_filter( 'update_footer', 'change_footer_version', 9999 ); ?> |