I recently moved a photoblog from one domain to another, and from WordPress to Habari at the same time. It was one that had been running for a while and I had 180+ photos that I needed to move to Habari, and more, to move across domains. That meant 180+ posts with an image URL in each one. The solution was fairly simple so I thought I should share the basic code I used.
This could just as easily be done using SQL straight to the database but I felt that it would be an interesting little project to find out how to do it in a plugin.
There is only one function in the plugin so I have only included the function and not the rest of the plugin, i.e. the pugin info.
/** * Function that runs automatically when the plugin is activated. */ public function action_plugin_activation(){ //set variables to hold replacement counts $total_replacements = 0; $pages_with_replacements = 0; //get a list of entries //iterate through each post foreach($posts as $post){ //get the post content $content = $post->content; //replace the old URL with the new 'http://www.newurl.co.uk' , $content , $repcount); //update the content in the post object $post->content = $content; //call update to save the changes use true to indicate that it is //minor so the update function should change the modified date. $post->update(true); //update the replacement count if ( $repcount > 0 ) { $replacements += $repcount; $pages_with_replacements++; } } //send a note to the log to give the number of posts and replacements made $log_text = "$replacements replacements made"; $log_text .= "on $pages_with_replacements pages."; }
Hopefully the comments should make it pretty evident what is going on, but I do want to highlight two parts of this plugin that rely on Habari's strengths.
The first point is that the post object contains all the logic needed to manipulate the post. So making changes is a very simple matter of just updating the content as needed and calling the update method.
The second point is the ease with which a plugin can make a log entry. This is then available to search in the log page of the admin section so you can get feedback on the plugin without needing to write any output code.
This blog is my stage for investigating and using Habari. This is my place to try new themes and plugins in the real world and live with it on a day to day basis.
Leave a reply