I want to share a database connection access problem I had last week, while working with a custom PHP script inside WordPress. I had created this WordPress Page Template containing some PHP code that needs access to the database. The problem was that the database connection for my custom script overwrote the WordPress database connection that was previously establish, causing certain parts of WordPress to not display properly.
It took me a while to figure out that it was the newer database connection of my custom PHP script that was causing the previously established WordPress database connection to disappear. Hence, certain parts of the WordPress page were not displayed. Little did I know, that the fix was quite simple. So, here’s a sample of my mysql_connect code. Prior to this line, I’ve already set the variables.
Mysql_connect
$db=mysql_connect($host,$username,$password);
The Fix
Simply add a fourth parameter called new_link and set it to TRUE.
$db=mysql_connect($host,$username,$password,TRUE);
What this does is basically telling mysql_connect to establish a new connection, while keeping the older mysql_connect connection around, in case we need to access it at a later time. It’s amazing how one little switch in a command can make a huge difference to this seemingly simple code. Anyways, adding a fourth parameter and setting it to TRUE was the solution.





Close Comments After X Amount Of Days
One thing I recently implemented on my WordPress blog is to close comments on posts older than 30 days. After 24 hours, I noticed my spam comments has dropped dramatically to zero. That’s a good thing.
To close comments on posts after x amount of days, all you have to do is access your WordPress Dashboard > Settings > Discussion page. Look for the option saying “automatically close comments older than x days.” Here’s a snapshot of the page.
Snapshot
Just check it to turn on the feature. Supply the number of days that you want the comments to be turned on. Comments will be turned off on posts older than x number of days that you’ve provided. Simple enough. This is just another tool to help lower your blog’s spam comments. It certainly did on my blog.