February 24, 2015

10 Remarkably Unmissable Wordpress Code Snippets

Every Wordpress website owner continuously strives to improve the website's efficiency and productivity to achieve the best possible results.

Typically, we set off on a plugin installing spree when we start to feel our website needs some extra dose of functionality to boast. We start to look for the list of best plugins for either SEO, social media, for custom widgets, having a more personalized menu, playing around with the website's style and so on. So far so good, but it is no hidden fact that too many plugins only end up bloating our websites and severely affect their performance in terms of multiple areas.

This is where the small code snippets come in handy. Wordpress, for being as user friendly as it is, lets you edit the core files of your themes with utmost ease. There are several such snippets already doing the rounds. And in this post, I will introduce you to some that can really let you refine your website thoroughly.

1. For a Login Screen that Perfectly Demonstrates Your Brand Character

The login screens are unarguably the most ignored aspects of any websites, particularly in terms of styling. We go to great lengths to make sure no one unauthorized gets their way around them, but we hardly pay attention to make this page interesting for our viewing pleasure, or for making it very relevant to our brands, especially in scenarios where we have a number of authors and editors on our website.

So, in order to give the screen a more personalized touch, you can try out this code:

function loginLogo() { echo ''; } add_action('login_head', 'loginLogo');

2. Add Extra Contact Methods to User Profiles

Contact forms have indispensable importance building a loyal list of followers is on your agenda. When you have more users signing up and creating their profiles on your website, you are actually enlisting more and more potential buyers.

But, truth be told, Wordpress makes the whole exercise extremely mundane with its old-age contact forms and methods. So, here is a small code snippet that can be used to add more contact methods to your Wordpress website, just to make things more interesting for those who are interested:

add_filter('user_contactmethods', 'my_user_contactmethods');

function my_user_contactmethods($user_contactmethods){
 
  $user_contactmethods['twitter'] = 'Twitter Username';
  $user_contactmethods['facebook'] = 'Facebook Username';
 
  return $user_contactmethods;
}

All you got to do from hereon is give the methods personalized name and labels.

3. Making More Space for the Widgets

There is already a truckload of literature dedicated to the importance of the Wordpress widgets. They way let you add more versatility to your website is unparallel and thus, making enough room for them to snug in is of very high significance.

Here is a code for you to create the space with the right frame size so that they fit in naturally:

if (function_exists('register_sidebar')) {
     register_sidebar(array(
      'name' => 'Social Widget Area',
      'id'   => 'social-widget-area',
      'description'   => 'Social Widget Area',
      'before_widget' => '
', 'after_widget' => '
', 'before_title' => '

', 'after_title' => '

' )); }

4. Getting Rid of the Default Widgets

Talking of widgets, there is a lot of default fluff that Wordpress swings at us, and this is how we get rid of it:

// unregister some widgets
 function unregister_default_widgets() {
     unregister_widget('WP_Widget_Pages');
     unregister_widget('WP_Widget_Links');
     unregister_widget('WP_Widget_Categories');
     unregister_widget('WP_Widget_Recent_Comments');
     unregister_widget('WP_Widget_Tag_Cloud');
     unregister_widget('WP_Nav_Menu_Widget');
 }
 add_action('widgets_init', 'unregister_default_widgets', 11);

5. For echoing Shortcode in Template

Shortcodes are a revelation on the website. And for you to echo shortcode in the template, here is the smallest code for it:

6. To Register the Menu

Registering menu on the Wordpress website is something many webmasters are eyeing. Instead of looking for a plugin for the same, here is a code that you can use:

function register_my_menus() {
  register_nav_menus(
    array(
      'header-menu' => __( 'Header Menu' ),
      'extra-menu' => __( 'Extra Menu' )
    )
  );
}
add_action( 'init', 'register_my_menus' );

7. For the If Have Posts, While Have Posts, The Post for Extracting Database Content

The following code snippet is perfect when you need to extract some content from the database:


8. For Arranging the Content in Vertical Order

When you want the content to look organized and free of clutter, aligning it vertically serves the perpose just right. Here is the code for the same:

.element {
  position: relative;
  transform: translateY(-50%);
  top: 50%;
}

9. Disable the Automatic Update of Plugins

while there is no doubt over the fact that we should continually update our Wordpress theme and all the plugins, but there is a flip side to it. Many times, updates lead to certain uninvited errors ad put our website in a fix.

Thus, it is better to keep the rein in our hands and disable the updates from happening automatically:

add_filter('site_transient_update_plugins', 'dd_remove_update_nag');
function dd_remove_update_nag($value) {
 unset($value->response[ plugin_basename(__FILE__) ]);
 return $value;

10. Displaying Notifications on Dashboard

There are several occasions when you wish to have a feature that displays some notifications or messages on the dashboard. Here is the code for the same:

function showMessage($message, $errormsg = false){
    if ($errormsg) {
        echo '
‘; } else { echo ‘
‘; } echo “$message
“; } function showAdminMessages() { showMessage(“YOUR MESSAGE HERE”, true); if (user_can(‘manage_options’) { showMessage(“Hello admins!”); } } add_action(‘admin_notices’, ‘showAdminMessages’);

These 10 snippets cover a host of facets and functions you can add to your Wordpress website. Do let us know if you find these useful for your website.

About the Author: Amanda Cline is a dedicated PHP developer for hire & she is currently serving as a senior most developer with Xicom Technologies Ltd. You can hire her for successful execution of varied web development projects, both simple as well as complex ones. Get in touch with her via Twitter or Google+.

0 comments:

Post a Comment