My First Wordpress Plugin and Widget

I knew this day would come. I finally bit the bullet and decided to figure out how to create a Wordpress Plugin and Wordpress Widget. I did it as part of a new members-only web site that I’m creating and wanted to provide a way for members to easily display my data on their Wordpress blogs.

Like most things, once I got everything working it really doesn’t seem too difficult, but getting to that point was a little frustrating. I didn’t find very much in the way of good documentation and examples, so I just looked at a few of the plugins and widgets that I already use on some of my blogs and tried to figure out what they were doing.

The functionality of the plugin was pretty simple. I wanted to make it possible for members  of my new site to be able to add some special syntax in a post and get info from a special RSS feed to be display. In order for the feed to display correctly the plugin requires a little configuration after the user has activated it. That meant I needed to create an "options" page.  So, I needed to create a few functions and add some calls to the "add_action" function:

  1. add_action(’the_content’, ‘myPluginFunction’); - this call registers the function (myPluginFunction) I created that will get called when a page or post is displayed (that’s what the "the_content" means, it is one of the plugin actions you can register for)
  2. add_action(’admin_menu’, ‘myPluginSetup’); - this causes the myPluginSetup function I created to get called when Wordpress displays the admin menus.  In the myPluginSetup function I created I just have a call to "add_options_page" to register a function that will display my custom option page that allows users to configure my plugin.
  3. The function I created to display my option page was pretty simple and there was decent documentation on the Wordpress developer site.

The widget was a little harder to get working because I wanted to allow the user to do a little configuration once they add it to the sidebar. Because I was trying to keep things pretty simple though, I was able to use the Wordpress functions of get_option and update_option for retrieving and story my options. This meant that I didn’t have to create a custom table and do any sql statements (not that I think that’s difficult, but it was just one more thing I didn’t want to have to deal with).

I still have some fine tuning to do, but I do feel pretty good about having gotten things to basically work. Now that I have one under my belt I just may have to go create some more!

Manual Wordpress Install

In my previous post I talked about how cool XAMPP is and that I was able to quickly install it and then install Wordpress on my local machine.  I didn’t give you any details on how I did that though.

Last night I decided to add a blog to one of my existing sites that I have hosted at ReliableSites.net.  So far I’m very happy with them.  I have several .NET sites hosted there, but they also support PHP and MySQL.  They don’t provide an automated install of Wordpress, which as it turns out is no big deal.

The process I followed to install Wordpress on my local XAMPP environment was the same as what I did last night to install Wordpress on my ReliableSites hosted site. And as you will see, it was pretty easy.

  1. You need to manually create a new MySQL database.  Connect to MySQL.  This is typically done using "phpMyAdmin" which is accessible from XAMPP or your hosting account control panel.  Once connected to your MySQL server, enter in the name of a new database (e.g., "wordpress" or "wp-blog" or..you get the idea), select "utf8_unicode_ci" for you collation, and click the Create button.  This will create a new, empty database.
  2. Unzip the Wordpress download file into a directory on your local machine.  You can get the download file from Wordpress.org. You need to unzip is on your local machine because you need to modify 1 file prior to "installing".
  3. In the directory that you unzipped the Wordpress files into you will find a file named "wp-config-sample.php".  First thing you should do is copy (don’t just rename!) the file to "wp-config.php".  Then you can open up the file using Notepad or any text editor (don’t be afraid if you don’t know PHP, this is a simple file!).  At the top of the file you will see lines like:

    define(’DB_NAME’, ‘putyourdbnamehere’); // The name of the database
    define(’DB_USER’, ‘usernamehere’); // Your MySQL username
    define(’DB_PASSWORD’, ‘yourpasswordhere’); // …and password

    These are the only 3 lines that you need to change. You need to specify the name of your MySQL database, the user name used to connect to it, and the password for it. That is all, move to next step.

  4. Now you need to copy the entire directory to its final destination.  For example, if you are using XAMPP you will need to copy the directory to the \xampplite\htdocs directory (so, if you’re wordpress directory is called "myblog" then you copy that and have \xampplite\htdocs\myblog). If you are using ReliableSites, for example, you need to use your FTP program and copy the folder and files to a folder on your web site.  You can copy into the root folder if you want your blog to be the root of your site.
  5. Okay, almost done. Open up your browser and navigate to the url with the wordpress files you just uploaded.  You will see a page the welcomes you to Wordpress and asks you to finish the install.  Click and it will create all of the necessary database tables, create an admin user account, and generate a random admin password.
  6. Now you have Wordpress installed and can go about configuring it with plugins, themes, and all of the normal stuff.

Now, that wasn’t too difficult was it? I realize I may not have gone into as much detail as some may need, but it really isn’t too hard.