Category Archives: wordpress

Fast page insert in wordpress

You want to create a lot of pages in wordpress using a script to populate a blog with content you might have. To make sure your script will be compatible with future versions of wordpress you want to use wp_insert_post

The Problem

The more pages you add the slower your script will be. With about 700 pages my script took 12 seconds to add a new page.
After digging through the code I found out that wp_insert_code calls $wp_rewrite->flush_rules() every time a new post or page is inserted and that this is what takes the most time to finish.
Now it makes sense, the more pages you have the more rules ( permalinks ) you have and more time it will take to finish that function call.

The Solution

The call to $wp_rewrite->flush_rules() can be disabled by defining WP_IMPORTING. Now inserting a post takes just a second or less. But you still have to call $wp_rewrite->flush_rules() after you're done inserting all posts. This call will take quite a lot depending on the total number of posts/pages you have but it's a lot better to call it only once then a few hundreds or thousand times.

The way wordpress updates it's rules needs to change. Even if we can solve the bulk import problem by calling flush_rules at the end , we end up with a blog with thousands of pages where trying to publish a new post manually might take 30 or more seconds.

MYC4 bid Importer plugin

MYC4 is a micro credit platform that enables anyone to invest in small businesses and entrepreneurs in poor countries in Africa. MYC4 enables you to help poor people in Africa ( people that can't get a loan from banks ) get a loan to improve their business.

This wordpress plugin will fetch the details of the businesses in which you invest and create posts on your blog from those details.

By using this plugin you can create exposure for those businesses, enabling them to raise the funds they need more quickly and thus to ensure a shorter way to success.

The plugin was developed after an idea by prof. David Costa of Robert Kennedy College. He runs a blog about microfinance and teaches microfinance as part of the investment management course at Robert Kennedy College.

Download the plugin

WP-Twitter-ID Social Profiles Integration

A few days ago I wrote a post about a wordpress plugin that will add a twitter id field to the comments form and I was reporint two problems with it. One was solved in that post and for the second one I'm going to show you a solution in this post.

The Problem

The second problem was that the twitter id field was show assigned to a comment when a registered user was commenting.

Basically I needed a way to specify the twitter id of a registered user in the profile and then just make the WP-twitip-id plugin use that value form the profile. I could have done this by implementing the necessary code to show the field on the profile page and save it's value when the profile was saved but I was lazy and I wanted a quicker solution.

The Quick Solution

The quick solution was to install the Social Profiles plugin, set my twitter id in my profile and then do a simple modification to wp-twitip-id.

To make the modification just open wp-twitip-id.php in any editor, look for "function addtwitterfieldmeta($comment_data){" and right after that line insert the following code:

  1.  

That's it. Simple don't you think ? Now next time you post a comment as a registered user you should see your twitter id in the comment header.

Download

Here's the zip file with all modifications including the one in the previous post.

[download#1]

Twitter id comment field

I saw this post on problogger about how Darren implemented a new field in the comment form for his new blog about twitter tips powered by wordpress. Basically he used a plugin for adding and extra field to the comment form and then modified the comment template to show the link to the twitter profile page.

There are two problems with this approach. #1 Unlike the author name, email and web site, the twitter id field is not saved in a cookie so your comentators will have to fill it in every time they post a comment, this migh be annoying for some of them. the second problem is that if you're logged in and want to reply to a comment there's no way you can set the twitter id.

The first problem was almost solved by a new plugin named wp-twitip-id by Andy Baily specially created for setting up the twitter id field. I said almost solved because this plugin uses javascript to add the new field and in some cases ( depending on the template you are using ) javascript would not be the best way to do this.

Luckily you can use this plugin without javascript and add the new field manually by modifying the comments.php template file but then the cookie would not work anymore so it needs some modification to set the cookie from the php script instead of javascript .

Simply calling setcookie before the comment is saved will do the work, then when you display the form field just use $_COOKIE['atf_cookie'] to set the value of the twitter field.

If you like to modify some plugin code get the wp-twitip-id plugin, open wp-twitip-id.php look for addtwitterfieldmeta function and add

  1.  

right after the line with this code: 'if($twitter){'

Then go into wp-content/themes/<yout_theme>/ edit comments.php  look for the URL field and add a new field after it like this:

<input name="atf_twitter_id" type="text" value="<?php echo $_COOKIE['atf_cookie']?>" />

You might have to add some other html code and css to make the field look like the others but that depends on the theme you are using.

For convenience I have attached the modified plugin but you still have to modify the comments.php file in your theme.
wp-twitip-id-091

Let me know how this works for you. Do you think the twitter id comment field is worth the effort ? Does it bring value to you it as a commentator?

wordpress 2.5.1

I have just upgraded to wordpress 2.5.1. My upgrade routine worked well without any problem.

The new release seems to bring a few bug fixes to  some annoying issues and a security fix as well as some performance enhancements.

Unfortunately the automatic plugin updater still fails at times and doesn't save the ftp password and the media uploader still renames .tar.gz files in a stupid way.

Upgraded wordpress to 2.5

I have finally upgraded wordpress to 2.5.

The old version was 2.3.1 ( yeah that old ) and the upgrade was pretty smooth.  I didn't use plugins like instant upgrade for this upgrade but I'm thinking of using for the next upgrades.

How did I do it?

I have my own method for this. It's very similar to what they descibe in the official upgrade docs but a bit modified.

Here are the 7 steps I followed:

  1. backup the database using the backup plugin
  2. remember ( write a list of) active plugins then deactivate all ( so if any plugin will not work in the new version you can still access the blog and the admin interface )
  3. make a backup of all files ( assuming current install is in public_html ):
    1.  
  4. download and extract the new version ( it will create a wordpress folder so make sure you don't already have one cause it will overwrite the files in it ) :
    1.  
  5. use rsync to copy the new files over the old files ( note the exclusion of wp-content to preserve the modifications you might have done to themes and plugins ) :
    1.  
  6. call the upgrade.php script, go to : http://your_own_blog.tld/upgrade.php
  7. reactivate the plugins you had active before the upgrade

The whole thing took about 10 minutes and there were no problems encountered except for a few plugins that could not be reactivated because they were incompatible with the new version.

What I like about it?

I like the new interface,it seems friendlier, faster, more slick and  I was getting  bored of the old one.

I don't care much about the dashboard, but I love the way the WYSIWYG editor works.

I think the feature that I love the most is the automatic plugin upgrade but this is also the one that I think it needs more work ( of course there may be others that I did not notice yet )

what id didn't like?

Here are three problems I found with the automatic plugin upgrade:

  1. the first time I tired it id didn't seem to care that I selected "No" for the "Use SSL" option and it just told me it could not connect.
    This was because my ftp server did not have ssl enabled but it was impossible to know this was the cause because it was clear to me from the web interface that it was not using SSL until I went in the code and just run a print_r on the $credentials variable .
    I'm not sure why this happened, I guess a bug. I had to go into the database and manually edit the ftp_credentials option and set ssl to 0 and then the plugin upgrade worked.
  2. It doesn't remember the value of the "Use SSL" option. After I go in to upgrade another plugin the option is set to "yes" even thou in the database it is 0.
  3. it removes the old plugin first and then tries to install the other. If the new version install doesn't work for some reason you are left with no plugin and you will have to go in and install manually the old way. I think a bettwe approach would be to first deactivate the old plugin, make a backup copy, try to install the new one and if it doesn't work restore the old plugin.

Conclusions

Overall wp 2.5 brings good improvements and great new features but as with any new major version increase there are a few minor details not taken good care of. Waiting to see what the 2.5.1 version wil bring on he bug fixing side.

Have you upgraded yet?  what are your thoughts on the 2.5 version ?

Hidden Affiliate Links update version 0.2

There is a new version of this plugin. Please download it from the plugin page

After the first release of the Hidden affiliate Links plugin some bloggers wanted to know if this plugin would be able to automatically replace some keywords in posts and turn them into affiliate links.

John Chow bought a plugin for wordpress that does something like that from DealDotCom (yes it's an afiliate link and it was created automatically ) but I know it wouldn't be that hard to add such a functionality to my plugin. So here's the real deal they should put on DealDotCom:

Now my plugin will let you define keywords and it will replace those keywords (when you save/update a post) with a new link you can specify. And it will still work for adding such links manually as the first version does (just don't put anything into keywords or Link fields ) .

The keywords field is actually a field where you can specify a regular expression ( so make sure you escape special characters like - or . (dot ) by placing a \ ( backslash ) in front of them.

Example Use

Let's say you write a post and you want this plugin to replace both the keywords/phrases DealDotCom and "Deal Dot Com" then the content of the keywords column should look like DealDotCom|Deal Dot Com . The "Match slug" field can be go/dealdotcom and the "New link" should be something like "/go/dealdotcom" or "http://patchlog.com/go/dealdotcom" ( without the quotes ) - this is what it looks like on your blog.

Download new version

Hidden Affiliate Links v0.2 zip file / tar.gz file

Installation

If you have already installed version 0.1 you will have to deactivate from wp-admin -> Plugins and delete the table wp_hal_links then just install the new plugin like any other plugin.

Notes

Because the plugin allows you to specify regular expression for the " Match Slug" field you will have to escape some special characters like . (dot ) , _ , - and / by placing a backslash in front of them . I could make the plugin so that this will not be necessary but that would make it lose the great flexibility that regular expression matching can offer.

ToDo:

  • make it configurable so that the user can chose to match case sensitive or insensitive keywords and link slugs. At the moment the match is case sensitive but some users may not like that. It would also be nice to be able to specify this for each link.
  • give the user the option to replace the keywords at runtime (every time the post is rendered). This has the advantage that the keywords on all posts will be replaced ( not just the ones you update after you install the plugin ) but has the disadvantage that it can slow down your blog
  • Anything else you would like ? feed free to let me know in the comments...

Hidden Affiliate Links WordPress Plugin

There is a new version of this plugin. Please download it from the plugin page

This post is about a wordpress plugin that will help you to manage your hidden affiliate links a lot easier then what others have suggested.

If you don't know why you should hide the affiliate links then continue reading, but if you already know this then skip to the plugin part of the post.

One of the best monetization methods for blogs is using affiliate programs.Money making blogs don't get their income from a single source like adsense, they also use affiliate links for a lot of services like Text Link Ads and other products.

Why hide your affiliate links?

Many bloggers think it's good to hide the affiliate links, not because they don't want their visitors to know they are affiliated but because of other reasons like:

  • Changing your affiliate links is a lot less work because you have to change in just one place then on hundreds of pages where you use those links
  • It hides the ugly part of affiliate codes. People tend to not trust long links with a lot of characters and unknown codes. A nicely formated and clean link is more likely to be clicked then one that contains an affiliate code in it.
  • It kind of forces them to click the link instead of just typing the link (without your affiliate code ) in the browser.
  • better SEO, the links don't look like affiliate links, the redirect is done from a html page that has noindex on it so search engines shouldn't look at it.

Methods to hide Affiliate links

There are a lot of methods to do this like:

  • writing a .htaccess rule
  • doing a http redirect from a script. Setting the HTTP location header to the destination url.
  • doing a redirect from the meta of a html page
  • doing a redirect from javascript by setting the window.location to the destination URL

Those methods are not hard to use but they do involve write a small piece of code and require this for each affiliate link. I will not show you in details any of those methods because there's a better, easier way to do this.

My Method to hide affiliate links

I wrote a plugin for wordpress that will let you define such redirects from a nice (AJAXy ) web interface inside your wordpress admin.

The plugin installs just like any other wordpress plugin and once installed you can start defining your affiliate link without having to write any code.

The plugin uses a meta and javascript redirect inspired by a post on John Chow dot Com about hiding affiliate links for better seo

License

The plugin is released under GPL and the usual "No Warranty" disclaimer applies

Download

Hidden affiliate links v0.1.zip
Hidden affiliate links v0.1.tar.gz

Installation

The script was tested on wordpress 2.2.3, so it you use any other version and it doesn't work let me know about and problems and I'll try to write a fix. If you use a previous version you should really upgrade to the latest version of wordpress.
Copy hidden-affiliate-links.zip and unzip or copy hidden-affiliate-links folder in wp-content/plugins then go to wp-admin -> Plugins and activate the Hidden Affiliate Links plugin

Using the plugin

After you activate the plugin you can go to wp-admin -> Manage -> Hidden Affiliate Links and add/edit/delete hidden affiliate links. For each link you have to define a slug, a destination link/code and a description.Manage Hidden Affiliate Links

The "slug" is what the link looks like on your blog, the link/code is the part where the visitor will be redirected and the description is something to remind you what's this link about, but it's also used in the title of the page that's doing the redirect ( in case the redirect does not succeed ).

For example if the slug is go/tla and the link/code is my affiliate link for Text Link Ads http://tinyurl.com/2kasce then if you write a post on your blog and you use a link like http://patchlog.com/go/tla in your post, anyone that clicks that link will be redirected to http://tinyurl.com/2kasce

Why link/code and not just Link? In case you want to hide an affiliate link where you cannot just do a redirect, like adsense referral links that are generated by javascript, then you will have to put the javascript instead of the link.
Because you cannot redirect to a link that is generated by javascript ( because there's no link until the javascript executes ) you'll have to redirect to a page where the javascript is inserted and will execute to generate the link. In this case the user will have to actually click the generated link. So if you put anything else then a link ( starting with http or https ) in the link/code box then the script will think this is a code and will not try to redirect to the code but to a page containing the code. You can customize the look of that page by modifying : redirect_code.php

In case the redirect does no succeed for some reason then you will see a page with the affiliate link in it and the title of the page will be the one specified in the description for the link. You can also customize that page by modifying: redirect_link.php

Advanced Feature

The script is very easy to use but if you want some power and you have some knowledge of writing regular expressions then here's how you can use it. The slug part can be any regex, and anything that will match that regular expression will be redirected to the real affiliate link.

For example I could make the slug like this: (.+)/tla and everything that ends in /tla like http://patchlog.com/go-to/tla or http://patchlog.com/recommended/tla, would be redirected to my text-link ads affiliate link. Or something like go/(tla|text-link-ads) would match both links like http://patchlog.com/go/tla and http://patchlog.com/go/text-link-ads

I hope you like the plugin and let me know of any questions or problems you might have. Feedback is highly appreciated in the comments section of this post.

If you like the plugin you might want to subscribe to my full RSS feed, other great plugins might be on the way.

Update:

there's a new version that can also automatically replace keywords you define with hidden affiliate links Click here to get it.

More optimization for comment relish plugin

In my previous post about optimizing the comment relish plugin I managed to lower the load generated by the comment relish plugin on the database server by adding an index on a column in the cr_emailed table and by optimizing a query ( basically removing a "useless?" join ). This improved the load time a lot but some users still reported slow load times on blogs with a lot of comments.

Looking more over the source code I realized that the function that tried to find new commentators was executed on each page. This function was executing a mysql query that joined two tables wp_comments and wp_cr_emailed. I think you can imagine the result of this on blogs with a large number of comments.

The solution was to execute this function only when a new comment was posted. And this comes with two benefits:

  1. because this is called only when a new comment is posted ( or approved ) the rest of the blog will remain as fast as before the plugin was installed.
  2. because of the way we call the function ( as an action/hook associated with the code that processes the comments ) we have more information about the comment so we don't have to do the query where we join two tables. We still do one query to get the whole comment data and one to see if this is a new commentator ( not emailed yet ) but these use indexes and are really fast.

Get the new optimized plugin here and let me know how it works for you.

Update:

the plugin link above contains a plugin that tries to send email even when the blog receives a pingback or trackback as reported by Rhys. I have uploaded another version that corrects this problem here: [download id="9" format="1"]

Comment relish optimization

Comment Relish is a wordpress plugin that sends an email the first time someone comments on your blog. The plugin allows you to define the message in wp-admin and you can embed tags like author's name, email, website and others in the message.

This can be used to send a welcome message to the first time commentators and maybe invite them to subscribe to your rss feed.

The Problem

John Chow, tried to use it on his blog but the plugin and his huge number of comments ( 43000 ? ) put the site down for about 2 hours. He said the plugin misbehaved and he would not recommend it if you have a lot of comments.

I did a little simulation on a wordpres blog with about 25000 comments and I can see how the plugin misbehaves :). It took more then tho minutes to select the email addresses that should be emailed before I decided to stop it, because that's just unacceptable, but I think it would have taken a lot more.

I managed to optimize it easily by just modifying a field in the table used by this plugin. If you don't care how I did it and just want the optimized plugin skip to the end of the post. If not then read on...

How it works

The plugin uses a table ( cr_emailed ) to remember which addresses received an email, so that it does not send a message more then once. Basically the plugin just inserts a new record in this table every time someone new comments on the blog. It tries to see if someone is new by doing a LEFT JOIN select on two tables: comments and cr_emailed, and then filters the results that have cr_emailed.email = NULL. The problem with this is that the email column has no index so if you have 43000 comments the query will have a huge result set and mysql will have to process each result to find out if cr_emailed.email is null ( the email has not received a message yet ) .

The solution

This was really simple, just convert the email field to a varchar ( needed in order to be able to set a fixed length key on this field ) and then add an index on it.

After doing this the query that took more then two minutes, now takes a little less then 1 second.

Some one on John Chow's blog suggested to add an index to wp_comments.comment_author_email. I tried that but I noticed no improvements, and using explain on the sql query shows the index on comment_author_email is not used, so that's useless.

Looking more over the code I noticed another weird thing. The query executed to find the email addresses looks like this:

  1.  

I wonder why the author used the posts table in this query? The results from the posts table are not used anywhere in the code. I really couldn't see a good reason for keeping the posts table in this query so I removed the posts table from this query, making it even faster.

Get the source code

Here is a diff file between my version and the original version and here is a my new version . If you have this plugin already installed you have to uninstall it and remove the wp_cr_emailed table and then copy the new version and activate it in wp-admin.

I'm using the new version on this blog and is working but I have few comments here. I have only tested the modifications on this blog and on a fictional blog with 25000 automatically generated comments so the usual disclaimer applies... you know all that "WARNING NO WARRANTIES" stuff...

If you have a blog with many comments and you want to try this, let me know how it works in a real environment.