### Run WordPress to Craft CMS Import (CLI) Source: https://context7.com/craftcms/wp-import/llms.txt Execute the WordPress to Craft CMS import process using the command line. Supports interactive prompts for URL and credentials, or non-interactive setup with provided details. ```bash # Interactive import with prompts for WordPress URL and credentials ddev craft wp-import # Non-interactive with credentials ddev craft wp-import \ --apiUrl="https://example.com" \ --username="admin" \ --password="xxxx xxxx xxxx xxxx xxxx xxxx" ``` -------------------------------- ### Basic Importer Structure for Products in PHP Source: https://context7.com/craftcms/wp-import/llms.txt Defines the basic structure for importing WordPress 'product' post types into Craft CMS. It specifies the API endpoint, element type, query parameters, and how to populate Craft entry fields from WordPress data. It also includes logic for finding existing entries and preparing the Craft environment before import. ```php // config/wp-import/importers/Product.php namespace config\wpimport\importers; use Craft; use craft\base\ElementInterface; use craft\elements\Entry; use craft\wpimport\BaseImporter; class Product extends BaseImporter { public function slug(): string { return 'product'; // WordPress post type slug } public function apiUri(): string { return 'wp/v2/product'; // WordPress REST API endpoint } public function label(): string { return 'Products'; // Display name } public function elementType(): string { return Entry::class; // Craft element type } public function queryParams(): array { return [ 'status' => 'publish,draft', 'orderby' => 'date', 'order' => 'desc', ]; } public function populate(ElementInterface $element, array $data): void { /** @var Entry $element */ $element->title = $data['title']['rendered']; $element->slug = $data['slug']; // Import related media if (!empty($data['featured_media'])) { $assetId = $this->command->import('media', $data['featured_media']); $element->setFieldValue('featuredImage', [$assetId]); } // Import ACF fields if present if (!empty($data['acf'])) { $acfFields = $this->command->fieldsForEntity('post_type', 'product'); $values = $this->command->prepareAcfFieldValues($acfFields, $data['acf']); $element->setFieldValues($values); } // Set post date if (!empty($data['date_gmt'])) { $element->postDate = new \DateTime($data['date_gmt']); } } public function find(array $data): ?ElementInterface { // Return existing element to update instead of creating duplicate return Entry::find() ->section('products') ->slug($data['slug']) ->one(); } public function prep(): void { // Create section/fields before import starts $sectionsService = Craft::$app->getSections(); if (!$sectionsService->getSectionByHandle('products')) { // Create products section... } } } ``` -------------------------------- ### Run WP Import CLI Command Source: https://github.com/craftcms/wp-import/blob/main/README.md Initiates the WordPress content import process via the Craft CMS CLI. Prompts for WordPress site URL and credentials. Can be extended with options for specific item types, IDs, and update behavior. ```sh ddev craft wp-import ddev craft wp-import --type=post --item-id=123,789 ddev craft wp-import --type=post --item-id=123 --update ddev craft wp-import --help ``` -------------------------------- ### Advanced WordPress Import Options (CLI) Source: https://context7.com/craftcms/wp-import/llms.txt Utilize advanced options for the WordPress to Craft CMS import CLI tool, including dry runs for testing, failing fast on errors, and controlling pagination. ```bash # Dry run (rollback at end) ddev craft wp-import --dry-run # Fail on first error instead of continuing ddev craft wp-import --fail-fast # Pagination control ddev craft wp-import --type=post --page=2 --per-page=50 ``` -------------------------------- ### Register Custom Components Source: https://context7.com/craftcms/wp-import/llms.txt This section details how to register custom importers, block transformers, and ACF adapters by listening to specific events within the WP Import command. ```APIDOC ## Register Custom Components via Events ### Description Register custom importers, block transformers, and ACF adapters by subscribing to events dispatched by the `craft\wpimport\Command` class. ### Method PHP Event Listener ### Endpoint N/A (Configuration) ### Parameters N/A ### Request Example ```php // config/app.php or plugin/module use craft\events\RegisterComponentTypesEvent; use craft\wpimport\Command; use yii\base\Event; if (class_exists(Command::class)) { // Register custom importer Event::on( Command::class, Command::EVENT_REGISTER_IMPORTERS, function(RegisterComponentTypesEvent $event) { $event->types[] = \config\wpimport\importers\Product::class; $event->types[] = \config\wpimport\importers\Review::class; } ); // Register custom block transformer Event::on( Command::class, Command::EVENT_REGISTER_BLOCK_TRANSFORMERS, function(RegisterComponentTypesEvent $event) { $event->types[] = \config\wpimport\blocktransformers\CustomCallout::class; $event->types[] = \config\wpimport\blocktransformers\VideoEmbed::class; } ); // Register custom ACF adapter Event::on( Command::class, Command::EVENT_REGISTER_ACF_ADAPTERS, function(RegisterComponentTypesEvent $event) { $event->types[] = \config\wpimport\acfadapters\StarRating::class; $event->types[] = \config\wpimport\acfadapters\PostRelationship::class; } ); } ``` ### Response N/A ``` -------------------------------- ### Configure DDEV for Cross-Project Communication Source: https://github.com/craftcms/wp-import/blob/main/README.md Enables communication between a Craft CMS project and a locally hosted WordPress project using DDEV. This involves creating a specific docker-compose file to link the router services. ```yaml services: web: external_links: - "ddev-router:my-wordpress-project.ddev.site" ``` -------------------------------- ### Import WordPress Media and Assets using WP Import Command Source: https://context7.com/craftcms/wp-import/llms.txt Import WordPress media (attachments) into Craft CMS assets. The command handles downloading the file, preserving alt text, captions, descriptions, and upload dates, and linking the uploader to an imported Craft user. ```php // Import WordPress media/attachment $assetId = $this->command->import('media', 999); // Creates Craft asset with: // - Downloaded file in uploads volume // - Alt text from WP // - Caption and description fields // - Original upload date preserved // - Uploader linked to imported user ``` -------------------------------- ### Import Specific Content Types (CLI) Source: https://context7.com/craftcms/wp-import/llms.txt Import specific content types from WordPress to Craft CMS via the CLI. Allows importing single or multiple types, specific items by ID, and updating existing content. ```bash # Import only posts ddev craft wp-import --type=post # Import multiple specific types ddev craft wp-import --type=post,page,media # Import specific items by ID ddev craft wp-import --type=post --item-id=123,456,789 # Update existing content (re-import) ddev craft wp-import --type=post --item-id=123 --update ``` -------------------------------- ### Built-in Block Transformers Reference Source: https://context7.com/craftcms/wp-import/llms.txt This section lists the built-in block transformers available in the WP Import plugin for parsing and transforming WordPress block content. ```APIDOC ## Built-in Block Transformers Reference ### Description This section details the available block transformers that process WordPress Gutenberg blocks into a structured format suitable for Craft CMS. ### Method N/A (Internal processing) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response N/A *(Note: Specific block transformers are typically registered via events, similar to custom importers. Refer to the custom component registration section for details on extending this functionality.)* ``` -------------------------------- ### Built-in Importers Reference Source: https://context7.com/craftcms/wp-import/llms.txt This section provides reference for the built-in importers available in the WP Import plugin for various WordPress content types. ```APIDOC ## Built-in Importers Reference ### Posts and Pages Import WordPress posts, pages, and custom post types using the `import` command. #### Example Usage: ```php // Imports WordPress posts (default post type) $this->command->import('post', 123); // Imports WordPress pages $this->command->import('page', 456); // Custom post types auto-discovered from REST API $this->command->import('product', 789); // Custom post type slug $this->command->import('portfolio', 101); ``` ``` ```APIDOC ### Media and Assets Import WordPress media and attachments, creating Craft assets with associated metadata. #### Example Usage: ```php // Import WordPress media/attachment $assetId = $this->command->import('media', 999); ``` #### Resulting Craft Asset: - Downloaded file stored in the uploads volume. - Alt text populated from WordPress. - Caption and description fields mapped. - Original upload date preserved. - Uploader linked to the imported Craft user. ``` ```APIDOC ### Users Import WordPress users into Craft CMS, mapping roles and preserving registration dates. #### Example Usage: ```php // Import WordPress user $userId = $this->command->import('user', 5); ``` #### Resulting Craft User: - Username, email, and name are imported. - Roles are mapped (e.g., WordPress 'Administrator' to Craft 'Admin'). - Registration date is preserved. - Users will be prompted to reset their password on first login. ``` ```APIDOC ### Taxonomies Import terms from WordPress taxonomies, including built-in and custom ones. #### Example Usage: ```php // Import category term $categoryId = $this->command->import('category', 12); // Import tag $tagId = $this->command->import('post_tag', 34); // Custom taxonomies $termId = $this->command->import('product_category', 56); ``` ``` ```APIDOC ### Comments Import WordPress comments, with support for threaded comments and integration with the Verbb Comments plugin. #### Example Usage: ```php // Import comment (requires Verbb Comments plugin) $commentId = $this->command->import('comment', 789); ``` #### Resulting Craft Comment: - Comment text is imported. - Author is linked to an imported user or set as guest. - Threaded comments are supported (parent comments are mapped). - Comment date and status are preserved. ``` -------------------------------- ### WordPress REST API Extensions Source: https://context7.com/craftcms/wp-import/llms.txt This section outlines the custom REST API endpoints provided by the WP Import Helper plugin to fetch data from WordPress. ```APIDOC ## WordPress Helper Plugin API ### Description Extends the WordPress REST API with endpoints designed for use with the Craft CMS WP Import plugin. ### Method GET ### Endpoint `/wp-json/craftcms/v1/ping` ### Parameters None ### Request Example None ### Response #### Success Response (200) - **pong** (boolean) - Indicates if the API is responsive. ``` ```APIDOC ### Method GET ### Endpoint `/wp-json/craftcms/v1/info` ### Description Retrieves ACF field group configurations and site settings like permalink structure, color palette, and WYSIWYG toolbars. ### Parameters Requires admin authentication. ### Request Example `Authorization: Basic base64(username:app_password)` ### Response #### Success Response (200) - **field_groups** (array) - An array of ACF field group objects, each containing `key`, `title`, `location`, and `fields`. - **permalink_structure** (string) - The permalink structure of the WordPress site. - **color_palette** (array) - The color palette configuration. - **wysiwyg_toolbars** (object) - The WYSIWYG editor toolbar configuration. ``` ```APIDOC ### Method GET ### Endpoint `/wp-json/craftcms/v1/post/{id}` ### Description Fetches a specific WordPress post by its ID, returning the full post object with all its fields, regardless of post type. ### Parameters - **id** (integer) - Required - The ID of the post to retrieve. Requires admin authentication. ### Request Example `Authorization: Basic base64(username:app_password)` ### Response #### Success Response (200) - (object) - The full WordPress post object, including all its fields. ``` ```APIDOC ### Method GET ### Endpoint `/wp/v2/posts/{id}?context=edit` ### Description Retrieves a post with additional parsed block content and ACF data, typically used for editing purposes. ### Parameters - **id** (integer) - Required - The ID of the post to retrieve. ### Request Example None ### Response #### Success Response (200) - **id** (integer) - The post ID. - **content** (object) - Contains `raw` and `rendered` versions of the post content. - **content_parsed** (array) - An array of parsed block objects (added by the helper plugin). - **acf** (object) - Contains Advanced Custom Fields data if ACF is enabled. ``` -------------------------------- ### Register Custom Components via Events in Craft CMS Source: https://context7.com/craftcms/wp-import/llms.txt Register custom importers, block transformers, and ACF adapters in Craft CMS by listening to specific events dispatched by the WP Import Command class. This allows for extending the import functionality with custom logic. ```php // config/app.php or plugin/module use crafteventsRegisterComponentTypesEvent; use craftwpimportCommand; use yiiaseEvent; if (class_exists(Command::class)) { // Register custom importer Event::on( Command::class, Command::EVENT_REGISTER_IMPORTERS, function(RegisterComponentTypesEvent $event) { $event->types[] = \config\wpimport\importers\Product::class; $event->types[] = \config\wpimport\importers\Review::class; } ); // Register custom block transformer Event::on( Command::class, Command::EVENT_REGISTER_BLOCK_TRANSFORMERS, function(RegisterComponentTypesEvent $event) { $event->types[] = \config\wpimport\blocktransformers\CustomCallout::class; $event->types[] = \config\wpimport\blocktransformers\VideoEmbed::class; } ); // Register custom ACF adapter Event::on( Command::class, Command::EVENT_REGISTER_ACF_ADAPTERS, function(RegisterComponentTypesEvent $event) { $event->types[] = \config\wpimport\acfadapters\StarRating::class; $event->types[] = \config\wpimport\acfadapters\PostRelationship::class; } ); } ``` -------------------------------- ### Render Gutenberg Blocks to HTML (PHP) Source: https://context7.com/craftcms/wp-import/llms.txt Convert WordPress Gutenberg blocks into HTML compatible with CKEditor using the wp-import API. Handles various block types and nested structures. ```php use craft\elements\Entry; $entry = Entry::find()->one(); $wpContent = [ [ 'blockName' => 'core/paragraph', 'attrs' => [], 'innerHTML' => '
Hello world
', 'innerBlocks' => [], ], [ 'blockName' => 'core/image', 'attrs' => ['id' => 789], 'innerHTML' => 'Hello world
..."}, // "content_parsed": [ // Added by helper plugin // {"blockName": "core/paragraph", "attrs": {}, "innerHTML": "...", "innerBlocks": []} // ], // "acf": {...} // If ACF enabled // } ``` -------------------------------- ### Import WordPress Posts and Pages using WP Import Command Source: https://context7.com/craftcms/wp-import/llms.txt Utilize the `import` command within the WP Import plugin to import various WordPress content types. This includes default posts, pages, and custom post types discovered via the REST API. Each import action returns an ID representing the created Craft CMS entry. ```php // Imports WordPress posts (default post type) $this->command->import('post', 123); // Imports WordPress pages $this->command->import('page', 456); // Custom post types auto-discovered from REST API $this->command->import('product', 789); // Custom post type slug $this->command->import('portfolio', 101); ``` -------------------------------- ### Transform Accordion Block with Inner Blocks - PHP Source: https://context7.com/craftcms/wp-import/llms.txt Transforms a custom 'accordion' Gutenberg block into a Craft CMS HTML structure. It iterates through inner 'accordion-item' blocks, extracting their titles and rendering their content. Dependencies include the `craftelementsEntry` and `craftwpimportBaseBlockTransformer` classes. The input is an array of block data and a Craft CMS Entry object. The output is an HTML string. ```php namespace config\wpimport\blocktransformers; use craft\elements\Entry; use craft\wpimport\BaseBlockTransformer; class Accordion extends BaseBlockTransformer { public static function blockName(): string { return 'custom/accordion'; } public function render(array $data, Entry $entry): string { $html = '