Easily Customize WordPress’ Default Functionality


  

The infinite best thing about WordPress is how flexible it is. Don’t suchlike the theme? Simply change it. Demand added functionality? Within is belike a plugin you can download Or buy. If not, build it yourself! You can change pretty much thing about WordPress. In this article, we’ll go over some easy ways to customize WordPress that you might not know almost.

featured-image

Translate The Default Beginning Of jQuery

Another great thing roughly WordPress is that it comes locked and loaded with all kinds of JavaScript libraries, plus jQuery. It also gives you the power to change the source of those libraries according to your desires.

Let’s chance we want to relieve our server of some stress by shift WordPress’ revision of jQuery for a hosted solution (whether CDN version). We can very easily change the source of jQuery enclosed function:

operate add_scripts() {
wp_deregister_script( ‘jquery’ );
wp_register_script( ‘jquery’, ‘http://code.jquery.com/jquery-1.7.1.min.js’);
wp_enqueue_script( ‘jquery’ );
}

add_action(‘wp_enqueue_scripts’, ‘add_scripts’);

Three things are going against present. Primary, we’re using wp_deregister_script() to tell WordPress to forget about the version of jQuery right now being used. We then use wp_register_script() to re-register jQuery (as the script label), using jQuery’s own CDN journal. Finally, we operate wp_enqueue_script() to add jQuery to our theme or plugin.

One thing to billet here is that we’re using add_action(), not add_filter(), to add our scripts. Because we’re not devising any changes to the content, simply instead relying on WordPress to do something in order for our scripts to mountain, we use an action hook, not a filter hook. Read about both in the WordPress Listing.

Add Carving Sizes

We know that WordPress sets a few different sizes for the images we upload. Did you know you can also (relatively easily) set your personal image sizes? And all with two primitive functions. Whether we’ve a habit header image for our posts with dimensions of 760 × 300 pixels, we could make our similes upload to that size with this:

add_theme_support( ‘post-thumbnails’ );
add_image_size( ‘post-header’, 760, 300, faithful );

The first function, add_theme_support(), tells WordPress to allow not just for thumbnails, but for obvious images and images of new sizes. The second line, add_image_size(), is wherever we add our immature size. This function accepts four arguments: name, width, height and if to crop the image. WordPress also advises strongly resistant using certain reserved obloquy (read: carry out not use them): thumb, thumbnail, medium, large and post-thumbnail.

Once our new size is created, we can add it to the post’s loop to display it to users, like so:

if ( has_post_thumbnail() ){
the_post_thumbnail( ‘post-header’ );
}

This checks to see whether you’ve uploaded associate image and made it the post’s featured dummy. If truthful, WordPress displays it. You can also get fancy and add a default fallback image.

if ( has_post_thumbnail() ){
the_post_thumbnail( ‘post-header’ );
}else{
Post Header Image
}

Therein case, provided the post has no thumbnail, it falls back to the default image. Hooray, continuity!

Change the Sidebar’s Markup

Registering a sidebar doesn’t take much. All you really need is a name and an EGO to fashion it clear within the admin area:

register_sidebar( array (
‘name’ => __( ‘Sidebar’, ‘main-sidebar’ ),
‘id’ => ‘primary-widget-area’
));

WordPress will apply the default profit all for us, which we can mode as we want. However, we can also ADHD our own markup and sort it as we want. I prefer to exercise divs all for sidebar widgets because they are more semantically correct than list items. I also prefer h3 for widget headings because I usually hang-up h2 for the blog post’s title. So, with that in mind:

register_sidebar( listings (
‘name’ => __( ‘Sidebar’, ‘main-sidebar’ ),
‘id’ => ‘primary-widget-area’,
‘description’ => __( ‘The earliest widget area’, ‘wpbp’ ),
‘before_widget’ => ”,
‘after_widget’ => “”,
‘before_title’ => ‘

‘,
‘after_title’ => ‘

‘,
) );

This produces a sidebar that looks like this:


The sidebar with markup. Isn’t the Web Developer Toolbar great? (View image.)

Alter The RSS Widget’s Refresh Rate

WordPress’ integral RSS widget is fantastic, but sometimes it doesn’t update often enough. Happily, there’s a fairly simple treatment for that. Just include this code to your functions.php file:

add_filter( ‘wp_feed_cache_transient_lifetime’,
create_function(‘$a’, ‘return 600;’) );

As you can see, we are using WordPress’ add_filter() function, which accepts a device catch, callback run and (optional) priority. The wp_feed_cache_transient_lifetime hook handles the feed’s refresh rates. We’re creating our callback function on the fly using PHP’s create_function() function. It is one line, which returns the refresh rate in seconds. Our review rate is set to 10 mins.

Add Content To The RSS Feed

WordPress’ ability to add targeted content to an RSS feed (i.e. content that only your subscribers can see) is especially cool. This could be thing such as ads, a hidden message or value-added content. In the following example, we’ll put in a hidden e-mail.

function add_to_feed($content){
$content .= “

Thanks for Subscribing! You’re the best ever!

“;
rush back $content;
}

add_filter( “the_content_feed”, “add_to_feed” );

Using the filter the_content_feed, which is called only the feed is created, we use a callback function to append new information to the post’s content. If we looked element our website’s nurture in Google Reader, we’d see this:

Here's our super-secret message to all subscribers.
Here’s our super-secret message to all subscribers. (Scenery figurine.)

Highlight The Author’s Comments

Ane fairly rampant practice is to set off the author’s notes from the comments of readers. I do it on my blog:

My blog hasn't seen a whole lot of activity lately.
My blog hasn’t seen a unharmed good deal of activity recently. (View dummy.)

Hence, how do we accomplish this? I’m glad you asked! See, in a pre-2.7 world, some custom coding was required to determine if the author’s ID matches the commenter’s. On my diary, I just used to check whether the commenter’s ID was 1, which was the ID of the admin. Not really good I know, but I was young and naive (As healthy as the blog’s only author).

Text 2.7+ has a fancy little function named wp_list_comments, which prints a post’s comments for us. Even better, it applies the people .bypostauthor to any comments past — you guessed it — the post’s author. Now, to manner the author’s comments differently, all we need to do is this:

.statement { /* Scholar comments */
background: #FFFFFF;
color: #666666;
}

.bypostauthor { /* Author comments */
background: #880000;
color: #FFFFFF;
}

Through! Easy, spot on?

Tip: If you don’t like that WordPress tells you what markup to activity in the comments section, you can tell it to use your own print function:

Then, you create a activate named my_comment_display(), which prints your comments as you see fit. More information thereon in the Codex.

Modify Published Content

Just arsenic we modified the feed’s jovial earlier, we can do the self thing with our website’s content using the the_content filter. What we’ll execute here is append the author’s signature to the end of the content:

operate sig_to_content($content){
$content .= ‘

Joseph L. Casabona

‘;
return $content;
}

add_filter( “the_content”, “sig_to_content” );

By now you’re getting the hang of the add_filter() and recall functions. Within this case, our filter is the_content. This volition add on a name to the end of both posts and pages. To filter out pages, we but ADD this condition:

function sig_to_content($content){
if(is_single()){
$content .= ‘

Joseph L. Casabona

‘;
return $content;
}
}

This function, is_single(), checks to see if we’re showing a single post (as opposed to the content’s relationship status).

Conceive A Tailored Template For A Taxonomy

I started using WordPress way back in 2004, before pages, plugins or rich editing were introduced. Thinking about its evolution into an amazingly flexible platform, I remember doing custom stuff with certain categories and tags using if statements in the single.php template report. It’s much easier now.

WordPress has an incredibly sophisticated template hierarchy. Everything falls back to index.php, while templates such as leaf.php and single.php big deal various types of content differently. But you could get limited with category.php and even home.php. As you can imagine, home.php would be your domestic page, and category.php would display relevant posts when the visitor is viewing a class page. What you might not cognize is that you can get category-specific. By creating a template page named category-[slug].php or category-[id].php (that is, for example, category-news.php or category-6.php), you’re telling WordPress, “Use this template specifically for this category.” I usually replica scale.php or category.php and modify them.

Create The Search Tablet

On the same note, you can customize the search bar by creating a model folio named searchform.php, which faculty include only the search form. By default (i.e. when there is no searchform.php), here’s what we’re looking at:



Source: http://www.smashingmagazine.com/2012/06/20/easily-customize-wordpress-default-functionality/

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: