Skip to content

badasswp/sql-to-cpt

Repository files navigation

sql-to-cpt

Coverage Status

Import & Convert SQL files to Custom Post Types (CPT).

sql-to-cpt
after-import

Download

Download from WordPress plugin repository.

You can also get the latest version from any of our release tags.

Why SQL to CPT?

This plugin helps you migrate legacy SQL database tables to WordPress' Custom Post Types (CPT). It provides a user-friendly interface that enables users upload an SQL file which is then parsed and converted to a CPT with meta data that is recognisable within WordPress.

If you ever need to migrate a non-WordPress database table into WP, look no further. This is exactly what you need!

Hooks

sqlt_cpt_table_name

This custom hook provides a simple way to filter the name of the custom post type where the table contents that is being imported will be stored.

add_filter( 'sqlt_cpt_table_name', [ $this, 'custom_post_type_name' ] );

public function custom_post_type_name( $table_name ): string {
    if ( 'student' === $table_name ) {
        return 'custom_' . $table_name;
    }
}

Parameters

  • table_name {string} By default this will be the name of the imported SQL table.

sqlt_cpt_table_columns

This custom hook provides a simple way to filter the table columns that is being imported.

add_filter( 'sqlt_cpt_table_columns', [ $this, 'custom_columns' ] );

public function custom_columns( $columns ): array {
    $columns = array_map( '__', $columns );

    return $columns;
}

Parameters

  • columns {string[]} By default this will be a string array of column names parsed from the table that is being imported.

sqlt_cpt_table_rows

This custom hook provides a simple way to filter the table rows that is being imported.

add_filter( 'sqlt_cpt_table_rows', [ $this, 'custom_rows' ] );

public function custom_rows( $rows ): array {
    $rows = array_map( 'santize_text_field', $rows );

    return $rows;
}

Parameters

  • rows {string[]} By default this will be a string array of row values parsed from the table that is being imported.

sqlt_cpt_post_values

This custom hook provides a way to filter the WP post values before import. An e.g is shown below where the post_title is filtered to use the first_name and last_name of the imported worker data.

add_filter( 'sqlt_cpt_post_values', [ $this, 'filter_post_title' ], 10, 2 );

public function filter_post_title( $args, $post_import ): array {
    if ( 'worker' === $args['post_type'] ?? '' ) {
        $args['post_title'] = sprintf(
            '%s %s',
            $post_import['first_name'] ?? '',
            $post_import['last_name'] ?? ''
        );
    }

    return $args;
}

Parameters

  • args {mixed[]} By default this will be an associative array containg the familiar WP Post values (post_type, post_title, meta_input & post_status) to be inserted.
  • post_import {mixed[]} By default this will be an associative array containg the key, value pair of the imported data.

sqlt_cpt_post_labels

This custom hook provides a way to filter the post labels of the CPT that is imported.

add_filter( 'sqlt_cpt_post_labels', [ $this, 'custom_labels' ] );

public function custom_labels( $labels ): array {
    if( 'Students' === $labels['singular_name'] ?? '' ) {
        $labels['singular_name']  = 'Student'
    }

    return $labels;
}

Parameters

  • labels {string[]} By default this will be a string array of containing the label values of the CPT.

sqlt_cpt_post_options

This custom hook provides a way to filter the post options of the CPT that is imported.

add_filter( 'sqlt_cpt_post_options', [ $this, 'custom_options' ] );

public function custom_options( $options ): array {
    $options['show_in_menu'] = false;

    return $options;
}

Parameters

  • options {mixed[]} By default this will be an array containing the post options of the CPT.