Hisham Sadek

10 Useful WordPress Functions for your theme

WordPress have changed a lot in the past few years, It turned to a complete CMS even if it still looks like a blog. It can do almost all what you need, from a simple website to a huge news website or e-commerce store. There are some useful functions theme developers should use.

1- Remove Admin Bar

If you have a WordPress website that give access to members or even if while you are checking the front-end and hate the Admin bar on top, putting this simple function in the functions file of your theme ‘functions.php’ will remove it

    // Remove Admin Bar
    add_filter('show_admin_bar', '__return_false');

2- Custom Post Excerpt

If you’re working on some news magazine theme you will need this function. This one simply makes you able to create custom excerpt for your posts to use instead of the default function the_excerpt() . You can define the length of excerpt so it would fit the design you are working on.

    // Custom Excerpts callback
    function etch_excerpt($length_callback = '', $more_callback = ''){
        global $post;
        if (function_exists($length_callback)) {
            add_filter('excerpt_length', $length_callback);
        }
        if (function_exists($more_callback)) {
            add_filter('excerpt_more', $more_callback);
        }
        $output = get_the_excerpt();
        $output = apply_filters('wptexturize', $output);
        $output = apply_filters('convert_chars', $output);
        $output = '

' . $output . '
<pre class="line-numbers"></pre>
'; echo $output; } function etch_custom_post($length){ return 40; }
<pre class="line-numbers"></pre>

What the code above does is to create the custom callback for excerpt, it gets the default excerpt from WordPress built-in function and modify it depending on the number of words you enter in the next function. Change the number ‘40’ with the number of words you want to appear in that custom excerpt. The last step is to call that custom function in your loop instead of the the_excerpt() or the_content() function.

    etch_excerpt('etch_custom_post');

3- Load a Conditional Script for specific page

This function simply makes you able to load a Javascript file to be loaded only in a specific page

    // Conditional Javascript
    function etch_conditional_script(){
        if (is_page('pagenamehere')) {
            wp_enqueue_script( 'scriptname', get_template_directory_uri() . '/js/scriptname.js', array(), '1.0', true );
        }
    }
    add_action('wp_print_scripts', 'etch_conditional_scripts');

In the code above, It detects if the page is the one you want to load the script in, then it loads the script. Change ‘pagenamehere’ with the Page name, slug or ID (Check is_page() function reference) and change ‘scriptname’ and ‘scriptname.js’ with the script name and path.

You can change the condition to load in other ways, for example if you want to load the script only in home so you use is_home() function instead. Learn more about Conditional Functions.

4- Enable Post Formats Support

This code enables support for Post Formats. Post formats allows you to customize how the post would look and it is widely supported in many of premium themes now. Learn more about Post Formats

    // Enable Post Formats
    add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio', 'chat' ) );

5- Enable HTML5 Markup

This code allows you to enable using HTML5 markup for the comment forms, search forms and comment lists.

    // Enable HTML5 Markup
    add_theme_support( 'html5', array( 'comment-list', 'search-form', 'comment-form', ) );

6- Pagination

This function extends the possibility of having Pagination for your posts without the need of any plugin. Embed this function in ‘functions.php’

    // Pagination Function
    function etch_pagination(){
        global $wp_query;
        $big = 999999999;
        echo paginate_links(array(
            'base' => str_replace($big, '%#%', get_pagenum_link($big)),
            'format' => '?paged=%#%',
            'current' => max(1, get_query_var('paged')),
            'total' => $wp_query->max_num_pages
        ));
    }

In the theme files, load the function after the loop ends.

    // Load Pagination
    etch_pagination();

7- Enable Translation for your theme

This simple function enables support for translating strings in your theme. First step is adding this function to your ‘functions.php’

    // Enable Localization Support
    function etch_theme_setup(){
        load_theme_textdomain('my_theme', get_template_directory() . '/languages');
    }
    add_action('after_setup_theme', 'etch_theme_setup');

Change ‘my_theme’ with something recognizable for you, and create a folder called language in your theme directory. You can learn more information about the next step for creating POT File and detecting strings from your theme.

8- Remove Unwanted code from head section

    // Remove Unwanted tags
    remove_action('wp_head', 'feed_links_extra', 3);
    remove_action('wp_head', 'feed_links', 2);
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wlwmanifest_link');
    remove_action('wp_head', 'index_rel_link'); 
    remove_action('wp_head', 'parent_post_rel_link', 10, 0);
    remove_action('wp_head', 'start_post_rel_link', 10, 0);
    remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); 
    remove_action('wp_head', 'wp_generator');
    remove_action('wp_head', 'start_post_rel_link', 10, 0);
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    remove_action('wp_head', 'rel_canonical');
    remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);

9- Add Custom Fields for User Profile

You can add custom fields to User Profiles if you want to show more information about users rather than Name and Website. This function and most of functions here can be used in Plugins too.

    // Custom User Fields
      function etch_custom_userfields( $contactmethods ) {
        $contactmethods['contact_phone_number']     = 'Mobile Phone';
        $contactmethods['address_line_1']       = 'Address Line 1';
        $contactmethods['address_line_2']       = 'Address Line 2';
        $contactmethods['address_city']         = 'City';
        $contactmethods['address_state']        = 'State';
        $contactmethods['address_zipcode']      = 'Zipcode';
        return $contactmethods;
       }
       add_filter('user_contactmethods','etch_custom_userfields',10,1);

10- Enable Threaded Comments

This function enables threaded comments in your WordPress very easily without the need to edit your header.php and add custom Javascript.

    // Enable Threaded Comments
    function enable_threaded_comments(){
        if (!is_admin()) {
            if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1)) {
                wp_enqueue_script('comment-reply');
            }
        }
    }
    add_action('get_header', 'enable_threaded_comments');

Thanks holaseba