Wednesday, January 7, 2015

10 WordPress Snippets For Theme Customizations

When you are using WordPress, it is extremely easy to change the look of your website, thanks to themes. There are lots (like a whole bunch) of WordPress themes available both for free or for a premium price. Pick a theme, install it, and you can get a completely new look for your website within minutes.



But beyond giving the look and feel, a WordPress theme can be extended in many ways as well. You can build new functionalities with plugins, but in this post, we’re looking at WordPress functions that can be handy for your theme. You just have to put these functions in the functions.php file of your theme for the effect to take place.


Recommended Reading: How To Manage And Use Code Snippets In WordPress


1. Change The Length Of Excerpt


An excerpt is a short piece of your post that you can see. WordPress, in this case, sets the excerpt to be 55 words long by default. But, WordPress allows us to customize the default length through the excerpt_length filter, as follows.


 function my_excerpt_length( $length ) return 30; add_filter( "excerpt_length", "my_excerpt_length", 999 ); 

The return value refers to the sum of words that will be displayed as an excerpt. In the example above, we display 30 words worth of each post in the excerpt.


2. Reduce Post Revisions


WordPress lets writers and bloggers look back at previous versions of their work. However, as the revisions grow in number, they may also affect a website’s performance as each newly recorded revision adds a new row to the database. This problem will only get worse over time.


To solve this issue, you can set how many revisions you want to save in the database. To do this, open your wp-config.php and add this snippet below. Change the number to limit the number of revisions you want saved.


 define("WP_POST_REVISIONS", 5); 

If you would rather disable WordPress revision, switch the value to FALSE like this:


 define("WP_POST_REVISIONS", false); 

3. Automatically Set Featured Image On A Post


It’s common practice to display a featured image that represents or describes a post. WordPress requires us to set this featured image manually. To make the process more efficient, we can set the featured image automatically by making the first image in the post the featured image. Use the following code snippet.


 function autoset_featured() global $post; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_has_thumb) $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" ); if ($attached_image) foreach ($attached_image as $attachment_id => $attachment) set_post_thumbnail($post->ID, $attachment_id); add_action("the_post", "autoset_featured"); add_action("save_post", "autoset_featured"); add_action("draft_to_publish", "autoset_featured"); add_action("new_to_publish", "autoset_featured"); add_action("pending_to_publish", "autoset_featured"); add_action("future_to_publish", "autoset_featured"); 

4. Force Minimum Comment Length


Not a fan of the 1-2 word comments like “Nice Post!”, “Good Job!”? Although the comment might not be spam, these are the type of comments that will not typically encourage further discussions. If you want to get your commenters talking, you can set a minimum length of words required before readers can post their comment.


Below is the snippet to set the minimum number of characters or words for comments. Put it in functions.php.


 function minimal_comment_length( $commentdata ) $minimalCommentLength = 20; if ( strlen( trim( $commentdata["comment_content"] ) ) 

$minimalCommentLength value is the minimum number of characters that is required, make your changes to this value to tweak this.


5. Disable Links From User Comments


Links that are included in the comments form will instantly become a clickable link once they are posted and approved. This can be exploited by spammers, encouraging them to flood your comment section with a link to their "spammy" page.


To counter this, you can add this filter to disable the click-ability of the link(s) and retain them simply as plain text.


 remove_filter("comment_text", "make_clickable", 9); 

6. Remove Class And ID"s From Custom Menus


If you look at the custom menu in WordPress, you will find a bunch of classes and ids on every menu item. Use the snippet below to remove the classes you don"t want and to keep the classes that you need.


 function my_css_attributes_filter($var) return is_array($var) ? array_intersect($var, array("current-menu-item")) : ""; add_filter("nav_menu_css_class", "my_css_attributes_filter", 100, 1); add_filter("nav_menu_item_id", "my_css_attributes_filter", 100, 1); add_filter("page_css_class", "my_css_attributes_filter", 100, 1);

In the above code, we are keeping the current-menu-item class.


7. Exclude Pages From Search


Doing searches in WordPress will pull results from both pages and posts, sometimes with not much relevance to your search query. To prevent this, you can filter the search results to show only those found in posts. Add this snippet to function.php to do this.


 function SearchFilter($query) if ($query->is_search) $query->set("post_type", "post"); return $query; add_filter("pre_get_posts","SearchFilter"); 

8. Replace Howdy Text


Bored with the "Howdy" text? If you want to change that to a special salutation on your site, just add this to functions.php and the effect will take place immediately.


 function change_howdy($translated, $text, $domain) if (false !== strpos($translated, "Howdy")) return str_replace("Howdy", "Hello", $translated); return $translated; add_filter("gettext", "change_howdy", 10, 3); 

9. Add Additional Menu Removal For Particular Roles


You can hide menus that are in the Dashboard, for non-administrators, with this snippet.


 function remove_admin_menus() if(is_user_logged_in() && !current_user_can("administrator")) remove_menu_page( "index.php" ); //Dashboard remove_menu_page( "edit.php" ); //Posts remove_menu_page( "upload.php" ); //Media remove_menu_page( "edit.php?post_type=page" ); //Pages remove_menu_page( "edit-comments.php" ); //Comments remove_menu_page( "themes.php" ); //Appearance remove_menu_page( "plugins.php" ); //Plugins remove_menu_page( "users.php" ); //Users remove_menu_page( "tools.php" ); //Tools remove_menu_page( "options-general.php" ); //Settings add_action("admin_init", "remove_admin_menus"); 

Please note that this just removes the menus from the screen, but does not filter the user"s permission to access these menu. It does not prevent a user from accessing those menus directly through the browser address bar. Remember to add them to your functions.php.


10. Remove Admin Bar Link For Non-Adminstrators


In the WordPress Dashboard, besides the main menu on the sidebar, you will also find a couple of menu links at the top. You can restrict access to this admin bar link from specific roles or users. This snippet below will remove the menu in the admin bar for users who aren"t an Administrator – adjust accordingly.


 function remove_admin_bar_links() global $wp_admin_bar; if (!current_user_can("administrator")) $wp_admin_bar->remove_menu("wp-logo"); // Remove the WordPress logo $wp_admin_bar->remove_menu("about"); // Remove the about WordPress link $wp_admin_bar->remove_menu("wporg"); // Remove the WordPress.org link $wp_admin_bar->remove_menu("documentation"); // Remove the WordPress documentation link $wp_admin_bar->remove_menu("support-forums"); // Remove the support forums link $wp_admin_bar->remove_menu("feedback"); // Remove the feedback link $wp_admin_bar->remove_menu("site-name"); // Remove the site name link $wp_admin_bar->remove_menu("view-site"); // Remove the visit site link $wp_admin_bar->remove_menu("updates"); // Remove the updates link $wp_admin_bar->remove_menu("comments"); // Remove the comments link $wp_admin_bar->remove_menu("new-content"); // Remove the new content link $wp_admin_bar->remove_menu("my-account"); // Remove the user details tab add_action("wp_before_admin_bar_render", "remove_admin_bar_links"); 










10 WordPress Snippets For Theme Customizations

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.