Quantcast
Channel: WPDressing
Viewing all articles
Browse latest Browse all 16

Customize Your Own WordPress Theme Through Functions.php

$
0
0

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.

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.

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

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');

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' =&gt; 'Site map',
        'id' =&gt; 'sitemap',
        'depth' =&gt; 2
        ), $params));
 
        // create sitemap
        $sitemap = wp_list_pages("title_li=&amp;depth=$depth&amp;sort_column=menu_order&amp;echo=0");
        if ($sitemap != '')
	     {
                 $sitemap =
                 ($title == '' ? '' : "<h2>$title</h2>") .
                 '<ul>$sitemap</ul>";
             }
         return $sitemap;
      }
 
add_shortcode('sitemap', 'GenerateSitemap');

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' );

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-&gt;post_content);
        $meta = strip_shortcodes($post-&gt;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 Widgetized Areas:

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

if (function_exists('register_sidebar'))
    {
       register_sidebar(array(
	   'name' =&gt; 'My New Widgets',
	   'id'   =&gt; 'my-new-widgets',
	   'description'   =&gt; 'My New widgetized area.',
	   'before_widget' =&gt; '<div class="widget %2$s">',
	   'after_widget'  =&gt; '</div>',
	   'before_title'  =&gt; '<h4>',
	   'after_title'   =&gt; '</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. And so on.

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>

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.


Viewing all articles
Browse latest Browse all 16

Latest Images

Trending Articles





Latest Images