Warning: Parameter 1 to wp_default_scripts() expected to be a reference, value given in /home/forge/masseltech.com/wp-includes/plugin.php on line 600
Jul 9 2010

MySQL Duplicate Function

Sometimes you want the ability to duplicate (clone) entities in mySQL. But duplicating their children can be a huge pain! Here’s how:

/* parent table */
insert into [parent table]
select 0, [field1, field2, field3] from [tablename] where [parent table primary key] = [parent table object ID]

/* child table */
insert into [child table]
select 0, [field1, field2, field3], (select max([parent table primary key]) from [parent table]) from [child table] where [parent table primary key] = [parent table object ID]

important note! make sure that you don’t select the primary key of the table. instead, select 0, and the auto_increment function will automatically figure out the correct primary key

As always, if you have questions, post em in the comments!


Mar 15 2010

Pure PHP Pagination

So, for a while I’ve been looking for a ridiculously simple way to paginate data stored in a table. And while I love PHP, it just doesn’t come with some of the free things I take for granted in .Net

Today, while working on a pretty simple plugin, I wanted to add this functionality, and I didn’t want to waste a bunch of time. I was given a link to this great script: http://www.warkensoft.com/2009/12/paginated-navigation-bar/

It took me about a minute to implement the functionality. I then spent another 5 minutes doing CSS and that was it. Done. I’m not usually one for link sharing, but this made my life much easier. Hopefully it can make yours easier too.

The solution is pure PHP, no javascript required. Some day, I may try to expand upon this to include javascripty goodness, because that’s pretty much the only thing that could make it better


Jan 12 2010

WordPress on Snow Leopard: Unable to Connect to Database

If an “unable to connect to database” comes up while installing WordPress on a Snow Leopard machine, try adding :3306 to the end of the DB_HOST in wp-config.php

Assuming everything else works, this should solve your problem!


Jun 10 2009

WordPress plugin DB access framework

While designing a recent WordPress plug-in for TruthMedia that was designed to follow MVC, I had an awful lot of database work to do, and it needed to all be properly secured, which was a lot of work, and while making a rather large change to the way the database worked, I realized that writing all the db code by hand was crazy inefficient.

So I wrote a class that you can extend with any model class of your own, and it will automatically have create, read, update, and delete functionality with no more work required. It also uses all of the WordPress security features to properly escape everything and prevent injection. Although, if you can crack it I’d love to hear from you!

Click here to download!

This is how it works:

  1. Create a new class (ex. person)
  2. Give that class instance variables (ID, FirstName, LastName, Address, PhoneNumber, PostalCode, HairColour…)
  3. Make sure that you add accessor methods (getters and setters) for the ID (although it’s good practice to have them for all instance variables). Depending on your project, it may be a good idea to run stripslashes() on your string get functions so that they don’t show up with escaping. You may want to run WordPress’ attribute_escape() on setters, however, again, depending on your set up this may be unnecessary.
  4. Write your SQL code to create a table named (wordPress prefix)(plugin prefix)person (ex. wp_wec_person)
  5. Make sure you call your primary Key personID **This is very important, without this, the system breaks down**
  6. If you wish to store variables in this class, you can modify them to be private, and they won’t be written to the database

In your class, extend the class name, so for example:

class person extends wec_db {

var $ID;

var $FirstName;

function __construct($id = null, $autoload = true){

//If we are given an ID
if(!empty($eventID)){

$this->setID($eventID);


if
($autoPopulate){

$this->read();

}

}

}

//getters and setters, and any other methods for this object

}

Setup Note: to keep your plugin from stepping on the toes of others, make sure you change the prefix at the top of the class. Also, changing the name of the class to (plugin prefix)db (example:wec_db) will make sure that it doesn’t interfere with anyone else’s if they’re using this class!

Usage:

Create / Add

$person = new person();

$person->setFirstName(‘Joe’);

$person->setLastName(‘Blow’);

$person->add();

Read

$person = new person($personID);

Update

$person = new person($personID);

–or–

$person = new person($personID, false);

$person->setFirstName(‘Jane’);

$person->update();

Delete

$person = new person($personID, false);

$person->delete();

Post any questions in the comments! I’d love to hear from you!


Mar 16 2009

Using WP_Query in WordPress 2.7

So I was trying for the last week or so to use three loops on a single page, to display categorized pages, and couldn’t figure out why it didn’t work. (I used the plugin page category plus to put pages in categories).

Anyways, if you want pages to be shown in a categorical listing, use post_type=page in the query string. Otherwise it’ll only show posts by default, which didn’t happen in previous versions! Mine looked like this: <?php $sportsQuery = new WP_Query(‘category_name=Sports Pages&post_type=page’); ?>

//Loop goes here…

Hope this helps somebody. I know I’ll be coming back to it when I forget!

For more info see: http://codex.wordpress.org/Template_Tags/query_posts


Mar 16 2009

HTML Special Characters

Any good web designer knows certain characters have to be encoded in HTML.

Here’s a list of pretty much every character and it’s encoding: http://webdesign.about.com/library/bl_htmlcodes.htm

Bonus: Need to use some encoded characters in your PHP script? Use html_entity_decode() to turn them into regular text.