7 WordPress Mods That Don’t Need Plugins
Plugins are a powerful feature of WordPress. They add useful functionalities to your WordPress installation with ease of use.
Although they can be powerful, they can also be pretty bad. Not all plugin writers are experienced programmers, which can lead to bugs and performance issues. An other thing with plugins is that you have to maintain them by updating them regularly, and with regularly I mean really often…
For some simple features it is just better to write them yourself in your functions.php, or a single custom plugin. Here is a list of simple mods that don’t need plugins.
Add special user capabilities
If you’re using WordPress for client websites like me. You might have assigned the client the ‘editor’ role, instead of the ‘administrator’ role, so they can’t change important settings that break the WordPress install.
There are some features that you would like to have available to your clients, but that aren’t when they have the ‘editor’ role, like the Widgets page under Appearance.
1 2 3 | $role_object = get_role('editor'); // get the role $role_object->add_cap( 'edit_theme_options'); // add a capability $role_object->remove_cap( 'edit_theme_options'); // remove a capability |
For a list of all available capabilities you can read here.
Adding custom post types
Custom post types are a powerful feature of WordPress, which allows you to have other types then Posts or Pages. For example, if you have a website that sells books, you need a way to enter those book details.
Here is a template you can use to add custom post types. Just replace ‘book’ with any other type you want. Of course you can also add multiple custom post types.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /* * Register the custom post type for book * * @return void */ function register_post_type_book() { $labels = array( 'name' => _x('Books', 'post type general name'), 'singular_name' => _x('Book', 'post type singular name'), 'add_new' => _x('New Book', 'performance'), 'add_new_item' => __('Add Item'), 'edit_item' => __('Edit Item'), 'new_item' => __('New Item'), 'view_item' => __('View Item'), 'search_items' => __('Search Items'), 'not_found' => __('No items found'), 'not_found_in_trash' => __('No items found in your trash'), 'parent_item_colon' => '', 'menu_name' => 'Books' ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'books', 'with_front' => false, ), 'capability_type' => 'post', 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title','editor', 'thumbnail', 'excerpt') ); register_post_type('book', $args); } add_action('init', 'register_post_type_book'); |
More on custom post types here.
Add image sizes
Most website designs have images displayed at multiple sizes. You might—for example—show a posts’ featured image in a slideshow at a bigger size then when you show it in a listing. Wordpress makes it easy to define multiple image sizes. Just add the add_image_size function call to your functions.php. The first parameter is an identifier for this image size, the second and third are the dimensions and the last parameter indicates if the image should be cropped.
1 2 3 | add_image_size( 'book_thumb', 170, 120, true ); add_image_size( 'book_normal', 337, 470, true ); add_image_size( 'book_slideshow', 440, 310, true ); |
To use the new image size in your templates you can add the following code.
1 | the_post_thumbnail('book_slideshow'); |
Custom MCE buttons
WordPress comes with a full featured WYSIWYG editor by default. Although that might be nice for experienced users, most clients I work for don’t that have this experience. They like to give their texts funky colors, even though that doesn’t match with the overall webdesign. That’s why I normally limit the some of the editors functions.
Here is a template that limits the available blockformats, like H2 and H3 blocks, and disables the forecolor option.
1 2 3 4 5 6 7 8 | function custom_change_mce_buttons( $initArray ) { //@see http://wiki.moxiecode.com/index.php/TinyMCE:Control_reference $initArray['theme_advanced_blockformats'] = 'p,h2,h3'; // limit the available blockformats $initArray['theme_advanced_disable'] = 'forecolor'; // disabled forecolor return $initArray; } add_filter('tiny_mce_before_init', 'custom_change_mce_buttons'); |
Change Contact Info Form
Each user in WordPress have it’s own contact information. On default there are fields for AIM, Yahoo IM and Google Talk. Well, these services feel a bit old-school and you might want to add some new ones to the list—like Twitter and Facebook.
Here is a simple filter function that just does that:
1 2 3 4 5 6 7 8 9 10 11 12 | function new_contactmethods( $contactmethods ) { $contactmethods['twitter'] = 'Twitter'; // Add Twitter $contactmethods['facebook'] = 'Facebook'; // Add Facebook $contactmethods['linkedin'] = 'Linkedin'; // Add Linkedin unset($contactmethods['yim']); // Remove Yahoo IM unset($contactmethods['aim']); // Remove AIM unset($contactmethods['jabber']); // Remove Jabber return $contactmethods; } add_filter('user_contactmethods','new_contactmethods',10,1); |
Adding shortcodes
Shortcodes are cool. They allow you to add “special stuff” to posts or pages inline. For example: insert Adsense blocks into you posts or insert a (contact)form inline.
Here is my adsense shortcode:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * Insert adsense into you post or page. * * Usage: * [adsense align="left|right" slot="00000" width="100" height="100"] * * @param array $atts * @param mixed $content */ function adsense($atts, $content = null) { $nlbr = "\n"; // set defaults extract(shortcode_atts(array( "client" => 'ca-pub-000000000000000', "slot" => '00000000000', "width" => '200', "height" => '200', "align" => '', ), $atts)); // alignment $style = ""; switch($align) { case "left": $style.="float:left; margin-right: 10px;"; break; case "right": $style.="float:right; margin-left: 10px;"; break; } // output ad code $retval = '<div style="'.$style.'">' . $nlbr; $retval.= '<script type="text/javascript"><!--' . $nlbr; $retval.= 'google_ad_client = "'.$client.'";' . $nlbr; $retval.= 'google_ad_slot = "'.$slot.'";' . $nlbr; $retval.= 'google_ad_width = '.$width.';' . $nlbr; $retval.= 'google_ad_height = '.$height.';' . $nlbr; $retval.= '//-->' . $nlbr; $retval.= '</script>' . $nlbr; $retval.= '<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>' . $nlbr; $retval.= '</div>' . $nlbr; return $retval; } add_shortcode("adsense", "adsense"); |
Learn more about shortcodes here and here.
Customizing the Admin Help texts
The default contextual help texts of WordPress aren’t always that helpful, especially when you’re developing WordPress sites for clients. You might even want to add some additional help information about certain pages.
Here is a function to get you started:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /** * Customize contextual help texts * * @param string $contextual_help * @param string $screen_id * @param string $screen */ function my_contextual_help($contextual_help, $screen_id, $screen) { $help_text = ""; $debug = 0; // toggle this to find out which $screen_id a certain page has switch($screen_id) { case "dashboard": $help_text = "<p>Welcome to the Dashboard, hier ziet u een overzicht van alle pagina\"s.</p>"; break; case "upload": $help_text = "<p>Hier kunt u afbeeldingen beheren.</p>"; break; case "edit-page": $help_text = "<p>Hier kunt u paginas beheren.</p>"; break; case "page": $help_text = "<p>Hier kunt u een pagina bewerken.</p>"; break; default: $help_text = ""; // remove default Wordpress help text if ($debug) { $help_text = $screen_id; } break; } // at least add a support text to every help text $help_text.= "<p><em>For questions you can see our <a href=\"http://support.webcreate.nl/knowledgebase\" target=\"_blank\">Knowledge Base</a> or you can contact us at <a href=\"mailto:support@webcreate.nl\">support@webcreate.nl</a>.</em></p>"; return $help_text; } add_action("contextual_help", "my_contextual_help", 10, 3); |
Final words
These are just some of the mods you can do without plugins. Of course there are a lot more. Do you have any favorite mods/snippets of your own?
About Jeroen Fiege
Jeroen Fiege is a PHP webdeveloper and founder of Webcreate. Follow him on Twitter at @fieg.
-
Menno
-
Anonymous
