This is a MySQL query I find myself using in a bunch of situations when there are multiple users contributing to articles or other means of providing content.  It’s usually to replace absolute links like “http://tools.belchamber.us/link-here/” to just “/link-here/”, but I found it’s useful for many other things.  Recently, I had to import thousands of articles from an older site into a new magazine site and ran into this, and other problems where this query made short work out of issues stemming from people hard-coding font size and inline styling.  Back then, this was the way to do things, but honestly, if you don’t encounter content these days where the only “inline styling” is assigning a certain part of the code the proper <H1…H6> tags or a class, then sit down with your content team and have a serious discussion because it will save you hours of re-editing content later the next time you update your website!

Using this “find and replace” query can quickly remove inline styles embedded into content with a class — a much better solution since, who knows, maybe you want to change the font, color and style of all the sub-headers later, so if everything is assigned a CSS class, you just change the class definition in the CSS instead of, oh, I don’t know, EDITING THOUSANDS OF PAGES MANUALLY?!!!!!!

Here’s an example, some goofball hardcodes something like this:


<p style="font-size:11pt;font-family:Arial,sans-serif;color:#999999;font-weight:bold">I am an idiot for writing content with inline styles.</p>

With search and replace, you can quickly replace the inline attribute ‘style=”font-size:11pt;font-family:Arial,sans-serif;color:#999999;font-weight:bold”‘ with something elegant and centralized with a class:  ‘class=”no-more-inlines”‘.  So, following the pattern of this basic MySQL find and replace query below, here is how you could replace the inline style completely with a class.  You just have to make sure a class isn’t also already assigned to the tag!


UPDATE [table_name] SET [field_name] = REPLACE([field_name], “find text”, “replace text”);

UPDATE my_table SET post_content = REPLACE(post_content,'style="font-size:11pt;font-family:Arial,sans-serif;color:#999999;font-weight:bold"','class="sub-heading-1"')

Before running these types of queries, keep in mind it is destructive so you should always back up the table before running this query, in case of any unintended consequences.


This handy query will return all the post IDs from all your WordPress posts that are exact duplicates.  I usually run this query in a loop and then will delete the newest post duplicate.  This problem usually comes from multiple users importing content into the new WordPress site or importing content from Joomla into WordPress without cleaning the data first.

I go by post content and not just titles, this is a good idea if you have more than 500 articles since there would be a high likelihood that some titles may match but the actual content of those matching title posts could be the same.  This ensures your website does not have duplicate content, which not only will help with your website performance and improve organization of your site, it could drastically improve your SEO rankings since all URLs and content on your site are unique.


//MySQL query -- posts_table is your WordPress "posts" table

SELECT ID FROM posts_table
JOIN (SELECT post_content FROM posts_table
GROUP BY post_content having count(*)>1) dupe
USING (post_content)

Even though there are thousands of plug-ins for WordPress that could accomplish many tasks, I have found that installing a plug-in to perform certain maintenance functions and other admin tasks are cumbersome and unreliable.  The WordPress database structure is very simple and straight forward and it is recommended to try to use single-line database queries that could accomplish the same thing without burdening your website with plug-ins that will slow down your website and cause possible conflicts with the plug-ins you already have installed.

Even for routine maintenance, it is a good idea first to consider running and testing a script that can interface directly with your WordPress database and set up a scheduled CRON job than install another plug-in to perform the same tasks.  Avoid relying too heavily on WordPress plug-ins!  You have been warned!!!


It’s inevitable, right when you thought your website was going smoothly, suddenly those strange symbols like the “black diamond question marks” start popping up. I sometimes have to clean variables returned from form input and other external text with PHP’s “iconv” function, but if the data is coming from a database and showing up funky, try forcing PHP to read the data as “UTF-8”. What’s “UTF-8”, you ask? It’s an encoding method like “latin1” that we humans devised to make sure nothing will be easy. Yes, it’s just not complicated enough all those bits and bytes of programmed code to work together like a million cells in the human body in order to function and not collapse into a big puddle of useless stew, we’re going to introduce unlimited issues that exist for a reason. Or do they? That line of thinking will argue there’s actually a good use for cockroaches…

exec("SET NAMES utf8");

// Getting really desperate?  Force any string to be reencoded and experiment with the results:
$newstring = mb_convert_encoding($oldstring, 'ISO-8859-15', 'UTF-8');

?>

Other functions to check out that may help getting rid of those pesky creatures? Check out the iconv() function.


Moving from procedural with mysql_connect, etc to PDO objects should be an easy transition.  It’s so much faster and reliable, too.  The “mysql” library in PHP will soon be deprecated, so you hold-outs won’t have a choice.  It’s actually less code to write and get whole queries into resulting arrays much faster.  Trust me!

Here’s how you instance a new PDO object and connect to your MySQL database through PDO:


<?php

/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'username';

/*** mysql password ***/
$password = 'password';

try {
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
/*** echo a message saying we have connected ***/
echo 'Connected to database';
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>