Tag Archives: PHP

WordPress Import Hack – Quick & Dirty

After finally convincing a client of mine to replace their existing 6 year old, custom built, extremely limiting & buggy blog software I’ve had to put in a bit of effort to transfer there 150+ blog posts from the MySQL table into WordPresses MySQL tables, which is pretty straight forward; the key here is to keep all of there featured images in place.

The following code does just that. It takes all the posts from the current blog, imports them into WordPresses tables, it inserts the featured image and then links it to the parent post. All thats then left to do is upload the images directory to WordPresses upload dir.

[php]
## DATABASE CONNECTION
// trololol you don’t get to see this :]

## CLEAN URLS PLEASE
function toAscii($str) {
$clean = preg_replace(“/[^a-zA-Z0-9\/_|+ -]/”, ”, $str);
$clean = strtolower(trim($clean, ‘-‘));
$clean = preg_replace(“/[\/_|+ -]+/”, ‘-‘, $clean);
return $clean;
}

## SELECT THE BLOG DATABASE TO IMPORT, ORDER IT BY ID ASC (OR BY DATE ASC W/E)
$blog_q = mysql_query(“SELECT * FROM `blog` ORDER BY `ID` ASC”);
while($blog = mysql_fetch_array($blog_q)):

## INSERT POST INTO WORDPRESS
mysql_query(“INSERT INTO `wp_posts` (
`post_author`,
`post_date`,
`post_date_gmt`,
`post_content`,
`post_title`,
`post_excerpt`,
`post_status`,
`comment_status`,
`ping_status`,
`post_password`,
`post_name`,
`to_ping`,
`pinged`,
`post_modified`,
`post_modified_gmt`,
`post_content_filtered`,
`post_parent`,
`guid`,
`menu_order`,
`post_type`,
`post_mime_type`,
`comment_count`) VALUES(
‘1’,
‘”.$blog['blogDATESUBMITTED'].”‘,
‘”.$blog['blogDATESUBMITTED'].”‘,
‘”.$blog['blogARTICLE'].”‘,
‘”.$blog['blogTITLE'].”‘,
”,
‘publish’,
‘open’,
‘open’,
”,
‘”.toAscii($blog['blogTITLE']).”‘,
”,
”,
‘”.$blog['blogDATESUBMITTED'].”‘,
‘”.$blog['blogDATESUBMITTED'].”‘,
”,
‘0’,
”,
‘0’,
‘post’,
”,
‘0’
)”);

## INSERT IMAGE INTO POSTS FEATURED IMAGE
mysql_query(“INSERT INTO `wp_posts` (
`post_author`,
`post_date`,
`post_date_gmt`,
`post_content`,
`post_title`,
`post_excerpt`,
`post_status`,
`comment_status`,
`ping_status`,
`post_password`,
`post_name`,
`to_ping`,
`pinged`,
`post_modified`,
`post_modified_gmt`,
`post_content_filtered`,
`post_parent`,
`guid`,
`menu_order`,
`post_type`,
`post_mime_type`,
`comment_count`) VALUES(
‘1’,
‘”.$blog['blogDATESUBMITTED'].”‘,
‘”.$blog['blogDATESUBMITTED'].”‘,
”,
‘”.$blog['blogIMAGEALT'].”‘,
”,
‘inherit’,
‘open’,
‘open’,
”,
‘”.toAscii($blog['blogIMAGEALT']).”‘,
”,
”,
‘”.$blog['blogDATESUBMITTED'].”‘,
‘”.$blog['blogDATESUBMITTED'].”‘,
”,
‘”.mysql_insert_id().”‘,
‘http://www.newdomain.com/wp-content/uploads/2012/03/”.$blog['blogIMAGE'].”‘,
‘0’,
‘attachment’,
‘image/jpeg’,
‘0’
)”);

## LINK IT TOGETHER IN THE POST_META
mysql_query(“INSERT INTO `wp_postmeta` (`post_id`, `meta_key`, `meta_value`) VALUES(‘”.mysql_insert_id().”‘, ‘_wp_attached_file’, ‘2012/03/”.$blog['blogIMAGE'].”‘)”);

## AND SPIT OUT ONCE DONEā€¦.
echo $blog['ID'].’ —> ‘.$blog['blogTITLE'].’
‘;

endwhile; // end, select the blog database

[/php]

WooCommerce: Limiting Product Lists

Woocommerce limits the product listings in the shop in relation to the reading settings within WordPress, so if you only show 10 blog posts at a time then your woocommerce shop will only show 10 products per page. Not always a great idea, to fix add this to your functions.php file and change 20 to however many you want to show;

[php]
add_filter(‘loop_shop_per_page’, create_function(‘$cols’, ‘return 20;’));
[/php]

WordPress Dynamic Navigation

Sometimes in WordPress you don’t want to use the custom menus for some navigation parts, like sub navigations in sidebars or something. Sometimes custom menus are just over kill, especially if every page has a sub navigation made up of child pages.

Heres an easy way of generating the sub navigations of Parent / Child pages & also showing the Parent page title above it.

Really Simple WordPress Queries

When I was learning about WordPress and how to manipulate data, it seemed like everyone had an opinion on how to do what is actually very simple queries. Take for example; Running a custom query to get posts from a certain category. If you were to Google that you would be returned with pretty much thousands of example code, some working, some not. My 2 pence; Its this simple:

[php]
have_posts()) : $bloggy->the_post();?>

Parsing CSV files with PHP

Today at work I had the amazing job of importing around 50 different artists profiles in to a database – each with about 12 different columns of values in a CSV file.

I could copy & paste it from the document – or create a quick little import script… Yea, I made an import script :]