Site icon MageComp Blog

How to Create CMS Static Page Programmatically in Magento 2

How to Create CMS Static Page Programmatically in Magento 2

Hello Magento Friends,

Welcome to Magento 2 How To Blog category. Today I will justify How to Create CMS Static Page Programmatically in Magento 2.

CMS Pages helps to increase the visibility of products for customers. With CMS pages you can create rich content and easy management of multiple contents. CMS pages allow inserting images, text, audio, video all in a single page. Magento 2 allows creating CMS Pages where you can edit, add or update the content.

Let’s get started with How to Create CMS Static Page Programmatically in Magento 2.

Steps to Create CMS Static Page Programmatically in Magento 2:

Step 1: Create a cmspage.php file in Magento 2 Root Directory and add the below code.

<?php
use Magento\Framework\AppInterface;

try
{
     require_once __DIR__ . '/app/bootstrap.php';
}
catch (\Exception $e)
{
     echo 'Autoload error: ' . $e->getMessage();
     exit(1);
}
try
{
     $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
     $objectManager = $bootstrap->getObjectManager();
     $appState = $objectManager->get('\Magento\Framework\App\State');
     $appState->setAreaCode('frontend');
     $om = \Magento\Framework\App\ObjectManager::getInstance();
     $storeManager = $om->create('Magento\Cms\Model\PageFactory');

     $pagedata = [
	        'title' => 'Your Title Here',
	        'identifier' => 'page-identifier-here',
	        'stores' => 0,
	        'is_active' => 1,
	        'content_heading' => 'Page Heading Here',
	        'content' => 'page content here',
	        'page_layout' => '1column'
	    ];
    
     $storeManager->create()->setData($pagedata)->save();
     echo "CMS static Page programmatically successfully created";
}
catch(\Exception $e)
{
     print_r($e->getMessage());
}

After adding the above code, run the below link.

https://www.domain.com/cmspage.php

Conclusion:

Hence, now all are able to Create CMS Static Page Programmatically in Magento 2. For any difficulty, mention in the comment part. Make sure to share the solution with your Magento friends and social media. I will be back with another useful tutorial, till then stay updated.

Happy Coding!

Exit mobile version