I have SQL file with US zip codes (over 41000 lines of code). I want make a plugin that inserts this SQL file into WP database so user can do a search by zip codes. I want to insert SQL with PHP rather than PhpMyAdmin

  • did u get solution for this ? Importing .sql file on plugin active ?
    – Ajay Patel
    Jun 26, 2012 at 7:13

2 Answers 2


See the WordPress Codex on Creating Tables with Plugins.

  • I've seen this, but there is nothing about importing SQL file. Codex just explains how to insert data manually $wpdb->insert
    – alexndm
    Jun 24, 2011 at 22:50
  • 2
    Import into what exactly- your own table, a WordPress table? This is more PHP general- how do you import an SQL file via PHP, the answer to that is potentially complicated depending on your server setup- maybe LOAD DATA INFILE?
    – Milo
    Jun 24, 2011 at 22:58
  • 1
    $newziptable = "CREATE TABLE IF NOT EXISTS " . $table_name . " ( Id int(11) NOT NULL auto_increment, ZipCode varchar(15) , State varchar(10) , City varchar(50) , County varchar(50) , Longitude decimal(10,2) DEFAULT '0.00' , Latitude decimal(10,2) DEFAULT '0.00' , PRIMARY KEY (Id) );"; After I've created new table I want to load all zip codes from my SQL file or just import it entirely
    – alexndm
    Jun 24, 2011 at 23:08
  • 1
    Yes definatly a php question rather than wordpress, have you looked at file_get_contents and LOAD DATA INFILE?
    – MartinJJ
    Jun 25, 2011 at 7:14

I have not created a plugin that does what you want, but I have been playing with a plugin that creates its own tables. The plugin in comes with an SQL.txt file in case your WordPress install does not have permissions to create tables (in which case you would have to manually run the SQL file)

So it seems that a plugin that creates tables could potentially be problematical during install.

Another thing that this same plugin does is use an array from another file to display things. This seems like it would be a better approach because you would not have to mess with people's DB, and you could easily update the file that holds your array of zip codes.

Here is some of the code it uses for doing that:

 //Build new blog_type array item.
    $blog_type_item = array('name' => $name,
                            'nicename' => empty($nice_name) ? strtolower(str_replace(' ', '_', $name)) : $nice_name,
                            'description' => $description,
                            'signup_only' => $signup_only);

    //Append new blog_type array item to global array.
    $blog_types[] = $blog_type_item;

    return $blog_type_item;

                    foreach ($blog_types as $blog_type) {
                        $selected = '';
                        foreach ($selected_blog_types as $selected_blog_type) {
                            if ($selected_blog_type == $blog_type['nicename']){
                                $selected = ' selected="selected"';
                                $selected_blog_type_store = $blog_type['nicename'];
                        echo '<option value="' . $blog_type['nicename'] . '" ' . $selected . '> ' . $blog_type['name'] . '</option>';

Not the answer you're looking for? Browse other questions tagged or ask your own question.