How to check if a post has the Excerpt field filled manually



the_excerpt( ) displays the content filled in the Excerpt field (if filled manually) or else it will display an automatic excerpt which refers to the first 55 words of the post’s content.

To check if the Excerpt was manually filled, wordpress has a function called has_excerpt( ). So, if you dont like WP’s auto-excerpt, you can write a few lines of code like:

1
2
3
4
5
<?php if ( has_excerpt( ) ) {
    the_excerpt();
} else {
    the_content();
} ?>

Make URLs within posts clickable

Did you know that WordPress has a function ‘make_clickable’ which converts plain text URLs to HTML links? It converts URI, www, ftp, and email addresses to clickable hyperlinks and also helps by fixing links within links.

To make use of this function in the most effective manner, edit your theme’s functions.php file and add the following line:

1
add_filter('the_content', 'make_clickable');

The above code will automatically make all links clickable in your post content. If you want the returned content to be used some place else,

1
2
3
$theContent = make_clickable(get_the_content( ) );
// Then, echo it wherever you wish to display the content
echo $theContent;

Source: Codex

How to remove the WordPress Admin Bar

WordPress 3.1 came out with a couple of new features, with the inclusion of an “Admin Bar” being the most visible of them all. If you are unhappy with the bar and want to take it off your blog, here are a few ways:

Method 1: From the good ol’ options page


Goto Users > Your Profile and uncheck “Show Admin Bar” from the options. However, this is a per-user option and wont be feasible if you want to disable the bar for a multi-user blog.

Method 2: Edit your theme’s functions.php file

Add the following lines of code to disable the Admin Bar globally

1
remove_action('init', 'wp_admin_bar_init');

Also, to disable “Show Admin Bar” option for all your users, you can add the following line too:

1
remove_action( 'personal_options', '_admin_bar_preferences' );

Method 3: Using plugins

If you do not have permissions to edit your theme files, you can go with installing plugins. With the release of WP 3.1,

Global Hide/Remove Admin Bar Plugin – Add a global option in Settings Menu to hide/remove the new Admin bar in WP 3.1 and above.

Admin Bar Removal (completely disable) 3.1 only – Completely disables Frontend and Backend Menu that now appears on the WordPress 3.1 based blogs, plus helps to remove code and get more free memory.

Admin Bar Disabler – Disable the WP Admin Bar in 3.1+ completely, or only for roles and capabilities which aren’t in the ‘whitelist’/‘blacklist’. This plugin can help you disable admin bar for subscribers on your blog.

Fix “Is its parent directory writable by the server?” error for WordPress Image uploads

You might encounter an error saying something like:

“Unable to create directory /nfs/05/username/wp-content/uploads/2011/03. Is its parent directory writable by the server?”

Naturally, the first thing we’d try to do is change the /uploads/ directory permissions to 777. Here’s a way to fix the error without having to CHMOD directories.

Goto Settings > Media from your WP Dashboard and change” to “wp-content/uploads”
PS: NOT “/wp-content/uploads” or “/nfs/05/username/wp-content/uploads/”

Please note that there is NO forward slash before wp-content/uploads. Hope this solves the media upload problem. Else, try CHMOD’in the uploads/ directory to 777.

Source: TRLocke’s suggestion found at WP Forum

Displaying a random blogroll link

We are going to make a simple function in WordPress to fetch a random blogroll from the WP database and display it as a link.

Place the following lines of code in your theme’s function.php file.

1
2
3
4
5
6
7
8
9
10
11
12
function random_blogroll($number=1) {
        global $wpdb;
        $query = "SELECT link_url, link_name, link_description FROM $wpdb->links WHERE link_visible = 'Y' ORDER BY rand() LIMIT ".$number;
        $results = $wpdb->get_results($query);
        foreach ($results as $result) {
            $link_url = $result->link_url;
            $link_name = $result->link_name;
            $link_description = $result->link_description;
                $retsrting .= '<a href="'.$link_url.'" rel="external" title="'.$link_name.'">'.$link_name.'</a> ';
            }
            return $retsrting;
    }

To display the random link in your blog, place the following code where you want the link(s) to appear:

1
if(function_exists('random_blogroll')) echo random_blogroll();

To display ‘x’ number of links, pass the number as a parameter to the function

1
2
/* Example - To display 4 Blogroll links */
if(function_exists('random_blogroll')) echo random_blogroll(4);

Remove “View all posts filed under” from wp_list_categories Title attribute

WordPress automatically includes the Title attribute for each element in the wp_list_categories() function. It looks pretty bad when it you’re implementing the category list in a place like the top navigation menu.

1
wp_list_categories();

Here is a way to take off the “Title” attribute from wp_list_categories(). wp_list_categories has a parameter echo [like many other wp functions], which when set to 0 does not echo the code onto the screen. This way, we can get the whole string onto a php variable.

To change Title attribute to “Category Name” instead of “View all posts filed under Category Name” :

1
2
3
$categoriesVar = wp_list_categories("echo=0");
$categoriesVar = str_replace( "View all posts filed under " , ' ' , $categoriesVar );
echo $categoriesVar;

To completely remove the title attribute:

1
2
3
$categoriesVar = wp_list_categories("echo=0");
$categoriesVar = preg_replace( '/title=\"(.*?)\"/' , ' ' , $categoriesVar );
echo $categoriesVar;

The same code can also be written in a single statement:

1
2
echo preg_replace('/title=\"(.*?)\"/','',wp_list_categories("echo=0&amp;title_li="));
/* Strips off Title attribute from wp_list_categories */

Update: Feb 08, 2010

WordPress checks for the category description before it defaults the title to “View all posts filed under “. So, you could just add a single whitespace as the description of categories to not show anything in the title.
- Thanks to Justice Chad for this.

Adding lightbox overlay to default WP gallery

Sure, WP has a great inbuilt gallery feature. Unfortunately it does not allow you to change the ‘rel’ attribute for links. It’s odd that wp doesn’t already provide an option to change the rel attribute for the hyperlinks that link directly to the image attachments. Here is a simple code hack to make your WP gallery images open up in an overlay screen like lightbox or any other js modal window.

Change ‘rel’ attribute of WP default gallery

You will have to edit post-template.php in the wp-includes directory. Find the following line of code (WP 2.8 has it in line#946)

946
947
return apply_filters( 'wp_get_attachment_link', "<a title="$post_title" href="$url">$link_text</a>",
$id, $size, $permalink, $icon, $text );

Change the above line of code with

946
947
948
return apply_filters( 'wp_get_attachment_link',
"<a title="$post_title" rel="shadowbox[media]" href="$url">$link_text</a>",
$id, $size, $permalink, $icon, $text );

That’s it. Now all your image attachments would open up in a lightbox. [PS: The code above will be found in a single line in post-template.php. It has been broken down into multiple lines for easy readability.]