Customize Your Own WordPress Theme Through Functions.php

Nothing is better than having a personalized WordPress theme that looks the way you like and prefer. Some people believe that they have to be savvy programmers to be able to customize their wordpress theme on their own. With all certainty, it is a myth and on the contrary, even those who have just a little bit of knowledge about programming can customize their wordpress theme at ease and without a hitch. Just read on this article to discover how things are easier than you think.

How to customize wordpress

About Functions.php

At the outset, let`s talk slightly about Functions.php. There, in your theme`s folder, you will find a file named functions.php. To change the standard functionality of WordPress, you can add some codes to that file. You can use functions.php file to extremely change the features and aspects of your wordpress theme instead of using plug-ins that sometimes are malicious and can cause many inconveniences to your site. Functions.php mainly is responsible for the display of content and the theme; however, it is possible to use it to extend your admin or other areas behaviors with new capabilities.

Editing Functions.php File

There are two ways to do this:

  1. Go to your WordPress dashboard (www.yoursite.com/wp-admin). Go to Appearance >> Editor. Choose the theme you want to modify from the dropdown menu, and then select the functions.php file from the right sidebar. But this method is very risky and your site may be prone to many mistakes.
  2. Use your FTP manager to access your WordPress files. Explore to wp-content/themes/your-theme. Open up the functions.php file and download a copy of your theme folder to your local computer. You need to back up the functions.php file before any change. Use a text editor to open the functions.php file and save as the file so that you can make a copy of the original file in a safe place, give it a proper name like “original-functions.php” and do not forget to close that backup copy. Now you are ready to go on.

Using Functions.php to build ShortCodes

WordPress offers numerous very specific and readymade shortcodes that allow users to apply nifty yet great functions with minimal effort. You can enter the specified shortcode in a certain post to perform particular function when actually viewing the post on the website, for example:

  • audio: that displays uploaded audio file as an audio player
  • rdio: embeds music from Rdio
  • gallery: inserts an image gallery into a post or page

The good news, you can easily and quickly build your own shortcodes as well, for a verity of purposes. Read on to educate yourself.

1. Printing The Words “Hello World” In Your Posts

Go to and open your functions.php file. Copy and paste the next codes in the file. Once done, close your Functions.php and go to any post and insert the shortcode [helloworld]. This is a very simple example just to be familiar with the process.

function HelloWorldShortcode() 
{
    return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');

How to customize wordpress

2. ShortCode for building a sitemap

To have amazing formatted list of all the pages in your blog, insert the next code into your functions.php file. Once done, enter the shortcode [sitemap] in the post or page you are targeting to get the output.

function GenerateSitemap($params = array()) 
{
    // default parameters
    extract(shortcode_atts(array(
        'title' => 'Site map',
        'id' => 'sitemap',
        'depth' => 2
    ), $params));

    // create sitemap
    $sitemap = wp_list_pages("title_li=&depth=$depth&sort_column=menu_order&echo=0");
    if ($sitemap != '')
		{
        $sitemap =
            ($title == '' ? '' : "<h2>$title</h2>") .
            '<ul>$sitemap</ul>";
    }
    return $sitemap;
}

add_shortcode('sitemap', 'GenerateSitemap');

3. Display a message only for authors

If you need to tell an author a certain message when he/she opens his/her published posts, insert the next code into your functions.php. Once done, go to the posts you are targeting and place this shortcode [note]

function display_author( $atts, $content = null )
{
     if ( current_user_can( 'publish_posts' ) )
        return '<div class="note">Message goes here</div>';
    return '';
}

add_shortcode( 'note', 'display_author' );

How to customize wordpress

4. Creating a meta description automatically from the content of your post or page

This shortcode is built to add the Meta description in the page but in the head HTML section, meaning, it is a mandatory to embed it in your header.php file; it cannot be run anywhere else in the content.

function create_meta_desc()
{
    global $post;

if (!is_single()) { return; }
    $meta = strip_tags($post->post_content);
    $meta = strip_shortcodes($post->post_content);
    $meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
    $meta = substr($meta, 0, 125);
    echo "";
}

add_action('wp_head', 'create_meta_desc');

Generally, to allow a shortcode to work anywhere in a WordPress theme file, simply insert the next php code.

<?php echo do_shortcode("[my_shortcode]"); ?>

Using Functions.php to build Widgitized Areas

When we navigate to Appearance >> widgets, we can find numerous ready to use widgitized area where we can drag and drop widgets into them. In all likelihoods, you will find a widgitized area for the footer and header, a post or page sidebar. Sounds great! But, what about building widgitized areas anywhere in your theme on your own? Of course, it is a gorgeous idea, for example, creating a widgitized area in certain places in the content. Let`s go on. Just, past the next code into your functions.php.

if (function_exists('register_sidebar'))
{
	register_sidebar(array(
		'name' => 'My New Widgets',
		'id'   => 'my-new-widgets',
		'description'   => 'My New widgetized area.',
		'before_widget' => '<div class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h4>',
		'after_title'   => '</h4>'
	));
}

To see the new widget that is called “My New Widgets”, Go to Appearance >> widgets”

Where do you like the widgets to show now? First, you need to edit the corresponding template file.

  • For pages use page.php
  • For posts, use single.php
  • For tag pages use tag.php
  • For categories use cat.php
  • For 404 error page use 404.php
  • For search results page use search.php
  • For archives use archive.php, archives.php

Add the next code in the targeted template file where you want the widgets to appear.

<div>
	<div class="no-widgets">

		<p><strong>Add some widgets</strong></p>

		<p>This is the default, add some widgets in the WP Admin</p>
	</div>
</div>

How to customize wordpress

In fact, each website has its own requirements in terms of widgets and the areas of placing those widgets. In Brief With your Functions.php file you can work magic. You can do more than you can imagine.

- Written by Mark Wilston -

He is a Content Writer and marketing professional working with PixelCrayons, a reputed Custom web design & development company India offering offshore cms, ecommerce and mobile apps development services. For more information about PixelCrayons, visit at http://www.pixelcrayons.com/.

1 Comment to “Customize Your Own WordPress Theme Through Functions.php”

Add Comments (+)

  1. bethany Crouch says:

    Great Info, I got my blog customized…

Trackbacks/Pingbacks

  1. Tweet Parade (no.45 Nov 2013) - Best Articles of Last Week | gonzoblog

Leave a Reply

 

Amazingly Beautiful WordPress Themes