Category: HOWTOs

Create Anti-Spam Email Address in WordPress

I recently found a gem in the WordPress documentation. If you are looking for a way to display an email address but you do not want to use the actual email address because you are worried about spam, worry no more. Check out the “antispambot()” function that we have in WordPress. This function will take the email address that you want to use and obfuscates it. When you view the page you see the actual email address. When you view the source you do not see you see an encoded email address.

Example
In the example below – I will am using the WordPress function “antispambot” to create a mailto link for the post author (this would go in the loop).

<a href="mailto:<?php echo antispambot(get_the_author_meta('user_email')); ?>">Email the Author</a>

Further Documentation
http://codex.wordpress.org/Function_Reference/antispambot

Easily Add Page Excerpts in WordPress

Ever need to add an excerpt field for WordPress when working with pages? There is an easy, one line way to do this (no plugin needed). Simply add this to your theme’s functions.php file:

add_post_type_support( 'page', 'excerpt' );

Once you have that in place – you can then use the WordPress the_excerpt() function to show the excerpt within the theme.

Change the_excerpt Length and Ending

Using WordPress – there are some times when you want to use the_excerpt but you do not want to show the default 55 words and/or show the […] after those 55 words.

Change the Length

Instead of only showing the default 55 words where you want to change it to something longer or shorter. To change this – just add/modify this in your functions.php file:

add_filter('excerpt_length', 'sdac_excerpt_length');
function sdac_excerpt_length( $length ) {
	return 75;
}

In that code example – I am setting the length to be 75 words.

Change the Ending

Instead of showing the default […] after the excerpt – you can omit that or change it. To change this – just add/modify this in your functions.php file:

add_filter('excerpt_more', 'jappler_excerpt_more');
function jappler_excerpt_more( $more ) {
        global $post;
	return '... <a>ID ).'"&gt;Read More &raquo;</a>';
}

In that example – I added a read more which links to the post permalink. (This filter is available with WordPress 2.9+)

Hopefully these examples will help a little bit the next time you want to use the excerpt but wished it was easy to change the output.

HOWTO: Add Custom Classes to Individual Items (wp_nav_menu)

The WordPress function wp_nav_menu() is a great way to give users the ability to control their menu items (WordPress 3.0+). You can easily add/delete/move menu items by using the WordPress admin. I recently was working on a site where I wanted to add a class to the first menu item – as the padding/border for this item needed to be different than the other menu items. Fortunately – this was very easy by doing the following:

  1. Click on the “Screen Options” tab at the top right of the admin panel when on the Menu admin page (Appearance > Menus).
  2. Select “CSS Classes” under “Show advanced menu properties”. (Also there are options to add a link target, description, and a link relationship).
  3. Edit your menu items and now notice there is a spot to add a class for each individual item.

This is a great way to add a “first” or “last” or anything else you want to add on certain items.

Why not just use the IDs?
I specifically did this because I was using multiple menus (same menu class) and instead of using the list item IDs to add custom CSS for multiple IDs – I simplified it by adding a “first” class that would then cover all my menus.

HOWTO: Import WordPress Links

Ever get ready to migrate your site to a new host and after you exported your data using WordPress’s admin > Tools > Export and then realize your WordPress links (defined in WordPress’s admin > Links) did not get exported?

Importing your WordPress links is an easy and painless process. Simply go to your WordPress Admin > Import > Blogroll > and type in the URL to your site – with “/wp-links-opml.php on the end (http://example.com/wp-links-opml.php).

No more links left behind!

Include or Exclude Category for RSS Feed or Search

There are certain times when you need to include or exclude certain categories in the feed or search results. The easiest way to take care of this is to add the following to your functions.php file.

function sdac_remove_from_feed( $query ) {
	if ( $query-&gt;is_feed ) {
		$query-&gt;set( 'cat',-1 );
	}
        return $query;
}
add_filter( 'pre_get_posts','sdac_remove_from_feed' );

The code above will exclude all posts that are in the category with ID 1 from your feed. If you wanted to exclude everything from the search results – you would change one line:
$query->is_feed to $query->is_search

If you wanted to only show items from the category with ID 1 – you would just need to remove the “-” before the category number.

Note: You can include/exclude items tagged, etc by changing the ‘cat’ to whatever else you would normally use in the query.

Neat and Useful wp-config Settings

There are a ton of great options available for you to put into your wp-config.php file which can help your site run smoother, give you that extra customization you wanted without the use of plugins, and aid in development and debugging.

Here are some highlights that can help save you time and headaches by adding the following to your wp-config.php file:

Help for the Average User

  • Change the Autosave Interval: define(‘AUTOSAVE_INTERVAL’, 30 ); (30 = 30 seconds)
  • Disable Post Revisions: define(‘WP_POST_REVISIONS’, false );
  • Modify Empty Trash Time: define(‘EMPTY_TRASH_DAYS’, 7 ); (7 = 7 days)
  • Auto Optimize Your Database: define(‘WP_ALLOW_REPAIR’, true);
  • Enable the Cache:define(‘WP_CACHE’, true);
  • Define Maximum # of Revisions:define(‘WP_POST_REVISIONS’, 5);

Help for Developers

  • Output Errors:define(‘WP_DEBUG’, true);
  • Save Queries for Analysis:define(‘SAVEQUERIES’, true);
  • Define WP Site URL (override settings):define(‘WP_SITEURL’, ‘http://’ . $_SERVER[‘SERVER_NAME’] . ‘/path/to/wordpress’); – great for moving sites from one host to another when the actual domain name is not yet available
  • Define WP Home (override settings):define(‘WP_HOME’, ‘http://’ . $_SERVER[‘HTTP_HOST’] . ‘/path/to/wordpress’); – great for moving sites from one host to another when the actual domain name is not yet available

Full documentation is available: http://codex.wordpress.org/Editing_wp-config.php

WordPress: Keep Up to Date – No Excuses

Over the past few days, there has been a lot of buzz going around about keeping WordPress up to date because of a specific worm going around that is causing havoc for out of date WordPress versions. The worm creates a user, hides it from the user list, and then inserts all kinds of junk into your posts.

There are multiple ways to easily find out when WordPress has been updated.

Since everyone who uses WordPress should stay on top of these updates, here are a few ideas on how to find out when a new version has been released:

  1. Pay attention the the “Upgrade Now” notice when you log into WordPress.
  2. Get notified by email with the Upgrade Notification by Email plugin
  3. Read regularly/subscribe to the WordPress blog
  4. Read regulary/subscribe to this blog

Updating is quick and easy – and should always be done after you have a database backup. Database backups are quick and easy as well with WP-DB-Backup plugin.

Not really sure if you want to do the upgrade yourself? We would be happy to do it for you – just contact us.

Get Category Name By ID (WordPress)

I have been working on a new WordPress theme (to be released this week) and in one of the admin screens – I needed to get the category name by using the category ID. To do this (in or outside of the loop):

<?php get_cat_name( $cat_id ) ?>

Super simple – super helpful.

Reference: http://codex.wordpress.org/Function_Reference/get_cat_name