Applying Author Gravatar In Post

Posted by Nile | Posted in Tutorials | Posted on 11-24-2009 | 2 Comments

Tags:

A few years ago, Gravatar (globally recognized avatar) was new and shiny, and people were implementing it into their blogs. Gravatar has since become easily integrable and users can simply put in their themes comment.php file:


<?php echo get_avatar( $comment, 32 ); ?>

Where 32 would designate the pixel size of the image that the blog owner wants to allow to be seen on their site.

However, what about those wanting to call the Gravatar to the author of a post? That can of course be done.

What yoy want to do is retrieve the blog author’s email address and get the avatar if the author has a Gravatar account. You can apply this to your index.php, single.php, archive.php where you want the author’s avatar to show on the post.


<?php
$author_email = get_the_author_email();
echo get_avatar($author_email, '32');
?>

Again, 32 is the pixel size to display the avatar. You can adjust this number to what size you wish.

You can see more documentation in the WordPress Codex about Using Gravatars. If you do not have a Gravatar account, you should really consider getting one. It is free and much better than the default mystery man that shows up in the comments if a person has no account with Gravatar.

Your Own ‘Tweet This’

Posted by Nile | Posted in Tutorials | Posted on 11-22-2009 | Comments Off

Tags:

Personally, I think the Tweet This plugin while ingenious, is a bit annoying as it limits you where you want to put it. Instead of going through the plugin jargon code, you can put your own code. Really, all you are doing is telling Twitter your update with the link of the post. This is in case you prefer to have your own bookmark links, rather than a plugin do it for you..

Step 1. Go to your single.php and place the_permalink php code for WordPress where you wish it to be seen.


<a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Tweet This!" target="_blank">Tweet This</a>

You can also dress this up and put an image


<a href="http://twitter.com/home?status=Currently reading <?php the_permalink(); ?>" title="Tweet This!" target="_blank"><img src="YOURIMAGEURLHERE" alt="Tweet This!" /></a>

Display First Full Entry And Then Following Entries As Excerpts

Posted by Nile | Posted in Tutorials | Posted on 11-20-2009 | 7 Comments

Tags:

I like to use this code to display the first full entry and then the rest of my entries as excerpts from my main page. I have used this many times, even on my other site Blondish.net. This is not hard to do and only requires that you replace a few code snippets. You can apply this to your main index template.

Look for the following code in the loop in your:


<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Replace with the following.

<?php if (have_posts()) : ?>
<?php $postcount = 0; // Initialize the post counter ?>
<?php while (have_posts()) : the_post(); //start the loop ?>
<?php $postcount++; //add 1 to the post counter ?>

This code allows your blog to see how many posts you have.

2. Look for the following code. You are going to be replacing the_content


<?php the_content('Read the rest of this entry &raquo;'); ?>

with:


<?php if ($postcount == 1) : // if this is the first post ?>
<?php the_content('Read the rest of this article ->'); //Show the full post ?>
<?php else : //if this is NOT the first post ?>
<?php the_excerpt(); ?>
<?php endif; //end of the check for first post - other posts?>

The code basically tells your blog to show the first as the full entry and the following posts as excerpts.

This snippet is seen in action here at WP Addict.

Disable Comments For Older Posts

Posted by Nile | Posted in Tutorials | Posted on 11-19-2009 | Comments Off

Tags:

It is really annoying to get spam in posts so old you almost forgot about them. Here is a tutorial to help you with close your comments using the close comments function in version WordPress older than 2.7. This will help bring down the number of spammers who like to slip in comments on older posts.

In WordPress 2.7 and above, You can change this by going to your Admin Panel under Settings> Discussion. There is a section on “Other Comment settings”. You will be able to set how many day you want your posts open under “Automatically close comments on articles older than X days.”

Step 1. Open your close_comments statement with an if statement saying that you want to apply the function to your posts.
<?php
function close_comments( $posts ) {
if ( !is_single() ) { return $posts; }

Step 2. You want specify if the post is written a certain time, you want it to close in so many days

if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
}

Step 3. You want to close the function, but this is not just a function, but a filter, so you must apply it as a filter to work.


return $posts;
}
add_filter( 'the_posts', 'close_comments' );
?>

How To Make A Tag Cloud Page In WordPress

Posted by Nile | Posted in Tutorials | Posted on 11-17-2009 | 1 Comment

Tags:

Some people really would like to have a tag page. Well, on this tutorial, you will be able to add a tag cloud to a page without making a template or even using a plugin.

You can use the template where you create a page and can select it, but perhaps you want to add it to another page, like the 404 page.

Step 1. Create a New Page.

Step 2. Paste the following code in where you put your content. Make sure you are in the HTML format instead of Rich Text. The ‘number=0′ means that you want all of the tags to show. Note: You can also create a new page, and have the following code within a template and choose that template format.


<?php wp_tag_cloud('number=0'); ?>

You can add header tags (h2 or h3, whichever you use within your content) before it, or you can do without the header tags as the page will already be defined with its own when you publish it.

Very simple, right?

How to Secure Your WordPress Blog

Posted by Nile | Posted in Tutorials | Posted on 11-16-2009 | 1 Comment

Tags:

Securing or as some have dubbed ‘hardening’ your WordPress is important. Why? Well, it allows you to keep out hackers or if you have guest writers, you can make sure they do not take over your blog and boot you out. Also, it could help you with spammers. If you are reading this article and doing a first-time installation (for a self-hosted blog, meaning you have an account with a webhost and your own domain), make sure that your webhost is up-to-date on things like PHP, MySQL, Apache. If your webhost uses cPanel, then make sure that it is also up-to-date. Most webhosts will update if you ask. The up-to-date concept also extends to your WordPress. If you are not using the most current version, as well as the most current plugin versions for your WordPress site, you could be risking your site’s security. According to Matt Mullenweg during WordCamp Chicago 2009, keeping your WordPress up-to-date is the first and most important thing to do. (paraphrasing)

Here are some steps in creating a more secure WordPress.

1. Make a dynamic password. It is best to not use an easy or common password. Sometimes with numbers and letters (both capital and lowercase) mixed together are great. It is best to write these down in a special ‘book’ of login information. Even if it might be convenient to put something you will remember, there is also those times when you may not feel well and not be able to recall your password.

2. Use level privileges to specify your guest writers. Even if you think they are nice and trustworthy, sometimes the strangest things may occur like some falling out and if you do not set the user level, you will risk the integrity of the blog.

3. Use Askimet to help flush out the spam. Also, in your WordPress settings, you can set IPs to deny, words, and such to filter out spammers.

4. Use your .htaccess file to restrict access to certain areas like wp-admin, wp-content, wp-includes, and single files like wp-config.php.

In order to do this, you can write a new .htaccess file. A lot of times with automatic installers like Fantastico Deluxe, they will generate a htaccess file for you. If you are using permalinks, you will have to keep that information in your htaccess file, so if you go to your WordPress under Manage>Files>.htaccess, you can get the code to retain your permalink structure. However here is a simple one that you can put together. It protects your .htaccess file, your wp-config.php file, your directories, and a little snippet to protect yourself from spammers. Just copy and past the lines into your .htaccess file.

    Step 1. Protect Your .htaccess file itself


    order allow,deny
    deny from all

    Step 2. Protect wp-config.php


    order allow,deny
    deny from all

    Step 3. Disable directory browsing

    Options All -Indexes

    Step 4. Protect from spammers

    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} POST
    RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
    RewriteCond %{HTTP_REFERER} !.*yourdomain.com.* [OR]
    RewriteCond %{HTTP_USER_AGENT} ^$
    RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]

When you have done this, it is best when implementing anything to test your website to make sure these commands are working properly.

Do you have any extra suggestions that should be added?

Two Ways to Retrieve Your WordPress Password

Posted by Nile | Posted in Tutorials | Posted on 11-15-2009 | 1 Comment

Tags:

Sometimes you forget. You might find it annoying, but when there are so many things to do and remember on a daily basis, some things slide through your mind and out your ear, like perhaps your password to log into you admin panel for WordPress. This article will explain three ways for you to be able to retrieve your password, especially if you have a self-hosted WordPress blog.

There are other questionable ways, but until those can be worked out with the most current version of WordPress, I will not list them. As a sidenote, you should try to put your admin username and password in a safe place. I recommend keeping some type of small ‘book’ where you keep your login information.

1. When you set up your blog either manually or with Cpanel Fantastico or other auto-install programs, you told the script an email address and a password to log into your admin panel. On the login page (example: http://yourblog.com/wp-login.php), there is a link to retrieve the password. You simple enter your email address and it will send the password to you.

2. You can reset your password through your MySQL’s phpMyAdmin. Take caution as these directions might differ depending on your server’s installation of MySQL and PHP. As a note, you can ask your host if they are using the most current version available of MySQL and PHP on your server even if you are a shared client. If they are not, you should be able to request them to upgrade.

  • Go to wp_users and click ‘browse.’
  • On the user_pass field, click ‘browse’ and find the ID number associated with your login.
  • Once you find the ID number(should be only one), click ‘Edit.’
  • Delete the password content.
  • Type your new password.
  • Select ‘MD5′ in the dropdown menu.
  • Click Go or Save at the bottom of the screen.
  • Go to WordPress blog tab and test the new password. Just in case this did not work, start the process over and try again.

You can go to the WordPress Codex under Resetting Your Password to learn more ways and some I did not include here.

Edit This Entry Template Tag

Posted by Nile | Posted in Tutorials | Posted on 11-12-2009 | 1 Comment

Tags:

The ‘edit this entry’ template tag, or edit_post_link gives the blog owner and any other users with editing privileges to have a quick link to click on that will allow the user to edit an article. Users who do not have this capability, like your readers who are not registered members will not be able to see this link as it is hidden from them.

The code is put within the Loop.


<?php edit_post_link('Edit this entry.', '<br /><p>', '</p>'); ?>

Some users find this handy, while others do not. For those who have a theme that has the ‘Edit this entry’ and do not want it, go to the single.php or the page.php and look for the above coding. Simply remove it and it is gone.

How to Change The Default Avatar

Posted by Nile | Posted in Tutorials | Posted on 11-07-2009 | Comments Off

Tags:

Sometimes that default Gravatar with the gray and white seems boring. Lucky for you, instead of altering internal WordPres files, you can harness the power of adding a filter to your theme’s functions.php to change your default image to display.

1. Open your template’s functions.php

2. Insert the following code:

add_filter( 'avatar_defaults', 'newgravatar' );
function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo('template_directory') . '/images/yournewgravatar.jpg';
$avatar_defaults[$myavatar] = "Your New Gravatar";
return $avatar_defaults;
}

3. Change where younewgravatar.jpg to the image you want and change the following text, Your New Gravatar to the name you wish to have your new default avatar be called.

Editing The Number Of Words For The Excerpt

Posted by Nile | Posted in Tutorials | Posted on 11-06-2009 | Comments Off

Tags:

Some people might think it is the number of lines you have to define to edit how long or short your excerpt in your WordPress article. It is not. It is defined by how many words. WordPress by default picks the first 55 words as your excerpt. However, if you wish to edit to make it shorter or longer:

EDIT: Please note, this way changes the core files and upgrades will only change it back. (Thanks Mike Little!)

1. Go to your wp-includes folder and find the formatting.php file

2. Look for the group of code like

/**
* Generates an excerpt from the content, if needed.
*
* The excerpt word amount will be 55 words and if the amount is greater than
* that, then the string '[...]' will be appended to the excerpt. If the string
* is less than 55 words, then the content will be returned as is.
*
* @since 1.5.0
*
* @param string $text The exerpt. If set to empty an excerpt is generated.
* @return string The excerpt.
*/
function wp_trim_excerpt($text) {
if ( '' == $text ) {
$text = get_the_content('');
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', 55);
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '[...]');
$text = implode(' ', $words);
}
}
return $text;
}

3. If you see $excerpt_length = apply_filters(‘excerpt_length’, 55); change the text length which by default is 55 to the number of words you prefer.

—–

Mike Little suggests a better way with a lot less code and hassle involved! :)

Apply a filter! Alright the above code hints to you that you are applying a filter, so instead, you can leave the core files and formatting.php file alone.

Instead:

1. Go to your functions.php of your theme template and add the following


add_filter('excerpt_length', 'my_excerpt_length');
function my_excerpt_length($len) { return 75; }

Now your excerpts will be the length you want!