» October 5, 2007 in

The following tutorial will show you how to make a basic widget that a blogger can put on their blog and also give them the ability to change the colors of the widget.
At the end of the tutorial you will have a widget that looks like this one and you will be provided the files to download to get started making your own widgets ;-)
This is very geeky stuff and some php knowledge will be necessary to follow along. But you may feel geeky one day so be sure to bookmark it ;-)
Overview
For every blog that adds our widget to their blog menu the blogger will be given a small piece of javascript to include in their template.
The javaascript calls a php file and sends a variable or two along with it.
Our php file grabs the variables, does something with them, then returns the output back as a widget to display on the blog.
What Do We Want Our Widget To Do?
Since we are only getting our feet wet we will keep the widget functionality very basic.
We want our widget to say our blogs name and give the ability for the blogger to customize the colors of the widget.
Calling the PHP File From Peoples Blogs
We will start with a php file called mywidget.php that we want each blog to call to get the widget.
We also want to send two variables along with the call which will be textcolor and background color.
Our simple request a blogger will use will look like this:
<script type="text/javascript" src = "website.com/mywidget.php?textcolor=ffffff&backgroundcolor=000000> </script>
In summary the javascript on the blog page calls mywidget.php and also tells it two variables which are the text color (textcolor=ffffff) and background color (backgroundcolor=000000)
Inside the PHP File
Now we want to dive into the PHP. Open up mywidgets.php which is blank at the moment and let's build our widget.
Step 1 - Grab The Variables
PHP needs to grab the variables (the two colors) from the url.
Not only does the following lines of code grab the variables, the code also strips any characters except numbers and letters (sidetracking those nasty hacker people):
$textcolor = preg_replace("/[^a-z0-9-_]/i","",$_REQUEST['textcolor']);
$backgroundcolor = preg_replace("/[^a-z0-9-_]/i","",$_REQUEST['backgroundcolor']);
Step 2 - Further Validate The Variables
Our colors can only be 6 characters long, or a traditional hex color. (for instance ffffff is white).
To validate the colors we will check the length of the received variable. If it doesn't equal 6 then we will set it to our default color, otherwise the color is ok and is already set to the color to that the blogger has requested, like this:
if (strlen($textcolor) != 6){$textcolor="ffffff";}
if (strlen($backgroundcolor) != 6){$backgroundcolor="000000";}
Step 3 - Package The Widget To Send Back to the Blog
Now that we got our colors and we know the blog is waiting on us to send something back, let's get the widget ready to send.We will make a basic box with our blog name in it and make it the colors that the blogger requested.
$widget= "<div style="color:#" . $textcolor . ";background-color:#" . $backgroundcolor . ";text-align:center;font-size:15px;width:100px;height:20px;padding:10px;">";
That is our basic box with the colors set, let's write our blog name and close the box.
$widget.= "My Blog Name</div>";
Step 4 - Send Back The Final Widget To The Blogger
This is a little tricky but not really. Because we are in php we will need to echo the javascript command to write the widget.
echo "document.write('$widget')";
In the end the request for the PHP file returns a nicely formated box with your name in it and tells the blog to document.write or output the nice widget we put together ;-)
In Closing
Use this tutorial at your own risk. There are many considerations such as security and performance when building widgets that aren't addressed in this tutorial.
There are many ways to build widgets. This way is only one way. Hopefully you enjoyed this tutorial on how to build your own PHP widget.
Once again. You can try it out yourself at the following link. You can download the completed PHP code here.
If you like this post then please consider subscribing to my full RSS feed. You can also Subscribe to 45n5 by Email and have new posts sent directly to your inbox.
koen () says on October 5, 2007:
Great to see this Mark! I think a lot of people will be helped with this. And the regular expressions are a nasty great idea too ;)web boy says on October 6, 2007
thanks. very useful.
aaroncookdotcom () says on October 7, 2007:
Hey Mark,Just wanted to let you know that I featured this post of yours in my Super Link Sunday! - Batch #4 blog post.
I think it's a very useful tutorial that should reach as many bloggers as possible. :)
Shine on,
Aaron
45n5 () says on October 7, 2007:
wow, thanks for linking and sharing it further with the world ;-) I hope it helps some peole out also.aaroncookdotcom () says on October 7, 2007:
You're more than welcome, Mark. And I'm quite certain that it will. :)Shine on,
Aaron
Bob says on October 8, 2007
Thanks
How do you embed a link?
I keep getting http://presentpage.com/gotopage.com
when I code this line in to the php file.
gotopage.com/my_new_widget.php (appended to $widget with appropriate href code for a link)
I can't put in the full html line, your software deletes all the html codes, fair enough.
Whereas all I want is http://gotopage.com
thanks
45n5 () says on October 8, 2007:
hello bob, welcome to 45n5by embed a link, do you mean how to add the widget to your site? if so you need to add it to your template.
Most blog softwares like wordpress won't allow you to put widgets in posts and removes them. I'm guessing it is your blog software that is removing the code.
Did that help?
Bob says on October 8, 2007
Yes, I added it to my template within wordpress. sidebar.php
I put the code on one website, then inserted the calling script from other another side inside wordpress.
I get http://currentsite.com/widgethostingsite.com/widget.php
which obivously does not work. I want just plain http://widgethostingsite.com/widget.php
This does not make sense. I wonder if my host (bluehost) is trying to prevent some sort of cross site scripting. But other people's widgets, such as blogrush work just fine.
thanks
bobc
45n5 () says on October 8, 2007:
hey bob, when you get the calling domain added first in the url that usually means you dropped the "http://" or have a typo in it.check everything again to make sure every url has a "http://" before the domain and see how that tread you ;-)
Bob says on October 8, 2007
Got it working, instead of href=`qualityisbusiness.com` inside a /a then doc.write out I just dropped the `` and it works. go figure.
It's on the lower lower right hand right aide above site admin. http://qualityisbusiness.com
Thanks,
45n5 () says on October 8, 2007:
Awesome bob!No kidding, I've been stuck digging through searches for technical answers for HOURS before and then found out the problem was I forgot to do something very basic. (more times than I'd like to admit ;-)
Glad it is working for you.
Neil Duckett says on October 8, 2007
Is this what the guys use when they have their affiliate scripts in a /go/tla.php type format? I've never worked out how to do that.
Chris says on October 9, 2007
This is so helpful; I was just searching for info on this last night. I ran across your site through Aaron's, who I was checking out as a fellow recipient of Erina Hart's "thoughtful blogger award." I'm so glad I did. You have a great site here!
45n5 () says on October 9, 2007:
@neil - no, that is something different usually called "affiliate link cloaking".@chris - glad you liked it ;-)
Neil Duckett says on October 10, 2007
Thanks Mark, I`ll do some research on it now i have a `term` i can look for.
Part Time Blogger says on October 25, 2007
Great post Mark,
Didn't know what a widget was until a week ago, this is a very informative post. One of many i've been checking out on the blog already.
Thanks and keep up the good work!
Bryan says on May 13, 2008
I now understand how to make a widget in PHP. Thanks!
tom says on June 23, 2008
Hi
Found my self here, and thought i share how i make my widgets it so simple and easy..
www.swfmagician.com
injoy
mae says on July 4, 2008
I been using this for awhile, it works great for me.
For making flash widgets
I dont have a lot of expeince in the script method..
hope this helps anyone..
www.swfmagician.com
The 45n5.com Membership SiteTry MashupMoney.com, $1 for 7 day trial.
Start Your Own Membership SiteAmember rocks, free download and trial.
Create Your Own Mailing ListAweber is the best in the business, check them out.







