Unleash the Full Potential of Your PrestaShop Store

Welcome to MyPresta.eu – Your reliable partner in the e-commerce world. Find innovative modules that will take your business to a new level.

PrestaShop CMS page meta title tag - new field

Tutorial works with PrestaShop 1.7, 1.6, 1.5

Today I want to show you how to modify your prestashop store to support totally new meta title field to your CMS pages. Unfortunately, by default meta title of CMS pages is based on CMS page name. It's bad, because our meta title is short and it's not good for our seo. In this guide you can read how to change it. I will show you how to create new field, change database and how to implement everything to your shop based on prestashop engine.

 

cms page and new meta title field

cms page prestashop title meta

 

As you can see on image above, in prestashop by default you can find field named "Meta title" - in fact, this is cms page name

you can change it to long title, becase your cms page name will be also long. Weirdy, don't you think?

 

 

Database modification

We have to create something to store our new meta title field in database. In this case we will modify table in your shop database named ps_cms_lang. You can do it in database manager software like phpmyadmin. So, connect to your phpmyadmin script, then open your shop database. Search for table named ps_cms_lang and open its structure (1). Scroll page down and click on "Add" new column field (2). Then create field with credentials like you see for meta_title_new (3) (it's new field!).

 

Modification of ps_cms_lang database

cms table prestashop database modification

 

 

new field specification - create the same field in your ps_cms_lang table

new field creation database phpmyadmin

 

 

Add new field to CMS class

Now it's time to modify class of CMS object. Open file: /classes/CMS.php and add code: 

public $meta_title_new;

to the variables definitions list, like i show below (added line is highlighted)

class CMSCore extends ObjectModel
{
	/** @var string Name */
        public $meta_title_new;
	public $meta_title;
	public $meta_description;
...
...  

 

Now in ObjectModule::$definition section add our new field too, like i added below (added field is highlighted too)

	/**
	 * @see ObjectModel::$definition
	 */
	public static $definition = array(
		'table' => 'cms',
		'primary' => 'id_cms',
		'multilang' => true,
		'fields' => array(
			'id_cms_category' => 	array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
			'position' => 			array('type' => self::TYPE_INT),
			'indexation' =>     	array('type' => self::TYPE_BOOL),
			'active' => 			array('type' => self::TYPE_BOOL),

			// Lang fields
            'meta_title_new' =>		array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
			'meta_description' => 	array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),
			'meta_keywords' => 		array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'size' => 255),

Ok, your CMS object model is ready to handle your new field! But it's not everything that we have to modify. Now It's time to add this field to the back office CMS setting page (see first image).

 

Adding new field to back office

In this case we have to modify AdminCmsController, it's a file located in /controllers/admin/ directory. So, just open this file and add this code:

                array(
					'type' => 'text',
					'label' => $this->l('Meta title new:'),
					'name' => 'meta_title_new',
					'id' => 'name', // for copyMeta2friendlyURL compatibility
					'lang' => true,
					'required' => true,
					'class' => 'copyMeta2friendlyURL',
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'size' => 50
				),

to the $this->field_form variable, exactly the same as i show below (code that i added is highlighted)

		$this->fields_form = array(
			'tinymce' => true,
			'legend' => array(
				'title' => $this->l('CMS Page'),
				'image' => '../img/admin/tab-categories.gif'
			),
			'input' => array(
				// custom template
				array(
					'type' => 'select_category',
					'label' => $this->l('CMS Category'),
					'name' => 'id_cms_category',
					'options' => array(
						'html' => $html_categories,
					),
				),
				array(
					'type' => 'text',
					'label' => $this->l('Meta title:'),
					'name' => 'meta_title',
					'id' => 'name', // for copyMeta2friendlyURL compatibility
					'lang' => true,
					'required' => true,
					'class' => 'copyMeta2friendlyURL',
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'size' => 50
				),
                array(
					'type' => 'text',
					'label' => $this->l('Meta title new:'),
					'name' => 'meta_title_new',
					'id' => 'name', // for copyMeta2friendlyURL compatibility
					'lang' => true,
					'required' => true,
					'class' => 'copyMeta2friendlyURL',
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'size' => 128
				),
				array(
					'type' => 'text',
					'label' => $this->l('Meta description'),
					'name' => 'meta_description',
					'lang' => true,
					'hint' => $this->l('Invalid characters:').' <>;=#{}',
					'size' => 70
				),

After that your new field will appear in your back office  (see first image).

 

Meta class modification (we must use new field for meta_title tag)

Prestashop generates meta tags in class named Meta.php, you can find it in /classes/ directory. So, open this file. We must modify getCmsMetas function. By default, this function with sql query uses "meta_title" field (old one) to create title of CMS page. We must modify it to use our new field that we named it as a "meta_title_new". It's easy, just change function:

	public static function getCmsMetas($id_cms, $id_lang, $page_name)
	{
		$sql = 'SELECT `meta_title`, `meta_description`, `meta_keywords`
				FROM `'._DB_PREFIX_.'cms_lang`
				WHERE id_lang = '.(int)$id_lang.'
					AND id_cms = '.(int)$id_cms;
		if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql))
		{
			$row['meta_title'] = $row['meta_title'].' - '.Configuration::get('PS_SHOP_NAME');
			return Meta::completeMetaTags($row, $row['meta_title']);
		}

		return Meta::getHomeMetas($id_lang, $page_name);
	}

to:

	public static function getCmsMetas($id_cms, $id_lang, $page_name)
	{
		$sql = 'SELECT `meta_title_new`, `meta_description`, `meta_keywords`
				FROM `'._DB_PREFIX_.'cms_lang`
				WHERE id_lang = '.(int)$id_lang.'
					AND id_cms = '.(int)$id_cms;
		if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql))
		{
			$row['meta_title_new'] = $row['meta_title_new'].' - '.Configuration::get('PS_SHOP_NAME');
			return Meta::completeMetaTags($row, $row['meta_title_new']);
		}

		return Meta::getHomeMetas($id_lang, $page_name);
	}

 

 

Your modification is ready! 

cms title modification prestashop

 

Zdjęcie autora: Milosz Myszczuk

Artykuł napisany przez Milosza Myszczuka, eksperta PrestaShop i oficjalnego moderatora społeczności PrestaShop. CEO i założyciel agencji interaktywnej VEKIA. Dowiedz się więcej.

If you like this article, support our work!

Comments