Académique Documents
Professionnel Documents
Culture Documents
Functions File
In your WordPress theme folder, there lays the most powerful theme file known asfunctions.php. As the name
suggests, this file adds unique functions to your WordPress installation. This file works just like a plugin
within your theme. So you can modify/override WordPress core features without editing any core file. If used
correctly, this file can do wonders for your WordPress site. It also speeds up development because you can
have all the codes at one place. In this article, we will share some of the most incredible and most wanted
some hacks.
Duh Moments: All codes in this article will be added to the functions.php theme file.
Simply paste the code below and insert your Google Analytics where it says paste your Google Analytics. You
can paste the code once in your functions.php file and never have to worry about it again. We are adding an
action to the wp_footer, so it will automatically insert adsense codes wherever on all pages you have the
wp_footer string.
1 <?php
2 add_action('wp_footer', 'add_googleanalytics');
3 function add_googleanalytics() { ?>
4 // Paste your Google Analytics code from Step 6 here
5 <?php } ?>
Every blog deserves to have its own identity. You can add this identity by adding the favicon code in your
header.php file, or you can make it easier for yourself by simply adding the following code in your
functions.php file.
1 // add a favicon to your
2 function blog_favicon() {
Now whenever you are developing the theme, just upload the .ico file in the root folder where the blog is
uploaded. You can also modify the file destination if you like. Often theme developers forget to change the
if you are working with a client that does not want to upgrade, then it is essential that you remove your
WordPress version number from your WordPress header, RSS feeds, and all other locations. To do this, add
When creating themes for a client, you can use this as one of the perks to the theme. All you have to do is paste
Screenshot:
You can change the footer of your Free or Custom WordPress themes by adding the necessary links. Simply
Screenshot:
Source: WPRecipes
You probably have seen widgets that numerous plugins and themes add in the WordPress dashboard. As a
theme developer, you can add one yourself by pasting the following codes:
01 add_action('wp_dashboard_setup','my_custom_dashboard_widgets');
02
03 function my_custom_dashboard_widgets() {
04 global $wp_meta_boxes;
05
wp_add_dashboard_widget('custom_help_widget', 'Theme
06
Support','custom_dashboard_help');
07 }
08
09 function custom_dashboard_help() {
echo '<p>Welcome to Custom Blog Theme! Need help? Contact the developer <a
10 href="mailto:yourusername@gmail.com">here</a>. For WordPress Tutorials
visit: <a href="http://www.wpbeginner.com"
target="_blank">WPBeginner</a></p>';
11 }
Remember to change the email and add any other information you want. Result would look something like
this:
Source: Jake Goldman’s Article at Smashing Magazine
Have you seen the default Mystery man avatar on blogs? Why waste the branding opportunity for your clients?
You can replace the mystery man with a custom branded gravatar for your clients to give their site a unique
Don’t forget to upload a custom image to your theme’s image folder. Also change the name of the gravatar to
their brand name. Once you upload the image and the functions, then visit: WP-Admin » Settings » Discussion
Your gravatar will show up as one of the options.
Often you will come across sites with outdated copyright dates. Some sites show the current year as their
copyright date. Both of these are annoying, and it shows that the site designer was lazy. In order to give your
users a little background info about your site, you should display the copyright date as such: © 2006 – 2010.
Once you add this function, then open your footer.php file and add the following code wherever you like to
This function looks for the date of your first post, and the date of your last post. It then echos the years
Now many blogs are publishing other authors also known as guest authors. A lot of these guest authors are one
time writers. In this scenario, it is not feasible to create user accounts for them. This trick will let you replace
the author name to guest author with the use of a custom field. Simply paste the code below:
01 add_filter( 'the_author', 'guest_author_name' );
02 add_filter( 'get_the_author_display_name', 'guest_author_name');
03
04 function guest_author_name( $name ) {
05 global $post;
06
07 $author = get_post_meta( $post->ID, 'guest-author', true );
08
09 if ( $author )
10 $name = $author;
11
12 return $name;
13 }
Now every time there is a guest post, simply add the custom field guest-author.
you must enable the post thumbnails inside your functions.php file. Paste the code below:
1 add_theme_support( 'post-thumbnails' );
Then simply place the following code inside your loop where you want to display the thumbnail:
1 <?php the_post_thumbnail(); ?>
WordPress 3.0 comes with an awesome menu management system which theme developers can utilize to their
advantage. If you want to give your users the option to use this core feature, then add the following function:
1 add_theme_support( 'menus' );
Then you can call this function anywhere you like in your template file by pasting the code below:
<?php
1 wp_nav_menu( array( 'sort_column' => 'menu_order','container_class' => 'menu
-header' ) ); ?>
If you are creating a site where your client will be adding user information, then make the interface easier for
them. One of the ways you can do this is by removing the unwanted fields such as AIM, Yahoo IM, Jabber etc.
Source: Strangework
profile. The code below will show you how to add additional twitter and facebook fields, but you can use it to
You can then call the fields in your author.php template by adding the following code:
1 <?php echo $curauth->twitter; ?>
Source: Yoast
This is one of the most used ones and many developers already know about this. But it deserves to be in this
list for those who don’t know. Paste the following code in your functions.php file:
01 if ( function_exists('register_sidebar') )
02 register_sidebar(array('name'=>'MiddleSidebar',
03 'before_widget' => '<li class="widget">',
04 'after_widget' => '</li>',
05 'before_title' => '<h2 class="widgettitle">',
06 'after_title' => '</h3>',
07 ));
08 register_sidebar(array('name'=>'FooterSidebar',
09 'before_widget' => '<li class="widget">',
10 'after_widget' => '</li>',
11 'before_title' => '<h2 class="widgettitle">',
12 'after_title' => '</h3>',
13 ));
The code above will register two sidebars. You can register as many as you need. To display the sidebar in
Do the same for the other sidebars. Note sidebars does not have to be in yoursidebar.php file.
Have you seen blogs that adds their advertisement in their RSS Feeds below each post. You can accomplish
that easily with a simple function. Paste the following codes below:
1 function wpbeginner_postrss($content) {
2 if(is_feed()){
was written by Syed Balkhi and after the content Check out WPBeginner. But we add the function if(is_feed),
You can do this with a plugin called RSS Footer by Joost De Valk.
The post thumbnail feature is usually only displayed within your site design, but you can extend that
functionality to your RSS feed with a simple function. Paste the codes below:
01 function rss_post_thumbnail($content) {
02 global $post;
03 if(has_post_thumbnail($post->ID)) {
04 $content = '<p>' . get_the_post_thumbnail($post->ID) .
05 '</p>' . get_the_content();
06 }
07 return $content;
08 }
09 add_filter('the_excerpt_rss', 'rss_post_thumbnail');
10 add_filter('the_content_feed', 'rss_post_thumbnail');
Threaded comments is one of the best feature WordPress has. You can enable the threaded comments by
visiting your wp-admin settings > discussion area, but you still need to add the JavaScript to your header file to
make commenting easier. When developing themes, you want it to be as clean as possible, so stop cluttering
When you enter a wrong password or an invalid username, you get an error message in the login page. So if a
hacker gets one thing right, the error message will help them identify that. You can stop helping the hacker by
When using WordPress as a CMS, sometimes the search feature becomes unnecessary. You can remove the
search bar from the design, but the functionality still remains. You can add the following function and disable
Source: WPEngineer
Adsense is one of the most popular ad elements used by bloggers. Theme designers can place the adsense box
in one spot which limits the users. If you want to give your client the ability to add the adsense anywhere, then
you can create a shortcode for the adsense using this function:
01 function showads() {
02 return '<div id="adsense"><script type="text/javascript"><!–
03 google_ad_client = "pub-XXXXXXXXXXXXXX";
04 google_ad_slot = "4668915978";
05 google_ad_width = 468;
06 google_ad_height = 60;
07 //–>
08 </script>
09
10 <script type="text/javascript"
11 src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
12 </script></div>';
13 }
14
15 add_shortcode('adsense', 'showads');
If you setup feedburner for your blog, then you should redirect your WordPress feeds to the feedburner feed
links. This way you will not lose any subscriber. Normally users do it with a plugin, but you can do it for your
There are times when you are writing a post, and you get an opportunity to plugin your donate link. This
function makes it easy for you by enabling a shortcode which you can use in your posts at any given time.
Make sure to change your account information and the text if you like.
Source: ThemeForest
All bloggers make errors that we catch after we publish the post. Sometimes even within the next minute or
two. That is why it is best that we delay our posts to be published on the RSS by 5-10 minutes. You can do that
This code is adding a 10 minute delay on your post being shown on the RSS Feeds, you can change it by
There is an annoying […] added at the end of each excerpt. You can change this to anything you like with a
simple function:
01 // custom excerpt ellipses for 2.9
02 function custom_excerpt_more($more) {
03 return '…';
04 }
05 add_filter('excerpt_more', 'custom_excerpt_more');
06
07 /* custom excerpt ellipses for 2.8-
08 function custom_excerpt_more($excerpt) {
09 return str_replace('[...]', '…', $excerpt);
10 }
11 add_filter('wp_trim_excerpt', 'custom_excerpt_more');
12 */
By default the excerpt length is capped at 55 words. Many theme designers like to have the flexibility that is
why WordPress lets you customize the excerpt length with this function:
1 function new_excerpt_length($length) {
2 return 100;
3}
4 add_filter('excerpt_length', 'new_excerpt_length');
looks really bad especially when you are not displaying the trackbacks and pings. To fix the count add the
following function:
01 add_filter('get_comments_number', 'comment_count', 0);
02 function comment_count( $count ) {
03 if ( ! is_admin() ) {
04 global $id;
$comments_by_type =
05
&separate_comments(get_comments('status=approve&post_id=' .$id));
06 return count($comments_by_type['comment']);
07 } else {
08 return $count;
09 }
10 }
For static WordPress sites, you do not need to have an RSS feed option. You can disable the RSS feeds with
Source: WPEngineer
There are widgets that display Twitter followers count, but those are limited and ugly. You can use this
function to customize the way your twitter followers count look on your blog. Simply paste the code below:
01 function rarst_twitter_user( $username, $field, $display = false ) {
02 $interval = 3600;
03 $cache = get_option('rarst_twitter_user');
0 $url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($u
4 sername);
05
06 if ( false == $cache )
07 $cache = array();
08
09 // if first time request add placeholder and force update
10 if ( !isset( $cache[$username][$field] ) ) {
11 $cache[$username][$field] = NULL;
12 $cache[$username]['lastcheck'] = 0;
13 }
14
15 // if outdated
16 if( $cache[$username]['lastcheck'] < (time()-$interval) ) {
17
18 // holds decoded JSON data in memory
19 static $memorycache;
20
21 if ( isset($memorycache[$username]) ) {
22 $data = $memorycache[$username];
23 }
24 else {
25 $result = wp_remote_retrieve_body(wp_remote_request($url));
26 $data = json_decode( $result );
27 if ( is_object($data) )
28 $memorycache[$username] = $data;
29 }
30
31 if ( is_object($data) ) {
32 // update all fields, known to be requested
33 foreach ($cache[$username] as $key => $value)
34 if( isset($data->$key) )
35 $cache[$username][$key] = $data->$key;
36
37 $cache[$username]['lastcheck'] = time();
38 }
39 else {
40 $cache[$username]['lastcheck'] = time()+60;
41 }
42
43 update_option( 'rarst_twitter_user', $cache );
44 }
45
46 if ( false != $display )
47 echo $cache[$username][$field];
48 return $cache[$username][$field];
49 }
Then place the following code where you want to display the count in your theme file:
1 echo rarst_twitter_user('wpbeginner', 'name').' has '.
2 rarst_twitter_user('wpbeginner', 'followers_count').' followers after '.
3 rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';
Source: Rarst
This list does not do justice to the power of the functions.php file. Entire theme frameworks are built solely on
this one file. You can use it to add custom post types, create theme admin panels, and much more.
Additional Resources
Digging into WordPress – One of the best e-books for WordPress developers. It has some of the best uses of
functions.php file.
Go to Any Design / Development related blog and type functions.php in their search to see what you can do
with this file. Just on our site, you should be able to find 30+ articles about the uses of functions.php file. Try it
for yourself:http://www.wpbeginner.com/?s=functions.php