WordPress 2.7 Cookbook 1847197388, 9781847197382

Packt Publishing's WordPress 2.7 Cookbook has been out for while now, but I still thought I'd pick up a copy a

246 12 8MB

English Pages 316 Year 2009

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

WordPress 2.7 Cookbook
 1847197388, 9781847197382

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

WordPress 2.7 Cookbook

Jean-Bapiste Jung

BIRMINGHAM - MUMBAI

WordPress 2.7 Cookbook Copyright © 2009 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: June 2009

Production Reference: 1190609

Published by Packt Publishing Ltd. 32 Lincoln Road Olton Birmingham, B27 6PA, UK. ISBN 978-1-847197-38-2 www.packtpub.com

Cover Image by Vinayak Chittar ([email protected])

Credits Author Jean-Baptiste Jung

Reviewers Alan Doucette

Editorial Team Leader Akshara Aware

Project Team Leader Lata Basantani

Narayan Bhat Paul Thewlis

Project Coordinator Joel Goveya

Acquisition Editor David Barnes

Proofreader Jeff Orloff

Development Editors Amey Kanse Nikhil Bangera

Technical Editors Mehul Shetty Rakesh Shejwal

Indexer Monica Ajmera

Production Coordinator Shantanu Zagade Adline Swetha Jesuthas

Cover Work Copy Editor Leonard D'Silva

Shantanu Zagade

About the Author Jean-Baptiste Jung is a Web developer, Web designer, and blogger born in Paris, France and now living in Wallonia (French-speaking part of Belgium) with his wife and cat. Jean unearthed the World Wide Web in 1998 and started creating web sites three years later. In 2006, while working as a freelance Web developer for a well known French TV channel, Jean started to work with blogs and WordPress. A few months later, he created his irst blog. He became immensely passionate about WordPress and launched a blog dedicated to WordPress hacks, http://www.wprecipes.com/, which quickly managed to become one of the most popular WP-related web sites over the Internet. Meanwhile, Jean is also an author on some prestigious blogs, such as WpHacks, ProBlogDesign, and Smashing Magazine. When he's not blogging or tweaking web sites, Jean enjoys travelling and spending time with his wife and cat. He has a strong love for animals and always stands up to defend animal rights. I'd like to thank my wife Emmanuelle as well as our cute cat for being here with me. They mean so much to me.

About the Reviewers Alan Doucette is a partner of KOI (www.koitech.net), a web development company. He is passionate about PHP and Open Source software. His constantly changing blog can be found at http://alanio.net. Thanks go to the awesome WordPress community for all their daily hard work creaing great Open Source sotware. I would also like to thank Brandi & Jack Lee (www.pandria.com) for their support and dealing with me while I constantly put ime into Open Source projects.

Narayan Bhat is an avid user of Blogger, Twitter, and other useful web applications. He is a top contributor to the Blogger Help Group with more than 50,000 posts to date. Get Blogger tips and tricks and hacks at his blog http://www.blogdoctor.me. He has also worked on the following books: • •

Blogger Beyond the Basics by Lee Jordon. WordPress for Business Bloggers by Paul Thewlis.

Paul Thewlis has worked as a Web communications professional in public and private sectors. He is currently E-Communications Manager for a multinational transport company based in the UK. He began his Web career as a Technical Editor, working on web design books for a well-known publisher. He has extensive experience of many Content Management Systems and blogging platforms. He is an expert in the use of social media within corporate communications, and blogs about that subject, as well as WordPress and the Web in general, at http://blog.paulthewlis.com. He also runs the popular Twitter trivia quiz, Twrivia (http://twrivia.com), and the email reminder service, Urge-Me (http://urge-me.com). Paul is the author of WordPress For Business Bloggers, published by Packt.

Table of Contents Preface Chapter 1: Getting Ready to Cook with WordPress Using built-in WordPress tools Managing media iles with the Media Library Getting ready How to do it Deleting media Bulk media deletion

How it works Live editing themes with the built-in Theme Editor Getting ready How to do it How it works There's more... Editing plugins with the WordPress built-in Plugin Editor Getting ready How to do it How it works There's more... Managing authors and users with the User Manager Getting ready How to do it Deleting users Editing user details

How it works Importing and exporting content with the Import and Export tool Getting ready How to do it Importing content Exporting content

How it works

1 5 6 6 6 7 7 8

9 9 9 10 10 10 11 11 12 12 12 12 12 13 13 13

14 14 14 14 15 17

18

Table of Contents

Chapter 2: Finding, Installing, and Tweaking Themes Installing a theme Getting ready How to do it There's more... Classic themes Advanced themes Premium themes Lists of themes Important notes about themes

Chapter 3: Get the most out of your WordPress Theme Modifying your theme colors Getting ready How to do it How it works There's more... Modifying your theme fonts Getting ready How to do it There's more... Tips and things to know about fonts

Creating and integrating a favicon Getting ready Favicon format

How to do it How it works Integrating your own logo Getting ready How to do it How it works There's more... Adding a link to the homepage

Adding social bookmarking buttons to your theme Getting ready How to do it How it works There's more... Using CSS to style the social bookmarking widget Adding Del.icio.us live count Code explanation

Integrating Feedburner feeds on your theme How to do it How it works ii

19 20 20 20 22 22 28 34 40 41

43 44 44 45 45 46 46 46 47 47 47

48 48 48

48 49 49 50 50 51 51 52

52 52 52 53 53 53 54 55

55 56 57

Table of Contents

Integrating Twitter on your theme using the Twitter Tools plugin Getting ready How to do it How it works Displaying your Twitter entries on your blog, using a page template Getting ready How to do it How it works Customizing WordPress admin login page Plugin versus hack How to do it How it works Using conditional tags to display content on speciic pages Getting ready How to do it How it works? There's more...

57 58 59 59 59 59 60 62 62 62 63 64 64 64 64 66 66

Special parameters

67

Using page templates in your theme Getting ready How to do it How it works Creating an archive page Getting ready How to do it How it works There's more... Creating a custom 404 error page Getting ready How to do it How it works There's more... Using a static page as a homepage Getting ready How to do it How it works There's more... Creating a Featured Posts block on your homepage Getting ready How to do it

67 68 68 69 69 69 69 70 71 71 71 72 72 73 73 73 73 74 74 74 75 75

iii

Table of Contents

How it works There's more... Making your new posts stands out with a custom style Getting ready How to do it

How it works There's more...

Chapter 4: Doing anything with Plugins and Widgets Installing plugins Getting ready How to do it How it works Getting rid of comment spams with Akismet Getting ready How to do it How it works Backing up your database with WP Database Backup Getting ready How to do it How it works There's more... Scheduling automatic backups

How it works There's more… Optimizing your blog performances with WP Super Cache Getting ready How to do it How it works There's more... Prevent URLs from being cached Adding redirects for changed Permalinks Getting ready How to do it How it works There's more... Using redirects for afiliate marketing and cloaking

Get more comments with the Subscribe to Comments plugin How to do it How it works

iv

76 76 76 77 77

78 79

81 82 82 82 83 83 83 83 84 84 84 84 85 85 85

85 85 86 86 86 87 87 87 88 88 88 89 89 89

89 89 90

Table of Contents

There's more... Accessing real time statistics with WordPress.com Stats Getting ready How to do it How it works Monetizing your blog with ISIS Ads Management Getting ready How to do it How it works Extending WordPress search with Search Unleashed Getting ready How to do it How it works Installing widgets Plugins versus widgets Getting ready How to do it How it works There's more...

90 90 91 91 91 92 92 92 93 93 93 93 94 95 95 95 96 96 96

Installing downloaded widgets

Make your sidebar widget-ready Getting ready How to do it How it works Creating two (or more) different widget-ready zones Getting ready How to do it How it works There's more... Modifying core widgets Getting ready How to do it How it works Creating your own widget Getting ready How to do it How it works There's more...

97

97 97 97 98 98 99 99 100 100 101 101 101 102 102 102 102 103 104

Complete widget code

104

v

Table of Contents

Chapter 5: Displaying Posts Getting posts within the WordPress loop Getting ready How to do it How it works Retrieving posts from a particular category only Getting ready How to do it How it works Getting an exact number of posts Getting ready How to do it How it works Retrieving posts by date Getting ready How to do it How it works Getting posts published today Getting ready How to do it How it works Getting posts published exactly a year ago Getting ready How to do it How it works Using two different loops without duplicate posts How to do it How it works Complete code Accessing post data outside of the WordPress loop Getting ready How to do it How it works Usage

Accessing permalinks outside the loop Getting ready How to do it How it works There's more... Using the $post global variable

vi

107 108 108 108 108 108 109 110 110 110 110 110 111 111 111 111 112 112 112 112 113 113 113 113 114 114 115 115 116 116 116 116 117 118

118 118 118 119 119 119

Table of Contents

Displaying any RSS feed on your blog Getting ready How to do it How it works There's more... Displaying thumbnails on your blog homepage Getting ready How to do it How it works There's more... Deining a default image

Alternating background color on post list Getting ready How to do it How it works Displaying posts in two columns Getting ready How to do it How it works Save time by using WordPress shortcodes Getting ready How to do it How it works There's more... Creating a nice download message box using a shortcode Getting ready How to do it How it works

Chapter 6: Managing and Enhancing Multi-Author Blogs Creating an author page template Getting ready How to do it How it works There's more... Displaying a custom login form in your blog's sidebar Getting ready How to do it How it works Adding a control panel to your blog's sidebar Getting ready

110 120 120 120 120 121 121 121 122 123 123

123 124 124 124 125 125 125 126 126 126 127 127 128 128 129 129 130

131 132 132 132 133 134 134 135 135 136 137 137 vii

Table of Contents

How to do it How it works There's more... Adding a login form and a control panel

Coniguring author roles Getting ready How to do it How it works There's more... Controlling what authors can do

Displaying author-related information on posts Getting ready How to do it How it works There's more... Displaying author picture on posts Getting ready How to do it How it works There's more... Displaying the author's gravatar picture on posts Getting ready How to do it How it works Adding moderation buttons to the comments Getting ready How to do it How it works Getting notiied when a new draft is saved Getting ready How to do it How it works Allowing multiple authors on posts Getting ready How to do it How it works Displaying a list of all of the authors Getting ready How to do it How it works

viii

137 138 138 139

141 141 141 142 142 142

145 145 146 146 147 147 147 148 148 148 149 149 151 151 151 152 152 153 153 153 154 154 154 154 155 155 156 156 156 156

Table of Contents

There's more...

156

Controlling the wp_list_authors() function

Chapter 7: Securing your WordPress Blog Creating manual backups of your WordPress blog Getting ready How to do it How it works There's more... Restoring a MySQL backup Getting ready How to do it How it works Creating backups of your WordPress iles Getting ready How to do it How it works Using a shell script to create automatic iles and database backups Getting ready How to do it How it works Securing your plugins directory Getting ready How to do it How it works Removing a WordPress version from the theme iles Getting ready How to do it How it works Getting rid of the Administrator account Getting ready How to do it How it works Automatically forbid login after some failed login attempt Getting ready How to do it How it works Protecting the wp-admin directory brute force with the help of AskApache Getting ready How to do it How it works

157

159 160 160 160 161 161 161 162 162 163 163 163 164 164 164 164 164 165 165 165 166 166 166 167 167 167 168 168 168 169 169 169 169 170 171 171 171 173 ix

Table of Contents

Restricting wp-admin directory to your IP address Getting ready How to do it How it works There's more... Allowing access to more than one IP

Testing your blog security Getting ready How to do it How it works There's more... Deny comment posting on no referrer requests Getting ready How to do it The .htaccess method The PHP method

How it works

Chapter 8: SEO Tips and Tricks to Get More Visits Optimizing your permalinks for SEO Getting ready How to do it How it works There's more... Structure tags reference

Further optimizing your permalinks Migrating your permalinks safely Getting ready How to do it How it works Optimize your title tag for SEO Getting ready How to do it How it works Create Meta descriptions for your posts Getting ready How to do it How it works There's more... A more sophisticated code by using custom ields

Avoid duplicate content with a robot.txt ile Getting ready x

174 174 174 175 175 175

176 176 176 178 179 179 179 180 180 180

180

181 182 182 182 183 183 184

184 185 186 186 186 187 187 188 189 189 189 190 190 190 190

191 191

Table of Contents

How to do it How it works There's more...

191 192 192

PHP code to avoid duplicate content

Adding a sitemap to your blog Getting ready

192

193 193

How sitemaps works General rules for creating sitemaps

193 194

How to do it How it works

195 196

Basic options

196

Using Google Webmaster Tools How to do it How it works There's more...

197 197 198 198

Overview Statistics Links Sitemaps Tools

Pinging third party services Getting ready How to do it How it works There's more...

199 199 199 200 200

200 200 201 202 202

Services to ping

Enhancing your WordPress blog SEO with the All in One SEO Pack plugin Getting ready How to do it There's more...

202

203 203 204 206

Five more tips for a better SEO

Chapter 9: Making Money with WordPress Integrating Adsense to your WordPress blog Getting ready How to do it

206

209 210 210 211

Method 1: Using your Adsense code in your theme iles Method 2: Using a text widget to display your Adsense ads

How it works Display ads anywhere in your posts by using WordPress shortcodes Getting ready How to do it How it works

211 212

212 213 213 213 214

xi

Table of Contents

There's more... Inserting shortcodes into sidebar widgets

Displaying Adsense ads to search engines visitors only Getting ready How to do it How it works There's more... Managing who Sees Ads Getting ready How to do it How it works There's more... Advanced conditions

Inserting ads in your RSS feeds Getting ready How to do it Option 1: Using a hack Option 2: Using a plugin

How it works There's more... Solving problems with Feedburner

Redirecting your WordPress RSS feeds to Feedburner Getting ready How to do it Option 1: Using a hack

How it works Option 2: Using a plugin

How it works There's more... Plugin versus hack

Show your blog stats to ind advertisers Getting ready How to do it How it works There's more... Managing your 125*125 px ad spots

Enhancing your Advertise page by adding Paypal subscriptions Getting ready How to do it There's more... Telling advertisers how to unsubscribe

xii

214 215

215 216 216 217 217 218 218 219 220 220 221

221 222 222 222 223

224 224 224

225 225 225 226

226 226

227 227 227

228 228 228 230 230 230

230 231 231 233 233

Table of Contents

Chapter 10: Enhancing User Experience Replacing the Next and Previous links by a paginator Getting ready How to do it How it works Highlighting searched text in search results Getting ready How to do it How it works Using the CSS sliding doors technique within WordPress Getting ready How to do it Applying this hack to pages Applying this hack to categories

How it works Creating a drop-down menu for your categories Getting ready How to do it Step 1: PHP and HTML Step 2: The CSS Step 3: Optional JavaScript

How it works There's more... Creating a horizontal drop-down menu

235 236 236 236 238 238 238 238 239 239 239 241 241 242

243 243 243 244 244 245 246

247 247 247

Adding a breadcrumb to your theme Getting ready How to do it How it works There's more...

250 250 250 251 252

Using a hack to display breadcrumbs

252

Displaying related posts Getting ready How to do it How it works Display tabs on your sidebar Getting ready How to do it How it works

253 253 254 255 255 256 256 256

xiii

Table of Contents

Chapter 11: Make your Blog Stand Out Rewarding your commentators to get more comments Making your blog dofollow with the NoFollow Free plugin The NoFollow Free plugin

257 257 258 258 258

Using the Top Commentators widget

260

Installing the Top Commentators widget Conclusion

260 262

Adding a print stylesheet to your blog Getting ready How to do it How it works

262 263 263 264

Print button Print stylesheet inclusion and selection Print stylesheet

264 264 264

Using WordPress as a photoblog Photoblog themes for WordPress Photoblog Nishita Fotolog

Creating your own Photoblog theme Getting ready How to do it How it works Creating an iPhone-friendly version of your blog Getting ready How to do it How it works There's more... Using an header image Modify the title and slogan

Integrating a forum in your WordPress blog Getting ready How to do it How it works There's more... Skins Moderators User groups

Index

xiv

266 266 266 267 268

268 268 269 272 272 272 273 273 273 274 274

274 275 276 277 277 277 277 278

279

Preface About 120,000 blogs are created every day. Most of them quickly die, but a few stay, grow up, and then become well known and respected places on the Web. If you are seriously interested in being in the top league, you will need to learn all the tricks of the trade. WordPress 2.7 Cookbook focuses on providing solutions to common WordPress problems, to help make sure that your blog will be one of the ones that stay. The author's experience with WordPress enables him to share insights on using WordPress effectively, in a clear and friendly way, giving practical hands-on solutions to WordPress problems, questions, and common tasks—from themes to widgets and from SEO to security. Are you feeling limited with WordPress, or are you wondering how popular blogs do a certain kind of thing that you can't? With this cookbook, you will learn many WordPress secrets and techniques with step-by-step, useful recipes dedicated to achieving a particular goal or solving a particular problem. You'll learn the secret of expensive premium themes, how to optimize your blog for SEO and online proits, and how to supercharge WordPress with killer functions used by the most popular blogs over the Internet.

What this book covers Chapter 1 introduces you to WordPress. It introduces you to some basic—but often forgotten—built-in tools to make your blogger life easier. Chapter 2 discusses the various WordPress themes and provides you with the location where to ind professional—but free—themes. It also teaches you how to install and customize these themes. Chapter 3 teaches you how to customize any existing WordPress theme and make it it your taste and need.

Preface Chapter 4 describes an easy procedure to install plugins. It shows what different plugins can do for you in particular situations. It also teaches you how to download and install widgets and how to make a WordPress theme widget-ready. Chapter 5 describes the procedure to display posts and to retrieve post information from WordPress. Chapter 6 teaches you to manage a multi-author blog and integrate powerful functions for creating an author page template. Chapter 7 educates you about hacks, plugins, and tips and tricks to secure your database and your WordPress blog. Chapter 8 teaches you—by providing you with tips and tricks—the art of getting trafic from search engines to your blog. Chapter 9 discusses the monetization solution that can be used in a WordPress blog. It will also provide you with many tips and tricks to make money while blogging. Chapter 10 helps you make your blog easy and functional for your visitors. Chapter 11 provides you with tips and hacks to make your blog better than your competitor's blog.

What you need for this book You'll need a working installation of WordPress on a server, or on your local machine (using MAMP, WampServer, EasyPHP, and so on). WordPress can be downloaded from the link, http://wordpress.org/download/. You need to have minimal knowledge of XHTML and CSS, PHP and JavaScript. You also need an FTP client for uploading iles. I recommend Cyberduck (http://cyberduck.ch/) for Mac and Filezilla (http://filezilla-project.org/) for both GNU/Linux and Windows platforms.

Who this book is for This book is for anyone who wants to enhance their WordPress blog to make it more engaging and feature-rich. It is not speciically for developers or programmers, rather it can be used by anyone who wants to get more out of their WordPress blog by following step-by-step instructions. A basic knowledge of PHP, XHTML, CSS, and WordPress is desirable, but not necessary.



Preface

Conventions In this book, you will ind a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning. Code words in text are shown as follows: “We can include other contexts through the use of the include directive." A block of code is set as follows: