How To

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!

Click to rate this post!
[Total: 8 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

20 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago