Archives

Modify PHP CLI Include Path Dynamically

I was working with an application platform that I had just downloaded today. I just wanted to check it out and play with it a bit. In the application framework was an executable script that provided some command line utility. I fired up the script and received an error like “PHP Fatal error: require_once(): Failed opening required…”. Of course non of the files from the downloaded archived were in the include path for my system, but I didn’t want to just go adding it to my php.ini file just to play around with it short term. What to do.

Command Line Options

First I wanted to see if there was a command line option that I could pass to PHP to tell it what include_path to use for just this one execution. If you execute the following you’ll see the usage:
php --help
There is no include_path command line option, but there is the -d foo[=bar] option which allows us to modify any of the INI entries by key/value.

Continue Reading

Cleaning Up HTML Code

I love (sarcastically) cleaning up other people’s HTML code. For instance I just found this little snippet.

So let me get this straight… in this table column you want a single space, formatted with”Arial, Helvetica, sans-serif” font of size 2. Then you want to follow that with another single space formatted with the “Arial, Helvetica, sans-serif” font and size 2.

Geez.

Inserting A Picture Into Your WordPress RSS 2.0 Feed

I use NetVibes for my browser home page and RSS feed reader and I’ve noticed over time that some of the feeds that I subscribe to have pictures embedded within their feed summary. Why doesn’t my feed have a picture in it? I thought that this was really neat because it gives you a quick visual queue as to what the article may be about. It is also eye-catching so that among 5 or so feeds on a page you are instantly drawn to the one with pictures. So how can you get a picture into your feed?

RSS 2.0 Specification

The RSS 2.0 specification has an <enclosure> element available at the <item> level. This element can specify various types of media that you want attached to your feed item. In our case we want that to be an image. The element should look something like this:

<enclosure url=”[URL To Media File]” length=”[Size Of Media File]” type=”[Mime Type Of Media File]” />

All three attributes are required:

  1. url – This tells the feed reader where the media file is located
  2. length – This is how big the media file is in bytes
  3. type – This is the standard MIME type of the file

WordPress Action Hooks

So, how do I get this information into my RSS 2.0 feed? Luckily the good folks at WordPress have included a little action hook in the file that generates your feed. So all we have to do is write a function and hook the action! For my purposes I was writing a new theme so I just included all of this in my functions.php file. You could modify the functions.php file of your existing theme or this could actually be made into a plugin.

First we have the function that does the heavy lifting. The first thing I do in my function is get the image URL. For my purposes I store the image URL in a custom field attached to each post. I also include a default image inside my theme in case I forget to supply an image for a post.

Next I calculate the MIME type of the file. Now there are some really cool functions out there that actually look inside the file to get to the real MIME type. I don’t really need to be that sophisticated since I’m in control of the image so I’ll just use the file extension. I’m supporting PNG, GIF and JPEG (default) image formats.

The final piece of information we need is the filesize. That is a quick and simple function call if you have the absolute path to the file. I found a good function to get that information on the PHP site.

Now just echo out the enclosure element.

The action hook looks like this and should come outside of the function you define. The first argument is the action hook in WordPress. The second argument is the name of our function.

Putting it all together we have.

Conclusion

This article describes part of the reason I love WordPress so much. They were so thoughtful to leave so many useful and meaningful hooks into the platform and that makes my job as a web developer that much easier. Oh and by the way, the above trick won’t work on every RSS reader. That is implementation specific as to whether the reader decides to display the image or not. All you can do is server it up to them.

Looping Through Pages In WordPress

In a recent article I outlined how to modify the output of the wp_list_pages() template function in WordPress. Later on, however, I found that having the <span> tags that I added in every single title was not desirable. So instead of using a filter I wrote a small bit of custom code to iterate through my pages instead of using wp_list_pages(). Here is how to do it.

Wherever you need to output your pages just use some code like this:

Writing A WordPress Filter To Modify Post/Page Titles

I was recently working on a new WordPress theme when I had need to modify the title of pages before output. The style I was attempting to use involved wrapping <span> tags around the title (inside of the href tags). The wp_list_pages() function returns pages (which are basically the same as posts) and has several arguments that you can pass to customize it’s output. However, modifying the title output is not one of the arguments.

Filters to the rescue! I simply opened up the functions.php file and added a function like this:

Pretty simple, eh? All that is left to do is to register this function as an appropriate filter, which you can do like this:

The first parameter tell WordPress which function/activity you want to “hook” into. The second parameter is the name of your function. The final parameter is a priority specifier. The lower this priority number the earlier it will execute.

Done! Now instead of output like this:

I get this:

For extra credit you actually throw all of this into a plugin. That way you wouldn’t necessarily have to modify any of your theme files.

How Can I Recover My Lost MySQL root Password?

Many people use the MySQL open source relational database server. Mostly for websites, but I’ve also seen it used elsewhere. One problem that I recently ran into with my local development installation is… “what was my root password again?” Aaaggghh!

OK, sorry. Don’t panic; it can be recovered. A quick survey of the internet and I found this very simple and very useful how-to Recover lost MySQL root password mini-HowTo. Is is very well written and easy to follow… but, it only covers the process from a Linux installation. The basic trick here in either platform is to restart your MySQL server in and tell it to skip the grants table. So here is my addition on the Windoze platform.

Recover Your Lost root Password In MySQL (Windows)

  1. Stop the MySQL server
    1. You can accomplish this by opening the Services control panel applet, finding the MySQL service listing and clicking the Stop icon.
    2. Alternately you can open up the command line and type > net stop MySQL
  2. Start the MySQL server and skip grants
    1. Go to the MySQL installation directory and then the \bin directory under that (in the console)
    2. Type > mysqld-nt --skip-grant-tables
    3. Now the MySQL server should be running wide open (ie. every user that logs in has full access)
  3. Change root password
    1. Now from another console open the mysql client
    2. Run the following SQL query replacing the dots (…) with your new password
  4. Flush the privileges
  5. Quit mysql
  6. Stop MySQL server
    I tried actually doing a CTRL-C in the console that was running mysqld… but that did not work. So I just opened up the handy-dandy task manager, found the mysqld-nt.exe listing and clicked on it and finally clicked the End Process button
  7. Restart MySQL server normally
    1. You can accomplish this by opening the Services control panel applet, finding the MySQL service listing and clicking the Start icon.
    2. Alternately you can open up the command line and type > net start MySQL

There you have it! All should be well in the world once again and you should now be able to login to MySQL with your new root password. If not… umm… reinstall? lol

View Source Chart – A Firefox Extension

A couple of weeks ago I wrote an article titled Instant Source – Hacking Websites The Easy Way. It covered the Internet Explorer extension Instant Source, which allowed a much more informative view source functionality. I really liked this extension BUT being an IE only program I set out to find a Firefox alternative.

View Source Chart is a fairly suitable alternative that I found. It is a Firefox extension that allows you to view the source of the document in a state that represents the document after it has been altered by any Javascript. Here are a few of the features:

  • Draws a Color-Coded Chart of a Webpage’s Source Code
  • Displays Source in its Altered State After the DOM has been Manipulated by JavaScript
  • Graphically Displays HTML Tag Boundaries
  • Graphically Defines Tag Nesting Order, Structure and Hierarchy
  • Adds A Simple but Powerful User Interface to Source Code
  • Quickly scan and identify the types of tags a document contains without reading a single tag
  • See how deeply nested an element is, just by looking to its left (no scrolling)
  • Identify the ancestor tag types an element is nested within, without having to scroll and read them

I’ve been using this for a couple of weeks now and have found great use for the way it shows the structure of the document. Not only that but you can collapse/fold the document code to get a real birds eye view of what you are looking for.

View Source Chart

How Can I Extract And Convert Windows Icons For Free?

Have you ever wanted to develop a website that had some distinctly Windows characteristics to it? Have you ever wondered how you could get those nifty Explorer icons into a format you could use? Well, if you search on Google you’ll find plenty of tools that can do it all for you in one shot. However, they are all commercial or shareware applications. So how can you accomplish this for free?

Well, there are two steps to this process and it’s not exactly as user-friendly as the commercial applications will be. You can’t knock it for the price, though.

Extraction

The first thing we need to do is extract the icon or .ICO file out of the DLL or EXE that it happens to be embedded in. I found a very simple and FREE application that can do this called IconsExtract.

IconsExtract ScreenshotWhen you run the program you’ll be prompted to enter the file in which to look for icons. The default file is C:\WINDOWS\system32\shell32.dll which you can test first. Click the Search For Icons button and you will see all of the familiar Windows icons. Just click the one you want and choose the Save Selected Icons menu option. You can select where to extract the icon and that’s it for the first step.

Conversion

The next thing we need is to get this icon into a format that we can use on a website. This would be a gif, jpeg or png image usually. The software package I found that can accomplish this task (for FREE) is the ImageMagick library. If you’ve heard of this software it has probably been in reference to a prerequisite for another program. Many applications use this library as the foundation for image processing because it is so good. However, if you go to the site and download the Windows binaries which is a Windows installer you will see that there is more to this library than a few DLLs. That’s right, there are some executables in there! They are command line utilities and one of them is aptly named convert.exe.

In my case the icon I had extracted was 16×16 pixels and the images I was working with were all 20×20 pixels. Not only did I need to convert my icon, but I needed it resized as well. You won’t believe how simple the syntax is to accomplish this. Open up a command prompt in the directory where your icon file is. The ImageMagick setup program should have placed it’s bin directory into your path so Windows knows where to find convert.exe. So the command looks like this:
> convert.exe folder.ico -resize 20x20 folder.png

Conclusion

That’s all there was to it! Not too bad for completely free applications. The convert.exe program actually does a lot more. If you look at the help you’ll see that it is quite extensive in it’s abilities. The next time you pull out your credit card because you think that shareware program is the only way, think twice. There just may be a free alternative.

LyricWiki Search Widget Now Hosted On WordPress

LyricWiki is a free site which is a source where anyone can go to get reliable lyrics for any song from any artist without being hammered by invasive ads.

The LyricWiki Search Widget is a WordPress plugin that provides a widget to place a search box right on the sidebar of your WordPress site so that your readers can utilize the LyricWiki site and find their favorite lyrics from your blog!

This is not news… I’ve had this plugin posted up for a while. True! However now my widget is finally hosted on the WordPress Plugins Page. Hopefully this will give the plugin (and my blog) a little more exposure to the internet masses. 🙂 You can download it from the WordPress plugin site below:

Download LyricWiki Search Widget (24 kb)