Jelajah More on this category »
Wisata More on this category »
Liburan More on this category »
.

Useful Code Snippets for WordPress Developers

 
Let’s simplify one thing from the beginning: becoming a WordPress developer is tough work even with the help of these code snippets. For many WordPress designers out there, knowing a few tricks and/or changing a theme it’s enough to call themselves ‘experts’ and charge high for a simple theme. So, today I would like to share with you and with these ‘developers’ a list of useful code snippets that you can use in any project depending on your needs. Remember to back-up your functions.php file before doing any change.


Break Out of Frames for WordPress

Stop anyone from placing your website inside a frame. This is my favorite solution against content stealers.
// Break Out of Frames for WordPress function break_out_of_frames() { if (!is_preview()) { echo "\n<script type=\"text/javascript\">"; echo "\n<!--"; echo "\nif (parent.frames.length > 0) { parent.location.href = location.href; }"; echo "\n-->"; echo "\n</script>\n\n"; } } add_action('wp_head', 'break_out_of_frames');

Set Minimal Comment Limit In WordPress

Tired of those annoying comments like “Nice post” ? Change the $minimalCommentLength to whatever you want and your good to go.
// Break Out of Frames for WordPress function break_out_of_frames() { if (!is_preview()) { echo "\n<script type=\"text/javascript\">"; echo "\n<!--"; echo "\nif (parent.frames.length > 0) { parent.location.href = location.href; }"; echo "\n-->"; echo "\n</script>\n\n"; } } add_action('wp_head', 'break_out_of_frames');

Maintenance Mode

A quick way to activate maintenance mode on your website. Just comment the lines when you finish the changes.
function maintenace_mode() { if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) { die('Maintenance.'); } } add_action('get_header', 'maintenace_mode');

Replace WordPress default logo

Simply replace the WordPress logo with your own.
function my_custom_login_logo() { echo '<style type="text/css"> h1 a { background-image:url('.get_bloginfo('template_directory').'/images/custom-login-logo.gif) !important; } </style>'; } add_action('login_head', 'my_custom_login_logo');

Add favicon without a plugin
<span style="font-weight: normal;"> </span> // add a favicon to your function blog_favicon() { echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" />'; } add_action('wp_head', 'blog_favicon');

Add a search box to your nav menu

Want to automatically add a search box to your nav? Here’s how: add_filter('wp_nav_menu_items','add_search_box', 10, 2); function add_search_box($items, $args) { ob_start(); get_search_form(); $searchform = ob_get_contents(); ob_end_clean(); $items .= '<li>' . $searchform . '</li>'; return $items; }

Custom content under each post

If you want to add custom content under each posts, this is a quick solution. Also you can do this by editing single.php.
function add_post_content($content) { if(!is_feed() &amp;&amp; !is_home()) { $content .= '<p>This article is copyright &copy; '.date('Y').'&nbsp;'.bloginfo('name').'</p>'; } return $content; } add_filter('the_content', 'add_post_content');

Remove the url field from comment form


Another quick solution against spammers.
function remove_comment_fields($fields) { unset($fields['url']); return $fields; } add_filter('comment_form_default_fields','remove_comment_fields');

Remove the ‘read more’ jump

Adding the shortcode into your functions.php file will remove the “read more” link from your blog which will automatically takes you to the article page.
function wdc_no_more_jumping($post) { return '<a href="'.get_permalink($post->ID).'" class="read-more">'.'Continue Reading'.'</a>'; } add_filter('excerpt_more', 'wdc_no_more_jumping');

Exclude specific categories from your blog homepage

Sometimes you want to limit the number of categories on your homepage. Replace 5 and 34 category IDs with the ones you want to exclude.
function exclude_category_home( $query ) { if ( $query->is_home ) { $query->set( 'cat', '-5, -34' ); } return $query; } add_filter( 'pre_get_posts', 'exclude_category_home' );

Remove WordPress version number
add_filter('the_generator', 'digwp_complete_version_removal'); function digwp_complete_version_removal() { return ''; }

BONUS: Responsive Adsense Code

This is NOT a WordPress snippet but i found it very useful for Adsense users.
<!-- You can add multiple Adsense Ad units --> <!-- Just change the ad on Line #4 and Line #7 --> <div id="google-ads-1"> <script type="text/javascript"> adUnit = document.getElementById("google-ads-1"); adWidth = adUnit.offsetWidth; /* Replace this with your AdSense Publisher ID */ google_ad_client = "ca-pub-1234567890"; if ( adWidth >= 768 ) { /* Leaderboard 728x90 */ google_ad_slot = "AAA"; google_ad_width = 768; google_ad_height = 90; } else if ( adWidth >= 468 ) { /* Banner (468 x 60) */ google_ad_slot = "BBB"; google_ad_width = 468; google_ad_height = 60; } else if ( adWidth >= 336 ) { /* Large Rectangle (336 x 280) */ google_ad_slot = "CCC"; google_ad_width = 336; google_ad_height = 280; } else if ( adWidth >= 300 ) { /* Medium Rectangle (300 x 250) */ google_ad_slot = "DDD"; google_ad_width = 300; google_ad_height = 250; } else if ( adWidth >= 250 ) { /* Square (250 x 250) */ google_ad_slot = "EEE"; google_ad_width = 250; google_ad_height = 250; } else { /* Ad Link Unit (200 x 90) */ google_ad_slot = "FFF"; google_ad_width = 200; google_ad_height = 90; } </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div>

Please note that some of these code snippets may not work with the latest WordPress version. If you find one, please comment below so that others be notified.

Comment With The Topic

Copyright © 2013. Maz - Scripts - All Rights Reserved | Template Created by Kompi Ajaib Proudly powered by Blogger