baxtian/wp_importer

Class to be inherite to create importers

Maintainers

Package info

bitbucket.org/baxtian/wp_importer

pkg:composer/baxtian/wp_importer

Transparency log

Statistics

Installs: 427

Dependents: 0

Suggesters: 0

0.3.49 2026-07-21 22:50 UTC

README

Base class for creating WordPress importers/exporters (CSV + XLSX). Appears under Tools → Import in the WordPress admin.

Usage

class MyImporter extends \Baxtian\WP_Importer
{
    use \Baxtian\SingletonTrait;

    protected function __construct()
    {
        $this->tipo   = 'product';        // singular slug
        $this->tipos  = 'products';       // plural slug
        $this->accion = 'import_products';// importer action slug

        add_action('init', [$this, 'init']);
        parent::__construct();
    }

    public function init(): void
    {
        $this->campos = [...];  // define here — translations are loaded
        $this->textos = [
            'import_button'       => __('Import Products', 'domain'),
            'import_file'         => __('Products file', 'domain'),
            'plural'              => __('products', 'domain'),
            'description'         => __('Import products from CSV or XLSX.', 'domain'),
            'mensaje_publicacion' => __('Created: %d, Updated: %d, Linked: %d', 'domain'),
            // ↑ Use commas as separators — JS splits by "," to build a bullet list
        ];
        // For custom DB tables, hook import/export here:
        add_filter('MrkImporter/import_data', [$this, 'handle_import'], 10, 4);
        add_filter('MrkImporter/export_data', [$this, 'handle_export'], 10, 4);
        parent::init();
    }
}

Important: define $campos and $textos inside init(), not the constructor — translations must be loaded first. Call parent::init() at the end of init().

Campo types

typedescription
idPost ID — used for matching on import
fieldpost_title or any direct post field
thumbnailFeatured image (URL on export, URL/path on import)
termTaxonomy term (comma-separated on export)
postmetaGeneric postmeta value
postmeta_boolBoolean postmeta — exports as 1/0
postmeta_textareaMultiline postmeta
postmeta_dateDate postmeta (Y-m-d)
postmeta_datetimeDatetime postmeta (Y-m-d H:i:s)
p2pPost-to-post relationship — requires 'p2p' key
ignoreHeader/export mapping only — no-op on import

ignore

Declares a field that participates in rename_columns() (so the file header shows a friendly title) and in export, but is never written by the built-in import switch — it's a no-op on import.

Use it when a column's real value must be set through business logic that can't go through a raw update_post_meta() — for example, a value that has to pass through a domain object, trigger side effects, or be resolved by a third-party listener. Hook into MrkImporter/row_added (fired after all other fields of the row are processed) to write the value yourself:

$this->campos = [
    ['name' => 'id',         'type' => 'id'],
    ['name' => 'post_title', 'type' => 'field'],
    ['name' => 'price',      'type' => 'ignore', 'title' => __('Price', 'domain')],
];

add_action('MrkImporter/row_added', function ($post_id, $tipo, $line) {
    if ($tipo === 'product') {
        MyPricingService::set_price($post_id, $line['price']);
    }
}, 10, 3);

Hooks

HookTypeArgsWhen
MrkImporter/import_datafilter(false, $tipo, $data, $campos)Before importing — return ['nuevas'=>int, 'modificadas'=>int, 'terminos_nuevos'=>int] to replace built-in import; return false to fall through
MrkImporter/export_datafilter($data, $tipo, $tipos, $campos)Before writing the export file — return numeric array of rows (row 0 = header of campo names)
MrkImporter/row_addedaction($post_id, $tipo, $line)After each imported row (built-in importer only)
MrkImporter/attach_imageaction($attach_id, $attach_data)After each image is uploaded and processed

Export format

handle_export must return a numeric array where row 0 is the header (campo name values) and rows 1…N are data rows (numeric arrays aligned with the header). rename_columns() translates names → titles for the downloaded file.

$rows   = [['name', 'email', 'webinars']]; // header
$rows[] = ['John', 'john@example.com', 'Webinar 1'];
return $rows;

XLSX date cells

Cells formatted as dates in Excel are automatically converted to Y-m-d H:i:s strings. No manual serial-number handling needed in the importer.

Access control

WordPress restricts Tools → Import to users with the import capability. Grant it explicitly for custom roles:

add_role('my_role', 'My Role', ['read' => true, 'import' => true]);

Maintainers

Juan Sebastián Echeverry baxtian.echeverry@gmail.com

Changelog

See CHANGELOG.md.