most used wordpress functions in theme

most used wordpress functions in theme

In wordpress theming is very important. Developers know the importance of functions.php file. Here we given most used wordpress functions in theme which will be useful for wordpress developer. I always written some very nice code snippets in functions.php file.

most used wordpress functions in theme

I found very useful codes which is very helpful for very wordpress designer and developers.

Here is very useful code snippets.
Enable Hidden Admin Feature displaying ALL Site Settings

1// CUSTOM ADMIN MENU LINK FOR ALL SETTINGS
2   function all_settings_link() {
3    add_options_page(__('All Settings'), __('All Settings'), 'administrator', 'options.php');
4   }
5   add_action('admin_menu', 'all_settings_link');

Remove Update Notification for all users except ADMIN User

1// REMOVE THE WORDPRESS UPDATE NOTIFICATION FOR ALL USERS EXCEPT SYSADMIN
2       global $user_login;
3       get_currentuserinfo();
4       if (!current_user_can('update_plugins')) { // checks to see if current user can update plugins
5        add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
6        add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
7       }

Include custom post types in the search results.

1// MAKE CUSTOM POST TYPES SEARCHABLE
2function searchAll( $query ) {
3 if ( $query->is_search ) { $query->set( 'post_type', array( 'site','plugin', 'theme','person' )); }
4 return $query;
5}
6add_filter( 'the_search_query', 'searchAll' );

Add your custom post types to your sites main RSS feed by default.

1// ADD CUSTOM POST TYPES TO THE DEFAULT RSS FEED
2function custom_feed_request( $vars ) {
3 if (isset($vars['feed']) && !isset($vars['post_type']))
4  $vars['post_type'] = array( 'post', 'site', 'plugin', 'theme', 'person' );
5 return $vars;
6}
7add_filter( 'request', 'custom_feed_request' );

Modify the Login Logo & Image URL Link

01add_filter( 'login_headerurl', 'namespace_login_headerurl' );
02/**
03 * Replaces the login header logo URL
04 *
05 * @param $url
06 */
07function namespace_login_headerurl( $url ) {
08    $url = home_url( '/' );
09    return $url;
10}
11 
12add_filter( 'login_headertitle', 'namespace_login_headertitle' );
13/**
14 * Replaces the login header logo title
15 *
16 * @param $title
17 */
18function namespace_login_headertitle( $title ) {
19    $title = get_bloginfo( 'name' );
20    return $title;
21}
22 
23add_action( 'login_head', 'namespace_login_style' );
24/**
25 * Replaces the login header logo
26 */
27function namespace_login_style() {
28    echo '<style>.login h1 a { background-image: url( ' . get_template_directory_uri() . '/images/logo.png ) !important; }</style>';
29}

Loading jQuery from the Google CDN

01// even more smart jquery inclusion :)
02add_action( 'init', 'jquery_register' );
03 
04// register from google and for footer
05function jquery_register() {
06 
07if ( !is_admin() ) {
08 
09    wp_deregister_script( 'jquery' );
10    wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js' ), false, null, true );
11    wp_enqueue_script( 'jquery' );
12}
13}

Remove the WordPress Version Info for Security

1// remove version info from head and feeds
2function complete_version_removal() {
3    return '';
4}
5add_filter('the_generator', 'complete_version_removal');

Add Spam & Delete Links to Comments on Front End

1// spam & delete links for all versions of wordpress
2function delete_comment_link($id) {
3    if (current_user_can('edit_post')) {
4        echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&c='.$id.'">del</a> ';
5        echo '| <a href="'.get_bloginfo('wpurl').'/wp-admin/comment.php?action=cdc&dt=spam&c='.$id.'">spam</a>';
6    }
7}

Remove Default WordPress Meta Boxes

01// REMOVE META BOXES FROM DEFAULT POSTS SCREEN
02   function remove_default_post_screen_metaboxes() {
03 remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
04 remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
05 remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Metabox
06 remove_meta_box( 'trackbacksdiv','post','normal' ); // Talkback Metabox
07 remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
08 remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
09 }
10   add_action('admin_menu','remove_default_post_screen_metaboxes');
11 
12// REMOVE META BOXES FROM DEFAULT PAGES SCREEN
13   function remove_default_page_screen_metaboxes() {
14 remove_meta_box( 'postcustom','page','normal' ); // Custom Fields Metabox
15 remove_meta_box( 'postexcerpt','page','normal' ); // Excerpt Metabox
16 remove_meta_box( 'commentstatusdiv','page','normal' ); // Comments Metabox
17 remove_meta_box( 'trackbacksdiv','page','normal' ); // Talkback Metabox
18 remove_meta_box( 'slugdiv','page','normal' ); // Slug Metabox
19 remove_meta_box( 'authordiv','page','normal' ); // Author Metabox
20 }
21   add_action('admin_menu','remove_default_page_screen_metaboxes');

Add Custom User Profile Fields

01// CUSTOM USER PROFILE FIELDS
02   function my_custom_userfields( $contactmethods ) {
03 
04    // ADD CONTACT CUSTOM FIELDS
05    $contactmethods['contact_phone_office']     = 'Office Phone';
06    $contactmethods['contact_phone_mobile']     = 'Mobile Phone';
07    $contactmethods['contact_office_fax']       = 'Office Fax';
08 
09    // ADD ADDRESS CUSTOM FIELDS
10    $contactmethods['address_line_1']       = 'Address Line 1';
11    $contactmethods['address_line_2']       = 'Address Line 2 (optional)';
12    $contactmethods['address_city']         = 'City';
13    $contactmethods['address_state']        = 'State';
14    $contactmethods['address_zipcode']      = 'Zipcode';
15    return $contactmethods;
16   }
17   add_filter('user_contactmethods','my_custom_userfields',10,1);

Add an excerpt box for pages

1if ( function_exists('add_post_type_support') )
2{
3    add_action('init', 'add_page_excerpts');
4    function add_page_excerpts()
5    {
6        add_post_type_support( 'page', 'excerpt' );
7    }
8}

Function to change the length of Exerpt

1function new_excerpt_length($length) {
2    return 100;
3}
4 
5add_filter('excerpt_length', 'new_excerpt_length');

Auto Extract the First Image from the Post Content

01/ AUTOMATICALLY EXTRACT THE FIRST IMAGE FROM THE POST
02function getImage($num) {
03    global $more;
04    $more = 1;
05    $link = get_permalink();
06    $content = get_the_content();
07    $count = substr_count($content, '<img');
08    $start = 0;
09    for($i=1;$i<=$count;$i++) {
10        $imgBeg = strpos($content, '<img', $start);
11        $post = substr($content, $imgBeg);
12        $imgEnd = strpos($post, '>');
13        $postOutput = substr($post, 0, $imgEnd+1);
14        $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
15        $image[$i] = $postOutput;
16        $start=$imgEnd+1;
17    }
18    if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; }
19    $more = 0;
20}

Unregister WP Default Widgets

01// unregister all default WP Widgets
02function unregister_default_wp_widgets() {
03    unregister_widget('WP_Widget_Pages');
04    unregister_widget('WP_Widget_Calendar');
05    unregister_widget('WP_Widget_Archives');
06    unregister_widget('WP_Widget_Links');
07    unregister_widget('WP_Widget_Meta');
08    unregister_widget('WP_Widget_Search');
09    unregister_widget('WP_Widget_Text');
10    unregister_widget('WP_Widget_Categories');
11    unregister_widget('WP_Widget_Recent_Posts');
12    unregister_widget('WP_Widget_Recent_Comments');
13    unregister_widget('WP_Widget_RSS');
14    unregister_widget('WP_Widget_Tag_Cloud');
15}
16add_action('widgets_init', 'unregister_default_wp_widgets', 1);

Enable GZIP output compression

1if(extension_loaded("zlib") && (ini_get("output_handler") != "ob_gzhandler"))
2   add_action('wp', create_function('', '@ob_end_clean();@ini_set("zlib.output_compression", 1);'));

Enable shortcodes in widgets

1// shortcode in widgets
2if ( !is_admin() ){
3    add_filter('widget_text', 'do_shortcode', 11);
4}

If you have any interesting code snippets then please suggest me.

most used wordpress functions in theme
most used wordpress functions in theme

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.