Currently Browsing: Scripts & Code

Add Facebook Like Button To Your WordPress Site Without a Plugin

No need for a plugin to put the Facebook Like button on your posts. Paste this where you want the button in your single.php or post.php depending on the theme you’re using:

<iframe src="http://www.facebook.com/plugins/like.php?
href=<?php echo urlencode(get_permalink()); ?>&amp;layout=button_count
&amp;show_faces=false&amp;width=450&amp;action=like&amp;
font=lucida+grande&amp;colorscheme=light&amp;height=21"
scrolling="no" frameborder="0" style="border:none; overflow:hidden;
width:450px; height:21px;" allowTransparency="true"></iframe>

There are several settings, so if you want yours to look different than mine, generate the code here.
Then, put in

<?php echo urlencode(get_permalink()); ?>

where the URL is in the code, as I’ve done above.
(I’ve had to include carriage returns in the code above to get it to fit in the space. You might need to take them out when you paste in the code.)

How to replace Read More with an image

Sometimes it’s preferable to have a button instead of text to allow the reader to jump to the rest of a post. Following is the code to replace the text with an image:

 <?php the_content('<img src=
"' . get_bloginfo('template_directory'). '/images/readmore.png"
alt="read more" title="Read more..." />'); ?>

Obviously, replace /images/readmore.png with your image director and file name.

(This is slightly different from the instructions in the WordPress Codex, which did not work for me.)

Enable the WordPress 3.0 Multi-Site Option

There are so many great things about WordPress 3.0 (like the awesome Menus feature for example), and one of the best is that they’ve combined WP and WPMU at long last. You have to turn on the multi-site option by adding a bit of code to the wp-config.php file to enable it:

define('WP_ALLOW_MULTISITE', true);

After adding this, you should see Network under the Tools options in the Admin panel and you can take it from there!

Social media links for WordPress the right way

Inserting a simple link to Twitter is one thing, but including SEO friendly, valid links using WordPress template tags to make it extra convenient for your users to add posts to social media sites requires a little more thought. Fortunately, the linked article does the thinking for us, by supplying code for the most popular social media sites.

Use a shortcode in WordPress template files

Shortcodes are designed to allow you to call certain plugin or built in theme functions within the post/page editor. This code snippet allows you to use shortcodes in a template file as well.

Use different single post templates based on category

Need your single post views to be totally different depending on what category the post is in? You can do this easily by creating special single post files for each category as needed, and then replace the code in your single.php file with the following:

 <?php
if (in_category('jobs')) {
     include(TEMPLATEPATH . '/single1.php');
} elseif (in_categorye('News')) {
    include(TEMPLATEPATH . '/single2.php');
} else {
     include(TEMPLATEPATH . '/single3.php');
}
?>

Obviously if you only have one special category you won’t need the “elseif” statement in the middle.
Check the conditional tags reference in the Codex if you need to control the single post template by something other than category such as tag, post number, etc.

Change storage length of the cookie WordPress sets

WordPress sets a cookie on a user’s computer that remains stored for 10 days after they visit a site. This means they will not have to re-enter their info every time they leave a comment, nor will they have to re-enter the password on password protected pages or posts. Sometimes, however, a site owner might want to require the password each time a visitor wants to view a certain page. Here’s how:

Check out wp-pass.php in your wordpress directory.
You’ll find a line like this:
setcookie(‘wp-postpass_’ . COOKIEHASH, $_POST['post_password'], time() + 2, COOKIEPATH);
The value after the “+” indicates the time in seconds, the cookie stays valid. In this example it is valid for 2 seconds, setting it to 7200 would be equivalent to 2 hours.

This, of course, is a trade off. If there is very active discussion on a site, users may become frustrated at having to re-enter their information each time they want to leave a comment. In this case, it might be better to find another solution for protecting information on a given page.

How to limit WordPress search to certain categories

This is a great tip for limiting the built in search functionality to search only the category you specify. The author provides the code for searching just a single category or searching only the children of a certain category.

Display Feedburner subscriber counts without chicklet

Ever wanted to display your subscriber counts from Feedburner, but didn’t want to use the “chicklet” they provide for that purpose? This PHP script will do the trick!

Script plugin won’t run? Check the location of WP-Head

I don’t know much about JQuery and scripts in general, but I’m learning that the order they’re loaded in the header can make all the difference. If you have a plugin that uses the JQuery library and doesn’t come with it, then the hook for wp_head needs to be below the load of the JQuery library in the header. This is because often plugins use the wp_head function to insert script code into the header.

So, move this bit:

<?php wp_head(); ?>

lower in the header file just above the

</head>

tag.

« Previous Entries