Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DOC

Create CMS Page By Installer In Magento 2

Create CMS Page By Installer In Magento 2 – Here I’m going to explain you how you can create cms page by patch data installer in magento2.
First of all create a “AddCustomCmsPage.php” file on location Vendor\Module\Setup\Patch\Data then use below code snippet.

<?php

namespace Vendor\Module\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Cms\Model\PageFactory;

class AddCustomCmsPage implements DataPatchInterface
{

    /**
     * @param PageFactory $pageFactory
     */
    public function __construct(
        PageFactory $pageFactory
    ) {
        $this->pageFactory = $pageFactory;
    }

    /**
     * @inheritdoc
     */
    public function apply()
    {
        $cmsPageData = [
            'title' => 'Custom cms page', // cms page title
            'page_layout' => '1column', // cms page layout
            'meta_keywords' => 'Page keywords', // cms page meta keywords
            'meta_description' => 'Page description', // cms page description
            'identifier' => 'custom-page', // cms page url identifier
            'content_heading' => 'Custom cms page', // Page heading
            'content' => "<h1>Write your custom cms page content.......</h1>", // page content
            'is_active' => 1, // define active status
            'stores' => [0], // assign to stores
            'sort_order' => 0 // page sort order
        ];
        // create page
        $this->pageFactory->create()->setData($cmsPageData)->save();
    }

    /**
     * @inheritdoc
     */
    public static function getDependencies()
    {
        return [];
    }

    /**
     * @inheritdoc
     */
    public function getAliases()
    {
        return [];
    }
}

In this way we can create CMS page programmatically by installer file.Thanks..

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • Vikas mehar
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home