Creating a Shared Content Box Across All of Your WordPress Blogs
More than five years ago, I was bit by the Autoblog bug. I don’t build them anymore, but I still build WordPress blogs in large numbers. One of my pet peeves when I was working with 100+ different blogs was that if I wanted to interlink them, or have the exact same links on the sidebar of each blog, I would have to add these links manually to each and every blog every time I built a new blog. For example, if I have 98 blogs, and I want every one of them to have a link to blog #99 that I just created, I would have to add that link to all 98 blogs manually. That is very time-consuming, so I knew there had to be a better way.
Of course, PHP can do just about anything if you know how to tell it to. I thought it would be awesome if I could have a shared links box on the sidebar of each WordPress blog, and have a form online that I could enter in the name and URL to each new blog as I built them, and then have PHP add that link to all 98 blogs instantly. Thankfully, I was able to set this up exactly how I needed it. This is what I am going to show you today, and you can use it however you see fit. One thing I want to remind you of is that even though I am using the shared content box for links, it technically can be used for anything, your imagination is the limit. Let’s get started.
Hosting with PHP and MySQL Database (this is a common setup, so your host should have it)
Download the Samsarin PHP Widget to your WP blog; install and activate it. This plugin lets you create a widget that can execute any PHP code right in the widget. This is crucial for you because it allows you to connect to your PHP database and display the content right there on your blog.
Open up your PHPMyAdmin dashboard, and create a new database called links_database. You do this by clicking on the “databases” tab from the PHPMyAdmin home page, then you can specify the name of the database you want to create. If you don’t have enough admin rights to create a database from PHPMyAdmin, then you have to create it from within your hosting panel (CPanel, etc.).
Once you have the database created, you need to execute the following SQL code to create the table that will house the links. Do this by clicking on the SQL tab up top and pasting in and running the following SQL code:
CREATE TABLE IF NOT EXISTS `links` (
`site_name` varchar(50) NOT NULL,
`url` varchar(100) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
That SQL code will create a table named links inside the links_database database. Notice that it has two columns/fields: site_name is the anchor text, and url is the URL of the site that the anchor text will point to.
Next we are going to create a PHP file named connect.php which will contain the login and password for the specific database that we want to hook up to in our other scripts. Go ahead and create connect.php with your text editor and insert the following code into it (be sure to change the username and password to your own):
$hostname_links = "localhost";
$database_links = "links_database";
$username_links = "DB_USERNAME";
$password_links = "DB_PASSWORD";
$conn = mysql_pconnect($hostname_links, $username_links, $password_links) or trigger_error(mysql_error(),E_USER_ERROR);
Now we create another simple PHP script called update.php, which will actually update the database every time we create and add a new link. Fill the update.php with the following code:
<?php require_once('connect.php'); ?>
$site_name = $_POST['site_name'];
$url = $_POST['url'];
$query = "INSERT INTO links (site_name,url ) VALUES ('$_POST[site_name]','$_POST[url]')";
die('Error: ' . mysql_error());
echo "1 record added";
Next we are going to create another PHP script called enterblog.php which will serve as the online web form that you will go to every time you want to add a new link to the list. Create enterblog.php with the following code in it:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Enter New Blog</title>
<div align="center"><form id="form1" name="form1" method="post" action="update.php">
<table width="500" border="0" cellspacing="0" cellpadding="4">
<td width="190">Enter name of new site:</td>
<input name="site_name" type="text" id="site_name" size="50" maxlength="50" />
<td>Enter the url of the site:</td>
<td><input name="url" type="text" id="url" value="http://www." size="50" maxlength="100" /></td>
<td align="center"><input type="submit" name="submit" id="submit" value="Add Site to Database" /></td>
We now will create another PHP script called displayurls.php which will be the script we call from the widget in WordPress. This script simply pulls all of the rows from the links table in the links_database. Put the following code inside displayurls.php:
// Connects to your Database
mysql_connect("localhost", "DB_USERNAME", "DB_PASSWORD") or die(mysql_error());
mysql_select_db("links_database") or die(mysql_error());
$data = mysql_query("SELECT * FROM links") or die(mysql_error());
Print "<table border=0 cellpadding=3>";
echo "<tr><td><a href=\"";
Okay almost done! Upload all of the PHP files we created here to the root directory of your site. Then change the file permissions on them to 755. Once you are done with that, it is time to bring up the web form and enter in a few links to the database. To do that, just type in the URL to your enterblog.php script, which should be something like http://www.yourblog.com/enterblog.php and you will see a two field web form. Enter in a few sites and URL’s, and hit enter after you type each one. It should tell you that 1 row was added if it went through successfully. Once that is done we can move on to the final step.
Go into your WordPress admin and click on the Widgets option under the Appearance menu. In the widget selections you should see a widget titled something like “Samsarin PHP 1″, make sure you drag that widget somewhere onto your visible sidebar. When the widget pops up, enter in the title of the widget which might be something like “Links” and then in the body field type in the following PHP code:
<?php include("displayurls.php"); ?>
Then hit Save. If you should run into an issue where you cannot see the links, try typing in your Absolute Server Path inside the above PHP include code (so instead of “displayurls.php” you might put something like “/var/www/displayurls.php” but the exact path is different for everyone’s server). You can get the absolute server path from your hosting company. When you bring up your blog, you should see the links box on any site you put that widget on. If you have this widget installed on every blog on your server, you can add a link to them all instantly just by entering the link info once into the web form and hitting enter! Pretty cool huh?!
Just a side note, I am not a PHP programmer. I know enough to modify some things and write simple scripts, but if you are a PHP guru and you see something that can be improved in these scripts, or see something wrong, feel free to voice your opinion in the comments and I will listen to all of your ideas. Good Luck!