15 Essential Tips And Tricks For WordPress

Not every website support footer widgets. So this tweak will help you add multiple footer widgets into your WordPress theme. Read the tutorial here.

2. Customize log in page

Adding a few tweaks on your functions file will let you customize your login page. Here’s what you need to do.
a) In your current theme directory (../wp-content/themes/your-theme-name), add a folder called “login”. Create a CSS file inside the login folder and name it custom-login-styles.css
b) Next, add the following code into your functions.php file

function my_custom_login() {
echo ‘<link rel=”stylesheet” type=”text/css” href=”‘ . get_bloginfo(‘stylesheet_directory’) . ‘/login/custom-login-styles.css” />’;
}
add_action(‘login_head’, ‘my_custom_login’);

Simply customize your CSS file, custom-login-styles.css. This will reflect on the login page.

3. Add infinite scroll WordPress trick

Automatically load new content when the reader scrolls down and approaches the bottom of the page. Actually, infinite scroll is a Jetpack plugin feature. If you’re using a well-coded theme like the default WordPress theme, your theme will support infinite scroll.

Install the Jetpack plugin, enable infinite scroll feature and add the following code to your functions file.

add_theme_support( ‘infinite-scroll’, array(
‘container’ => ‘content’,
‘footer’ => ‘page’,
) );

4. Add customized CSS file

Add a customized CSS file with the name ‘custom.css’ to your theme by adding the following code to your functions file.

function custom_style_sheet() {
wp_enqueue_style( ‘custom-styling’, get_stylesheet_directory_uri() . ‘/custom.css’ );
}
add_action(‘wp_enqueue_scripts’, ‘custom_style_sheet’);

Make sure the new CSS file is located on the same directory that of main CSS file.

5. Display random image header

If you are a person who would love to display random image headers on your blog, this trick is for you.
Name your image 1.jpg, 2.jpg, 3.jpg, and so on. Upload those images to images folder inside your theme directory. Then, paste the following code to the header file.

<img src=”http://Path_to_image_folder/<?php echo(rand(1,10)); ?>.jpg” width=”image_width” height=”image_height” alt=”image_alt_text” />

Make sure you replace the Path_to_image_folder with the actual path.

Insert the below code to single file to show related posts without a plugin.

<?php  //for use in the loop, list 5 post titles related to first tag on current post

  $backup = $post;  // backup the current object
  $tags = wp_get_post_tags($post->ID);
  $tagIDs = array();
  if ($tags) {
    $tagcount = count($tags);
    for ($i = 0; $i < $tagcount; $i++) {
      $tagIDs[$i] = $tags[$i]->term_id;
    }
    $args=array(
      ‘tag__in’ => $tagIDs,
      ‘post__not_in’ => array($post->ID),
      ‘showposts’=>5,
      ‘caller_get_posts’=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <h3><a href=”/<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></h3>
      <?php endwhile;
    } else { ?>
      <h2>No related posts found!</h2>
    <?php }
  }
  $post = $backup;  // copy it back
  wp_reset_query(); // to use the original query again
?>

7. Increase PHP memory

If you were activating a huge plugin and found an error that says memory exhausted just add the following line of code to your wp-config.php file.

define(‘WP_MEMORY_LIMIT’, ’64M’);

The above code will increase the memory limit to 64M, but you can change the value to whatever your hosting server is able to support.

8. Enable shortcodes on widgets

By default, WordPress widgets aren’t enabled to manage shortcodes. Add the following to functions file and empower your widgets to support shortcodes.

define(‘widget_text’, ‘do_shortcode’);

9. Change the length of excerpts

By default, length of the excerpts in WordPress is 55 words. Tweak the functions by adding the following commands to customize the length so it can fit the layout.

function custom_excerpt_length( $length ) {
return 20;
}
add_filter( ‘excerpt_length’, ‘custom_excerpt_length’, 999 );

10. Display most commented posts

Add the following lines of code to enable another from the plenty of WordPress tips which uses hooks and the functions.php file of your theme

function wpb_most_commented_posts() {

ob_start();?>

<ul class=”most-commented”>

<?php

$query = new

WP_Query(‘orderby=comment_count&posts_per_page=10’);

while($query->have_posts()) : $query->the_post(); ?>

<li><a href=”/<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a> <span class=”wpb-comment-count”><?php comments_popup_link(‘No Comments;’, ‘1 Comment’, ‘% Comments’); ?></span></li>

<?php endwhile; ?>

</ul>

<?php// Turn off output buffering

$output = ob_get_clean();

return $output; }

add_shortcode(‘wpb_most_commented’, ‘wpb_most_commented_posts’);

add_filter(‘widget_text’, ‘do_shortcode’);

Then, add this shortcode in a widget wherever you want to display the most commented posts.

[wpb_most_commented]

To show the 5 most popular posts according to the comments count, place the below lines in the sidebar.php file. Of course, if you want to show more or less than 5, just change from 5 to another value you prefer in the $result line.

<h2>Popular Posts</h2>

<ul>

<?php $result = $wpdb->get_results(“SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5”);

foreach ($result as $post) {

setup_postdata($post);

$postid = $post->ID;

$title = $post->post_title;

$commentcount = $post->comment_count;

if ($commentcount != 0) { ?>

    <li><a href=”/<?php echo get_permalink($postid); ?>” title=”< ?php echo $title ?>”>< ?php echo $title ?></a> {<?php echo $commentcount ?>}</li>

<?php } } ?></ul>

‘Featured images’ is one of the most popular features of WordPress. It is supported in most of the themes available today. However, if your theme doesn’t support this feature, you can add support for this feature by tweaking the theme’s functions file

add_theme_support( ‘post-thumbnails’ );

13. Create custom user roles

WordPress provides the following user roles by default- administrator, editor, author, contributor and subscriber. However, at times, you may need to assign some customized user roles.

For example, if you want to provide an option to edit the pages only to a new user, here’s how to do it:

Add the following lines to the functions file. You can change the various functionality availability by setting the appropriate setting to true or false.

// Add a custom user role 

$result = add_role( ‘new’, __( 

‘New’ ),

array( 

‘read’ => true, // true allows this capability

‘edit_posts’ => false, // Allows user to edit their own posts

‘edit_pages’ => true, // Allows user to edit pages

‘edit_others_posts’ => false, // Allows user to edit others posts not just their own

‘create_posts’ => false, // Allows user to create new posts

‘manage_categories’ => false, // Allows user to manage post categories

‘publish_posts’ => false, // Allows the user to publish, otherwise posts stays in draft mode

‘edit_themes’ => false, // false denies this capability. User can’t edit your theme

‘install_plugins’ => false, // User cant add new plugins

‘update_plugin’ => false, // User can’t update any plugins

‘update_core’ => false // user cant perform core updates

);

14. Redirect to custom page after registration

Paste the below lines to the functions to redirect users to a custom page after registration

function __my_registration_redirect(){

    return home_url( ‘/my-page’ );

}

add_filter( ‘registration_redirect’, ‘__my_registration_redirect’ );

15. Add social profile information on the user profile page

By default, the user profile page in the dashboard has fields to add the contact info including AIM, Yahoo IM, Jabber/Google Talk, etc. Open the functions file and add the following snippet to add more social media fields in the user profile page.

function my_new_contactmethods( $contactmethods ) {

// Add Twitter

$contactmethods[‘twitter’] = ‘Twitter’;

//add Facebook

$contactmethods[‘facebook’] = ‘Facebook’;

return $contactmethods;

}

add_filter(‘user_contactmethods’,’my_new_contactmethods’,10,1);

You can use the following code in author.php file to display it.

echo $curauth->twitter;