Removing trailing comas with PHP

php_logo_mediumDealing with trailing comas in lists is common problem for developers. I found the best way to handle this situation in PHP is with the following regular expression.

$string = eregi_replace(',$', '', $string);

Before: string = “2, 6, 9,”
After:
string = “2, 6, 9”

Only the trailing coma will be stripped. If no coma is found, the string simply passes through. It’s a simple and effective solution.

Create WordPress Thumbnails Automatically

create wordpress thumbnails automaticallyWordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as meta-data. This meta-data can be used by theme authors to do some pretty cool things.

The theme I am currently using allows for a custom field named “thumb”. If you create the “thumb” custom field in a post and pass in the path to an image, for example “wp-content/uploads/2008/12/wordpress_logo.png”, a thumb nail version of the image will appear next the posts title on the home page of my blog. Awesome stuff.

When I discovered this theme and the “thumb” custom field, I already had 225 posts in my blog. I didn’t want to go back and manually create all the custom fields for the existing posts, so I wrote a PHP script to do it for me.

The PHP script cycles through your existing posts and looks for the first image in the post, and creates the thumb field based on it’s location. The script is configurable, your theme may want a different custom field name, like “thumbnail” or “image”.

This code is provided as is. If you aren’t a developer, you should be messing with stuff like this. Be sure to back up your wordpress database before running this.

Instruction:

  • download this code
  • configure the database connection info and custom field name
  • upload this code onto your server
  • point your browser to the file
  • watch the script do it’s magic
  • remove the file from your server
  • donate $2 to my paypal account (joshhighland@gmail.com)

Note: This is a one time deal, when you create new posts you will need to add the “thumb” field and image path manually

<?
 
//your database configuration information
$username = "YOUR_USER_NAME";
$password = "YOUR_PASSWORD";
$database = "DATABASE";
$databaseLocation = "localhost";
 
//name of the Custom Field to use
$customFieldName = "thumb";
 
/******************* END CONFIGURATION *****************************/
 
//get the wordpress posts to work with
$query = "SELECT * FROM wp_posts";
 
if ($result = $mysqli->query("$query"))
{
	//loop over all the posts
	while ($row = $result->fetch_object())
	{
		//get all the info from the post that we need to work with
		$cur_post_id = $row->ID;
		$cur_post_content = $row->post_content;
 
		//the pattern to find the html image tag
		$pattern = '/]+src[\\s=\'"]';
		$pattern .= '+([^"\'>\\s]+)/is';
 
		//try to match the pattern against the post content
		if(preg_match($pattern,$cur_post_content,$match))
		{
			//break up the image path
			$pieces = explode("wp-content/", $match[1]);
 
			//make sure the file name isnt empty
			if($pieces[1] != "")
			{
				//build the file path
				$imagePath = "wp-content/" . "$pieces[1]";
 
				//insert the custom field for the post
				$insertQuery = "INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES ($cur_post_id, '$customFieldName', '$imagePath')";
 
				$mysqli->query("$insertQuery");
 
				//debugging stuff
 
				if ($mysqli->errno)
				{
					printf("failed: %s \n", $mysqli->error);
				}
				else
				{
					echo "..... inserting $imagePath \n";
				}
				flush();
			}
		}
	}
}
 
echo "---completed---";
 
?>

phpBB admin password manual reset

phpbb_logo

Today I ran into a situation where I had forgotten the password to my development instance of phpBB 3. I was stuck in a situation were I needed to reset the password. I had full admin access to the database, so changing it there wouldn’t be the problem. The real problem is that phpBB uses its own password hashing, not MD5.

In a work around, I created a new user and used the password “123456” looking at the database , in the users table of the phpBB install. I saw the “user_password” field was “e10adc3949ba59abbe56e057f20f883e”.

I then changed my admin accounts user to the same string, “e10adc3949ba59abbe56e057f20f883e”.

phpbbpasswordreset

I went to the phpBB login screen, fillled out my username, and entered the password “123456”… BINGO! it worked.

So to save you the work. You can follow what I did or just use these hashes to reset your own password:

Hash: e10adc3949ba59abbe56e057f20f883e
Password: 123456

Hash: $H$9Ae3Uk.ECdWW5ya13M4ErWhr4c.761/
Password: password

I hope this helps someone else out there.

Modifying MimboPro to Add Paged Navigation To Category Pages

Recently I have been doing a lot of work with WordPress. I love WordPress. one of the things I like most about WordPress is the plugins and themes.

I recently bought a copy of the MimboPro theme. Mimbo is clean and very professional. More of a CMS theme then a blogging theme, prefect for what I want to use it for.

As I dug into MimboPro and WordPress, I realized something strange, the MimboPro theme doesn’t support paging in a category view. What this means is EVERY SINGLE post you make is shown on one page. If you have a very active site, like the one I’m working on, this quickly gets out of hand. Look at this image and you will see what I mean (click for full version).

One of the reasons I like MimboPro was the support that the authors of the theme offer. I headed over to their message forums and found other people like me asking for the Additon of Paged Navigation To Category Pages. The original post was 8 months ago, and no one had resolved anything. I made a post to the MimboPro authors only to be told that there is going to be a new theme that will handle this and it will be available at a deep discount to MimboPro owner. This upset me a bit, so taking matters into my own hand, I fixed what Mimbo wont, and I am handing out my own patch to solve this issue. Below are the instructions to hack your own files, or you can just download my zip file with the pre-hacked files.

Pre-Hacked Files:
Download with pre-hacked files: JoshHighlands_MimboPro_CategoryPagingHack.zip

DIY INSTRUCTIONS (click images for full sizes):

  1. Save a copy of your current categories.php and styles.css files inside of the MimboPro theme directory to a safe location
  2. Open up categories.php inside of the MimboPro theme directory, and find line 17
  3. Comment line 17 with a double back slash (//)
  4. Go to line 87 of category.php and add the following code
  5. Save and close category.php
  6. Open up style.css inside of the MimboPro theme directory
  7. Add the following CSS code to the bottom of the file (should be close to line 852)
  8. Save styles.css
  9. Upload categories.php and styles.css to the MimboPro theme directory on your server
  10. Everything should be working. Here is what my wordpress running MimboPro looks like

That’s it! MimboPro will now have paging in all of the categories. The next and previous links will only show up when you have enough posts. The WordPress default is 10 posts. You can edit the number of posts by going to change the number of posts to show on a category page, go into to the admin and under “setting” > “reading” you will find the option, as highlighted here.

If you had any problem reading the code in the image files, download the zip files with the pre-hacked files in them.

Download with pre-hacked files: JoshHighlands_MimboPro_CategoryPagingHack.zip

If you have any further questions, please post them in the comment.

WordPress 2.7 is almost here!

OMG OMG OMG! WordPress 2.7 is almost here. Here is a long list of the changes that are going to be in the latest release. I have been a huge wordpress fan for some years now. Its always treated me well. Its treated me so well that I have decided to move my main website, notPopular.com, to the wordpress platform for content management. I’m really nervous though, I have done a lot of custom work on the notpop install of wordpress, and it isnt released yet. I hope that my plugins and custom code doesnt break to badly. I will have a full review of wordpress 2.7 once it is released later this week.