Docs>Importer>Code Snippets

How to register a custom importer template

The following code shows you how to create a very basic importer template that allows you to import to a custom post type, in this example the custom post type is named ‘audio’

<?php

add_action('iwp/register_events', function ($event_handler) {

    // Make sure the PostTemplate class exists
    if (!class_exists('\ImportWP\Common\Importer\Template\PostTemplate')) {
        return;
    }

    class ImportWP_Audio_CPT_Template extends \ImportWP\Common\Importer\Template\PostTemplate
    {
        protected $name = 'Audio';

        public function __construct(\ImportWP\EventHandler $event_handler)
        {
            parent::__construct($event_handler);

            $this->default_template_options['post_type'] = 'audio';
        }
    }

    function register_audio_cpt_template($templates)
    {
        $templates['audio-cpt-template'] = ImportWP_Audio_CPT_Template::class;
        return $templates;
    }

    $event_handler->listen('templates.register', 'register_audio_cpt_template');
});