Category: HOWTOs

SVN Ignore Icon Files

Here are three easy steps to ignore the annoying Icon files when using SVN on Mac OS X:

  1. Open up the terminal application and type:

    cd ~/.subversion
    nano config
  2. In that file – find the line which starts with # global-ignores. The “#” comments out the line. Remove the # at the beginning of the line and add Icon* to the end of the line. We use Icon* because, the Icon file actually has a return at the end and we want to make sure we match it.
  3. Save changes

Viola – no more issues with the Icon file!

Search and Replace Serialized Data

After a recent site migration – I found myself needing to make some changes to a particular setting within the wp_options table in which the data was serialized. This script: Safe Search and Replace on Database with Serialized Data made it extremely easy. If you ever need to do the same thing – definitely give this script a look. As always – make sure you have a DB back up first.

Avoid WordPress Plugin Overload

One of the greatest benefits of being a part of the WordPress community – is the access to the hundreds of thousands of existing plugins. A site owner can easily go from a vanilla site to something with a lot of functionality in a matter of minutes. On the same note – having access to all these plugins can easily cause more harm than help. Example: I recently worked with someone who was having problems with upgrading their site to the latest version of WordPress. I figured this would be a 30 minute task. After logging in and seeing they had over 60 plugins – 40+ which required upgrades – the task quickly grew.

Something to remember – plugins are not all created equally. Quick example – some plugin developers use best practices so that you do not have multiple copies of a javascript library loading while others will include multiple libraries regardless of what you may already have.

By keeping everything up to date is important – but I would also like to stress keeping a lean version of your site is equally important. If you need a certain custom functionality – take into consideration the server resources, time for updates/debugging, etc. versus what you really get from it. Debugging 1 plugin is manageable, debugging 10 plugins is a task, debugging 40 plugins is insanity. Also – if you decide that a plugin is just not working the way you expected – delete it and forget about. There is no reason you need to keep plugins that you do not need around. By keeping these files around – it also takes up valuable resources that could be better used elsewhere.

If you are looking for a fast and secure web site (who isn’t?) remember to keep it simple. Custom functionality is great – but don’t forget to think about the cost.

HOWTO: Add Page Slug to Body Class in a WordPress Theme

Even though the body_class() function in WordPress outputs a good deal of classes you can easy use – there are sometimes when having the actual page name as a class is helpful (custom CSS).

Usage Example:

  • Page Named: My Test Page
  • You need the ability to apply some CSS to something only on this page

In your functions.php file – add the following:

/* 
 * Add in page nicename to body class
 * @param $classes = array of class names or a string with space-separated classes names
 */
function sdac_body_page_name_class( $classes ) {
	global $post;
		$classes[] = $post->post_name;
	return $classes;
}
add_filter('body_class', 'sdac_body_page_name_class');

Once that function is in place you will then see this on the test page as a class in the body tag: “my-test-page”. With that in place you can then use something like this to make a page specific change in CSS:

.my-test-page .entry {margin:50px 0 100px 0;} 

HOWTO: Set Custom Default Gravatar in WordPress

If you ever wanted to set your own custom avatar as the default for Gravatar on your WordPress site – here is a simple way to do it. All you need to do is make your custom avatar accessible somewhere on the web by either uploading it to your site or another site, then add this code to your functions.php file in your active theme:

add_filter( 'avatar_defaults', 'sdac_custom_gravatar' );
function sdac_custom_gravatar ( $avatar_defaults ) {
     $custom_gravatar = 'http://media.sandboxdev.com/wp-content/themes/SDACINC2012/images/logo-small.png';
     $avatar_defaults[$custom_gravatar] = "SDAC Inc. Logo";
     return $avatar_defaults;
}

You will then see it in the list of gravatars in Settings > Discussion on your site as well.

This is quick and painless way to help brand your site.

HOWTO: Add Custom Classes to Post Navigation Links

Sometimes you just need to have a bit more control over the display of the post navigation (next_posts_link, previous_posts_link). You can easily add in your own custom classes by adding the code below to your theme’s functions.php file. In the example below – I added in a custom class “older” and “newer”. You can add in anything there you would like.

add_filter('next_posts_link_attributes', 'sdac_next_posts_link_attributes');
function sdac_next_posts_link_attributes(){
		return 'class="older"';
}

add_filter('previous_posts_link_attributes', 'sdac_previous_posts_link_attributes');
function sdac_previous_posts_link_attributes(){
		return 'class="newer"';
}

HOWTO: Easily Change Registration Redirect in WordPress

Ever want to change the redirect for your WordPress site but were not sure how to do it? There is a quick and easy way to accomplish this. Simply put the following code in your theme’s functions.php file and change the URL after the “return”.

add_filter( 'registration_redirect' , 'sdac_registration_redirect' );
function sdac_registration_redirect() {
    return 'http://my-redirect-url.com';
}

HOWTO: Add TinyMCE to a Textarea in WordPress

If you are building a plugin or simply adding some custom fields for your theme and would like to use the WordPress Visual/HTML editor – you can – with one easy line of code:

<?php wp_editor( $content, $editor_id, $settings = array() ); ?>

No extra added JS or anything else is needed! It is that simple. (This was introduced with WordPress 3.3)

Used in a real world example (custom fields for a custom post type):

<?php wp_editor( $sdac_profile_general_description, 'sdac_profile_general_description' );?>

That outputs a text area with the name “sdac_profile_general_description” and the value of the custom field.

I hope this helps out next time you find yourself wanting the Visual/HTML editor in place but struggling to get all the needed JS/HTML in place.

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

HOWTO: Make WordPress Titles Title Case

Ever need to make sure all your titles showed up with using “Title Case” and not just by using CSS? By adding the following to your functions.php file in your theme – you can ensure the post title will have the case you want:

/**
 * Custom Title Case for the_title()
 * @returns $content (title with title case)
 */ 
add_filter( 'the_title', 'sdac_use_title_case' );
add_filter( 'wp_title', 'sdac_use_title_case' );
function sdac_use_title_case( $content ) {
	$content = ucwords( $content );
	return $content;
}