http://go.ad2up.com/afu.php?id=665574

10 Amazing WordPress Hacks Related to ‘function.php’ File

10 Amazing WordPress Hacks Related to function.php File

WordPress is the most popular and defined website platform of all time. Mostly bloggers and business website owners use WordPress. Using WordPress is the simplest thing but making it simpler is not an easy stack. WordPress developers try die hard to make it more refine and robust. One of the most important file within WordPress package is – ‘function.php’. This file is master file of all functions related to WordPress working. You can make changes to it to add or delete more features to your WordPress installation.

In current reading, we’re exploring 10 amazing WordPress hacks related to ‘function.php’ file. You can try and get the maximum outcome from your WordPress.

1. Stop pinging your own blog

Pining is the most annoy thing, especially if your blog is getting nice traffic from other blogs and social media. When you link up your own older blog posts to the current blog then you ping yourself automatically which is undesired behavior by the most of the bloggers. Then try out this handy function to stop pining back your own blog –

function no_self_ping( &$links ) {
$home = get_option( 'home' );
foreach ( $links as $l => $link )
if ( 0 === strpos( $link, $home ) )
unset($links[$l]);
}
add_action( 'pre_ping', 'no_self_ping' );

2. Change the excerpt length to custom value

Excerpt is the short description of your blog post which replaces the whole content on home page. With the help of excerpt you get summarize blog posts on home page for compact view. With the following function you can change word length of excerpt from 55 to your custom default –

function new_excerpt_length($length) {
return 160;
}
add_filter('excerpt_length', 'new_excerpt_length');

3. Remove the WordPress Version Info

In the blog footer area, you can see the WordPress version info. With the custom theme demand you need to remove it sometimes. With the following function you can completely remove the WordPress version info from the blog footer or from somewhere else.

function complete_version_removal() { return ''; } add_filter('the_generator', 'complete_version_removal');

4. Load jQuery files from Google’s CDN Directly

Adding jQuery files to your own servers is a good technique but it might be panic if your server is not responding or you have made changes in the jQuery code which need the latest or older version of jQuery file then you have to upload the desired file again. To overcome from this situation, use of google CDN is a better option. Use the following

function to load jQuery files from Google’s CDN directly.
add_action( 'init', 'jquery_register' );
function jquery_register() {
if ( !is_admin() ) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' ), false, null, true );
wp_enqueue_script( 'jquery' );
}
}

5. Enable Shortcodes in Widgets

Plugins are good at performance if you can use shortcodes into widgets directly. Use the following function to use shortcodes in widgets –

if ( !is_admin() ){
    add_filter('widget_text', 'do_shortcode', 11);
}

6. Use the First Image of the Post Automatically

Use the following effective WordPress code to take the first image of the post and use it as thumbnail for your home page –

function first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('//i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}

7. Add More Custom Fields to User Profile

Help your users to extend their profile data beyond available limited fields in the User Profile. Use the following functions to add more options for your WordPress User Profile –

function my_custom_userfields( $contactmethods ) {
$contactmethods['contact_phone_office'] = 'Office Phone';
$contactmethods['contact_phone_mobile'] = 'Mobile Phone';
return $contactmethods;
}
add_filter('user_contactmethods','my_custom_userfields',10,1);

8. Limit your Maximum number of Post Revisions

While creating WordPress post, WordPress gives your functionality to save your work in the draft as much as time you need. Every time a database record is created for your saved draft. It creates more overhead onto your database server. Use the following function to limit the number of post revisions while saving into the draft –

if (!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS', 3);

9. All Settings in Single Page

Use the following function to add an extra page to show off all the settings related to your WordPress site at one place. This function will add an extra page to your settings menu listing all available settings for your WordPress site –

function all_settings_link() {
add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
}
add_action('admin_menu', 'all_settings_link');

10. Get the Statistics Related to Your Site Performance

If you want to measure your WordPress site performance then use the following function. This will add parameters like – number of database queries, time spent to create a page, memory consumption etc.

function performance( $visible = false ) {
$stat = sprintf( '%d queries in %.3f seconds, using %.2fMB memory',
get_num_queries(),
timer_stop( 0, 3 ),
memory_get_peak_usage() / 1024 / 1024
);
echo $visible ? $stat : "" ;
}
add_action( 'wp_footer', 'performance', 20 );

For this snippet to work, you’ll have to make sure that your theme is using

wp_footer()