Monthly Archives: August 2014

Code for deleting all wordpress database

In this post I share how to delete all the posts, comments, tags, and such with a wordpress site.  To delete the proper data from the database you need to know which data is connected and how.  wordpress.org does a great job of describing this in full detail but you can basically see the connection and get through the code below without it.  The following script could take a wordpress site like http://privateschoolsreviews.com  and delete the entire site except for the theme and settings.  It would be blank slate to start over essentially.

The goal is to clean the data from the following tables:

  •  wp_posts
  • wp_term_relationships
  •  wp_term_taxonomy
  • wp_terms

The following code is slightly incomplete but a little modification would have it up and running.

return(mysql_connect(MYSQL_ADDRESS, MYSQL_USERNAME, MYSQL_PASSWORD));
}
$mysql_connect = connect_to_database();
mysql_select_db(‘table’, $mysql_connect);

# Execute SQL command
$sql=”TRUNCATE TABLE wp_posts”;//e_state =’NC’
$result = mysql_query($sql, $mysql_connect);
echo “posts”.mysql_error();

$sql=”TRUNCATE TABLE wp_terms”;//e_state =’NC’
$result = mysql_query($sql, $mysql_connect);
echo “terms”.mysql_error();

$sql=”TRUNCATE TABLE wp_term_relationships”;//e_state =’NC’
$result = mysql_query($sql, $mysql_connect);
echo “terms”.mysql_error();

$sql=”TRUNCATE TABLE wp_term_taxonomy”;//e_state =’NC’
$result = mysql_query($sql, $mysql_connect);
echo “posts”.mysql_error();

$sql=”TRUNCATE TABLE wp_posts”;//e_state =’NC’
$result = mysql_query($sql, $mysql_connect);
echo mysql_error();

$sql=”TRUNCATE TABLE wp_posts”;//e_state =’NC’
$result = mysql_query($sql, $mysql_connect);
echo mysql_error();

TRUNCATE TABLE simply removes all the rows from the tables without deleting the table .  The structure remains but the information that was saved has been removed.  You should make a backup of your entire database because this is not reversible.

 

Comments Off on Code for deleting all wordpress database

Filed under Uncategorized

php Forward Script

Are you looking for a simple script to forward visitors to a new page?  How about have them see the old page and meScripts Gurusmo to update
their bookmarks or show a disclaimer before they are forwarded.  This snazzy little piece of code will do that and even forward to one random page out of the ones that you designate.

 

<?php

$r=rand(1,5);
//  echo any information to the browser then forward

sleep(3); // seconds to pause.
if($r==1)
header(“Location: http://your destination site address”);
if($r==2)
header(“Location: http://your destination site address”);
if($r==3)
header(“Location: http://your destination site address”);
if($r==4)
header(“Location: http://your destination site address”);
if($r==5)
header(“Location: http://your destination site address”);

exit;

?>

A site about private charter jets would benefit from this type of script.  Let’s assume that the site owner wanted to have a simple way to advertise and rotate banners.  He/she could have one lead generation source and direct those leads to 1 of 5 different full-fillers.  As a user visited the site and looked for a particular flight to book, the script would be available behind the banner or text link to one of the advertisers.  If the traveler clicks on the link it will randomly select, show the disclaimer, then forward the link as selected.

The header() function also has the ability to forward with the association of the type of forward.  It isn’t really that much of a use in this usage on a travel site and for this purpose but it may help if you were going to move a site and needed to let the search engines know that the forward was because the page had permanently moved.

Use this for “AB” testing.  If you are running a campaign and want to see which form works better and provides more leads or conversions, use this script to test which one randomly converts.  It is also great for affiliate links and testing which advertisers do a better job of converting the traffic you send them.  I would think that even a kickstarter campaign would be able to make use of this simple 20 line code.

Enjoy and good coding.

Comments Off on php Forward Script

Filed under Uncategorized

Import a Large CSV file into Database

In thiScripts Gurus post I would like to show you how I import a large csv file into a database.  I typically use the import function in my phpadmin and import the file that way but if the file is too large and times out I have to resort to other ways.  I use a “Library” file to handle the mysql connection to the database and insert to the database.  I will cover than in another post.
This script will take the csv file which have uploaded to your host server and parse it and insert it into the database.  The column names will transfer to the database and become the column names in the table.  I didn’t take the time to make a key or unique column but that can be easily done in the phpadmin.  After the file is opened by the script it will be parsed by the comma line by line.  Each line is processed and put into the database table.  Don’t forget to use the “addslashes()” to the script to work with special characters.

<?php

include(‘LIB_mysql.php’);
$fn = ‘Your_CSV_FILE.csv’;

$fd = fopen($fn, “r”);

$sql=”CREATE TABLE Your_New_Table
(
“;
$flag=0;
$xxy=0;
while (!feof($fd)) {

$fields = fgetcsv($fd, 0, “,”);

if($flag==0){
$flag=1;
for($x=0;$x<count($fields);$x++){
$data_fields[$x]=$fields[$x];
if($x>0){
$sql.=”, “;
}
// If the field is a special type then set the type in the database here.
if($fields[$x]==’LEANM09′ ||$fields[$x]==’SCHNAM09′ || $fields[$x]==’MSTREE09′ ||$fields[$x]==’LSTREE09′ ||$fields[$x]==’LCITY09′ ||$fields[$x]==’CONAME09′ ||$fields[$x]==’Location’ ){
$sql.=$fields[$x].” varchar(60)”;
}else{
$sql.=$fields[$x].” varchar(16)”;
}
}

$sql.=”)”;

}else{
{
unset($data_array);
for($x=0;$x<count($fields);$x++){

$data_array[$data_fields[$x]]=addslashes($fields[$x]);
if($data_array[$data_fields[$x]]==”){
$data_array[$data_fields[$x]]=0;
}
}

insert(‘Your_Database’, ‘Your_New_Table’, $data_array);
}

}

}

php?>

This script is great for those wanting to import a large data file to use with wordpress.  I have used this for many different imports such as for hospital databases.  The hospital database was very large and couldn’t be imported with the phpadmin function.  The nurse uniforms database was for the nurses, and doctors, and those in the medical field to find uniforms.  The data was fairly massive and so the script ran without it issue for the initial upload.

Comments Off on Import a Large CSV file into Database

Filed under Uncategorized