Archive for the 'Wordpress' Category
Post via script
I just whipped up some PHP and threw it in the root of a brand new Wordpress 2.9.2 install:
include_once('wp-config.php');
include_once('wp-load.php');
include_once('wp-includes/wp-db.php');
$my_post = array();
$my_post['post_title'] = 'My post';
$my_post['post_content'] = 'This is my post.';
$my_post['post_status'] = 'publish';
$my_post['post_author'] = 1;
wp_insert_post( $my_post );
?>
Voila. The post appeared when navigating to the .php file.
See: http://codex.wordpress.org/Function_Reference/wp_insert_post.
For more creativity, get into the database: http://codex.wordpress.org/Function_Reference/wpdb_Class
Posted by Karl Herrick on March 11th, 2010 in Wordpress, PHP, Web Development | No Comments »
Recursive find and replace
From time to time I find myself wanting to replace a bit of text, in multiple files, throughout a huge directory tree (particularly in the case of a Wordpress migration).
Here’s a method I frequently use (other examples I’ve seen out there make use of perl, xargs, grep, etc.):
user@host:~$ find . -type f -exec sed -i 's/Text_To_Find/Replacement_Text/g' {} \;
The find command will return a list of all the files in the directory tree and execute the sed command on each one it locates. In the example below, I added the “-name” option to the find command to allow for working only on files with the .js extension:
user@host:~$ find . -type f -name "*.js" -exec sed -i 's/var pageName="example page";/var pageName="Final Page";/g' {} \;
In the next one, the text to find and replace has special characters (in this case the ‘/’ in the URL).
user@host:~$ find . -type f -exec sed -i 's/http:\/\/www.example.com\/old_directory\//http:\/\/newsubdomain.example.com\/new_directory\//g' {} \;
See the following links to read more about escaping special characters:
- Bash Hackers Wiki - Quotes and escaping
- Unix / Linux Shell Scripting Tutorial - Escape Characters
- Advanced Bash-Scripting Guide
Posted by Karl Herrick on February 8th, 2010 in Wordpress, Linux, Hosting, Bash | No Comments »
Switching MySQL Databases on 1and1
After wanting to test Wordpress 3.0 on a 1and1 hosting account, I ran into the following issue… the particular setup I was dealing with had only one database available, and it wasn’t MySQL 5, but instead MySQL 4… furthermore, it was a version lower than 4.1.2, which more recent versions of Wordpress require as a minimum.
So after doing a bit of research on what others had done, I went about fixing the problem. Interestingly enough, 1and1 could have made some money in this situation if they would allow customers to simply purchase another database to add to their packages, but no, they want an entire upgrade to be purchased.
The first step was to backup the website and database. I ssh’ed into the web host:
Then I backed up the database. The command below will dump all of the databases on the host (in this case, only one MySQL 4.x database) into a dated bzip file, in the home directory.
Now to backup the site itself (not only in case disaster struck, but this would get a local copy of the sql dump I just made as well). On a local OS X workstation (or Ubuntu, if that suits your tastes) I ran something similar to the following:
username@localhostname:~$ rsync -avz –exclude="logs" $username@example.com: ~/website_backups/
From there, I was able to log into the 1and1 control panel and delete the existing database. This allowed me to setup a new one, and in particular, choose MySQL 4 or 5 as the type.
Back to the 1and1 hosting account:
I uncompressed the sql dump:
and was greeted with the raw sql in the file, “2010-01-14-01-59-PM-db-backup.sql”. From here it was only a hop skip and a jump away to restoration. It was necessary to edit the sql file in order to have it restore properly to the newly created database that was just created:
And I changed the $oldDatabaseName to $newDatabaseName.
– Current Database: `$oldDatabaseName`
–
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `$oldDatabaseName`;
USE `$oldDatabaseName`;
ctrl+o, enter, and ctrl+x, to save the file and exit nano. The sql dump was now ready to restore to the new database.
The only other things to do were to update any existing applications that needed the new database name, username, hostname, and password.
As a note, if you are updating a Wordpress install to point to a new database, this info can be changed in the file, wp-config.php.
Posted by Karl Herrick on January 14th, 2010 in Wordpress, Backups, Apple, Linux, Hosting, Web Development, Bash | No Comments »
Portfolio
I have gathered some of the better work that I have done over the years, took some screenshots, and posted them to a running instance of MooFlow. Hopefully it can serve as a portfolio for others to view and discuss.
See it at the following url: http://karlherrick.com/portfolio/
Posted by Karl Herrick on October 11th, 2009 in Wordpress, Javascript, PHP, XML, Web Development, Firefox, Hosting | No Comments »
Connecting MySQL with OpenOffice Base
OpenOffice Base makes it incredibly easy to hook up to a MySQL database. Once this is accomplished you can easily create a front end to browse through your data, edit it, report on it, etc. Below are a few steps I took to create a functional setup:
What I did was:
- Have OpenOffice installed
- Access to a database (for this example I used my wp_posts table from Wordpress) from the workstation running OpenOffice
- Downloaded MySQL® Connector/J and installed
- Within OpenOffice clicked, “Tools > Options > OpenOffice.org > Java > Class Path… > Add Archive…”
- Selected “mysql-connector-java-5.1.5-bin.jar” or current version
- Restarted OpenOffice (had to make sure the process was fully “killed”)
- Created a new database within OpenOffice
- Within OpenOffice, clicked “File > New > Database”
- clicked the “Connect to an existing database” radio button
- selected “MySQL” from the drop down list
- clicked the “Next >>” button
- selected the “Connect using JDBC (Java Database Connectivity)” radio button
- clicked the “Next >>” button
- entered the “Name of the database” into the text field
- entered the MySQL “Server URL” into the text field
- clicked the “Test Class” button and received “The JDBC driver was loaded successfully”
- clicked the “Next >>” button
- entered as “User name” into the text field
- checked the “Password required” checkbox
- clicked the “Finish” button and save
- Made a form based on wp_posts:
- This screenshot is of the form I made to browse the raw data of my Wordpress posts from my Thoughts and Notions weblog and is showing data from this post in particular:

Posted by Karl Herrick on February 17th, 2008 in Wordpress, OpenOffice | Comments Off