Here are some wordpress tricks and codes which will enhance your blog. These code snippets are ready to use, just paste them in your functions.php and enjoy!

1-Add Favicon to WordPress Site

This will add Favicon to your site. The image named ‘favicon.ico’ must be present in the current active theme images folder. This code will replace the wordpress logo which you see at www.yourwebsite.com/wp-admin

function  ax_favicon() {

echo '<link rel="shortcut icon" type="image/x-icon" href="'.get_bloginfo('template_directory').'/images/favicon.ico">';
}
add_action('wp_head','ax_favicon');

2-Redirecting Users to Specific URLs

Sometimes we don’t want the users with a certain role to access back panel, in that case we can redirect the users to any specific url we want. This is especially useful when you have to implement front-end posting for wordpress.

function ax_redirect_user_role()
{
//retrieve current user info
global $current_user;
get_currentuserinfo();
//If login user role is Subscriber
else if ($current_user->user_level == 0)
{
wp_redirect( home_url() ); exit;
}
//If login user role is Contributor
else if ($current_user->user_level > 1)
{
wp_redirect( home_url() ); exit;
}
//If login user role is Editor
else if ($current_user->user_level >8)
{
wp_redirect( home_url() ); exit;
}
// For other rolse
else
{
$redirect_to = 'http://yahoo.com/';
return $redirect_to;
}
}
add_action('admin_init','ax_redirect_user_role');

Read about Wp Roles and levels here

3-Replace WordPress Logo on Login Page with Custom Logo

On the login screen you can have your own custom logo instead of wordpress logo. Just paste this snippet in your functions.php. You must have your logo named custom_logo.png in your theme’s images folder.

function ax_custom_login_logo() {

echo '<style type="text/css">

h1 a { background-image:url('.get_bloginfo('template_directory').'/images/custom_logo.png) !important; }

</style>';

}

add_action('login_head', 'ax_custom_login_logo'); ?>

 

 

4-Send an Email To Contributors When their Post is Published.

When you have a multi-author site, where multiple writers are posting stuff that is held for approval by the admin, This snippet can be used to send email to the contributor whose post is published.


function ax_Notification($post_id) {
$post = get_post($post_id);
$author = get_userdata($post->post_author);

$message = "
Hello ".$author->display_name.",
Your post, ".$post->post_title." has just been published!";

wp_mail($author->user_email, "Your article is online", $message);
}
add_action('publish_post', 'ax_Notification');

 5-Top Commenter’s Name

The last but not the least is to display the name of author who has the maximum number of comments.

Code for functions.php

function ax_top_comment_authors($amount = 5) {
global $wpdb;
$results = $wpdb->get_results('
SELECT
COUNT(comment_author_email) AS comments_count, comment_author_email, comment_author, comment_author_url
FROM '.$wpdb->comments.'
WHERE comment_author_email != "" AND comment_type = "" AND comment_approved = 1
GROUP BY comment_author_email
ORDER BY comments_count DESC, comment_author ASC
LIMIT '.$amount
);
$output = "<ul>";
foreach($results as $result) {
$output .= "<li>".$result->comment_author."</li>";
}
$output .= "</ul>";
echo $output;
}

code for template file where you want to display the commenter’s name use the code below

?>php ax_top_comment_authors(); ?>

source:http://wpsnipp.com/index.php/comment/display-user-with-the-most-comments/

Yumna Tatheer

I am a Computer Science graduate and a part time blogger from Lahore, Pakistan. I am a PHP programmer and a wordpress expert.

More Posts

Follow Me:
Facebook