I need any query or code to create CMS page programmatically, Please help me or suggest me if any module available for the same.
Thanks :)
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI need any query or code to create CMS page programmatically, Please help me or suggest me if any module available for the same.
Thanks :)
Create CMS Page By Installer In Magento 2 – Before you start you must have knowledge of Magento 2 custom module creation, So create your custom module first.
Here I’m going to explain you how you can create CMS page by installer in magento 2. First of all create a “InstallData.php” file on location Vendor\Module\Setup then use below code.
<?php
namespace Kodefolks\Cmsinstaller\Setup;
use Magento\Cms\Model\PageFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
class InstallData implements InstallDataInterface
{
private $pageFactory;
private $blockFactory;
public function __construct(PageFactory $pageFactory)
{
$this->pageFactory = $pageFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$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();
}
}
In this way we can create CMS page programmatically by installer file.
Thanks :)
save()
method has been deprecated in version 2.3.4
you have to use the BlockRepository
instead of the PageFactory
.
Mar 3, 2020 at 12:09
Try this
<?php
...
private $pageFactory;
public function __construct(
\Magento\Cms\Model\PageFactory $pageFactory
)
{
$this->pageFactory = $pageFactory;
}
...
...
$testPage = [
'title' => 'Test page title',
'identifier' => 'test-page',
'stores' => [0],
'is_active' => 1,
'content_heading' => 'Test page heading',
'content' => 'Test page content',
'page_layout' => '1column'
];
$this->pageFactory->create()->setData($testPage)->save();