How To Add Custom Button In CMS Page Search Engine Optimization Section In Magento 2

Hello Everyone,

In this blog, we will learn about how to Add Custom Button in the CMS Page in Magento 2.

If you want to Enhance Backend CMS page with custom functionality one requirement is to Add Custom Button In CMS page.

In this blog we add a custom button in the Search Engine Optimization Section of the CMS Page. 

Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Custom Button in Backend CMS Page in Magento 2.

STEPS FOR ADD CUSTOM BUTTON IN CMS PAGE IN MAGENTO 2

Step 1: Create cms_page_form.xml file

app/code/Vendor/Extension/view/adminhtml/ui_component/cms_page_form.xml

<?xml version="1.0" encoding="UTF-8"?>

 <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">

  <fieldset name="search_engine_optimisation" sortOrder="20">

         <field name="meta_title" formElement="input" sortOrder="21">

            <argument name="data" xsi:type="array">

                <item name="config" xsi:type="array">

                    <item name="source" xsi:type="string">page</item>

                </item>

            </argument>

            <settings>

                <dataType>text</dataType>

                <label translate="true">Meta Title</label>

                <dataScope>meta_title</dataScope>

            </settings>

        </field>

            <container name="cms_meta_title_button_container" sortOrder="25">

            <htmlContent name="html_content">

                <argument name="block" xsi:type="object">Vendor\Extension\Block\Adminhtml\Cms\Edit\Cmsbutton</argument>

            </htmlContent>

        </container>

   </fieldset> 

</form>

Step 2: Create Cmsbutton.php file

app/code/Vendor/Extension/Block/Adminhtml/Cms/Edit/Cmsbutton.php

<?php 

namespace Vendor\Extension\Block\Adminhtml\Cms\Edit;

class Cmsbutton extends \Magento\Backend\Block\Template

{

    protected $_template = 'Vendor_Extension::cms/button.phtml';

    public function __construct(

        \Magento\Backend\Block\Template\Context $context,

        array $data = []

    ) {

        parent::__construct($context, $data);

    }

    public function getButtonHtml()

    {

        $button = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')->setData(['id' => 'custom_button', 'label' => __('Save'),'class'=>'action- scalable save primary']);

        return $button->toHtml();

    }

}

Step 3: Create button.phtml file

app/code/Vendor/Extension/view/adminhtml/templates/cms/button.phtml

<div class="admin__field" data-bind="css: $data.additionalClasses, attr: {'data-index': index}, visible: visible">

    <div class="admin__field-control" style="text-align: center;">

        <?php

            echo $block->getButtonHtml();

        ?>

    </div>

</div>

Step 4: Finally run the below commands

$ php bin/magento cache:clean

$ php bin/magento cache:flush

Step 5: Output:

go to Content -> Elements -> Pages -> <select any page> -> Edit

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add Custom Button in Backend CMS Page in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *