Magento 2 create CMS page programmatically
Creating a CMS page programmatically in Magento 2 is also a crucial method you need to learn. As soon as creating CMS page successfully, you will have full control of the management of the content on the page.
Creating a CMS page programmatically in Magento 2 is also a crucial method you need to learn. As soon as creating CMS page successfully, you will have full control of the management of the content on the page. In a CMS page, you can edit or update or delete any data as need. With CMS page, it is flexible to intervene into the change of the content. In previous post, we talk about Create a CMS page in Admin, you also can follow the post to create a sample CMS page in backend.
In this topic, you can know how to insert CMS page programmatically into Magento 2 setup script through specific steps:
4 Steps to create CMS Page programmatically
- Step 1: Create UpgradeData.php file
- Step 2: Insert
UpgradeData
class - Step 3: Setup the module version
- Step 4: Run the upgrade script
Step 1: Create UpgradeData.php file
Generate Setup/UpgradeData.php
file in your module.
Step 2: Insert UpgradeData
class
Use UpgradeData
class as the below, then add required model with dependency injection and a code script to create a new CMS page.
<?php
namespace Vendor\Module\Setup;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
/**
* @codeCoverageIgnore
*/
class UpgradeData implements UpgradeDataInterface
{
/**
* @var \Magento\Cms\Model\PageFactory
*/
protected $_pageFactory;
/**
* Construct
*
* @param \Magento\Cms\Model\PageFactory $pageFactory
*/
public function __construct(
\Magento\Cms\Model\PageFactory $pageFactory
) {
$this->_pageFactory = $pageFactory;
}
/**
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
if (version_compare($context->getVersion(), '1.1') < 0) {
$page = $this->_pageFactory->create();
$page->setTitle('Example CMS page')
->setIdentifier('example-cms-page')
->setIsActive(true)
->setPageLayout('1column')
->setStores(array(0))
->setContent('Lorem ipsum dolor sit amet, consectetur adipiscing elit.')
->save();
}
$setup->endSetup();
}
}
Step 3: Setup the module version
Open etc/module.xml
file, you need to set your module version in setup_version
attribute that should be 1.1
Step 4: Run the upgrade script
Finally, run the following database upgrade script, then you will complete the creating new CMS page on your Magento 2 storefront.
bin/magento setup:upgrade
That’s all steps that allow you to create a new CMS page programmatically in Magento 2. Thank you so much for the watching!
Wrap up
That’s all steps to create a new CMS page programmatically in Magento 2. I hope this tutorial is helpful for you. Don’t forget to let me know if you have any questions and share it with your friends if they also need.
Enjoyed the tutorial? Spread it to your friends!
Sam Thomas
CEO and Founder of Mageplaza. Pursueing a simple and healthy lifestyle. A friend, a husband and a dad of two children, a trainer and an influencer wannabe. He is a big fan of sports and travel, also.
Featured Extensions
People also searched for
- magento 2 create cms page programmatically
- magento 2 create page programmatically
- magento 2 create cms block programmatically
- create cms block programmatically magento 2
- magento 2 add cms block programmatically
- how to create cms page in magento 2
- magento 2 create static block programmatically
- magento 2 update cms page programmatically
- magento 2 cms page
- magento 2 update cms block programmatically
- cms page magento 2
- magento 2 add js to cms page
- create cms page in magento 2
- magento cms module
- 2.2.x, 2.3.x, 2.4.x