Magento Expert Forum - Improve your Magento experience

Results 1 to 3 of 3

Magento 2: create CMS page programmatically

  1. #1

  2. #2
    Junior Member
    Join Date
    Sep 2018
    Location
    United Kingdom
    Posts
    635
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default

    Follow the below steps to create CMS page programmatically.
    Create UpgradeData.php and define upgrade()
    File : app/code/PHPCodez/First/Setup/UpgradeData.php
    <?php
    namespace Vendor\Module\Setup;
    use Magento\Framework\Setup\UpgradeDataInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\ModuleDataSetupInterface;

    class UpgradeData implements UpgradeDataInterface {

    protected $_pageFactory;


    public function __construct(\Magento\Cms\Model\PageFactory $pageFactory) {
    $this->_pageFactory = $pageFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
    $setup->startSetup();

    if (version_compare($context->getVersion(), '0.1.4') < 0) {
    $page = $this->_pageFactory->create();
    $page->setTitle('PHPCodez CMS page')
    ->setIdentifier('phpcodez-cms-page')
    ->setIsActive(true)
    ->setPageLayout('1column')
    ->setStores(array(0))
    ->setContent('This is a test Page created by PHPCodez. Cheers !!!')
    ->save();
    }

    $setup->endSetup();
    }
    }
    Setup the module version
    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="PHPCodez_First" setup_version="0.1.4"/>
    </config>
    Purge cache and deploy static content.
    php bin/magento cache:flush
    php bin/magento setup:static-content:deploy -f

  3. #3
    Junior Member
    Join Date
    Sep 2018
    Location
    United Kingdom
    Posts
    635
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default

    Creating CMS Page programmatically

    1: Create UpgradeData.php file
    2: Insert UpgradeData class
    3: Setup the module version
    4: Run the upgrade script

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •