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

 

author milos myszczuk
Article by Milosz Myszczuk PrestaShop expert, official PrestaShop community moderator. PHP developer, specialist in relative and spatial databases management, GIS Analyst, CEO & founder of VEKIA interactive agency. Read more about VEKIA company
If you like my articles and want much more valuable tips, feel free to send me donation
1.4 version 1.4.11 1.6 404 addon admin advertise ahref ajax alpha animation api app application authentication back office backup badge banner basics block bootstrap button cache carrier cart catalog category certificate changelog chat class clear client clip cms code colors columns comments configuration contact container content controller cookie counter country coupon css csv currency customer dashboard database debug default delete delivery desktop developer device disable discount displayNav displayTop download dynamic editor effect empty encrypt engine error exchange exclude export facebook faceshop fade fancoupon fancybox fanpage fatal feature feed field file fix fixed font footer free friendly url front ftp full gallery generate gift global godaddy google google+ gray grid groupon header help hide highlight homefeatured homepage hook hosting hover howto htaccess html html5 ID image import include input instagram installation integration iPhone issue javascript jquery kgb knowhow languages law left likebox link list livingsocial loading log login logo loyality mail mailing maintenance manufacturer marketing marquee mcrypt menu meta mobile modification module movie moving multilanguage multiupload must have mysql news newsletter notification number open graph order override page password performance PHP phpmyadmin picture pinterest plugin popup post prestashop prestashop 1.0 prestashop 1.1 prestashop 1.2 prestashop 1.3 prestashop 1.4 prestashop 1.5 price rules problem product profile promotion proslider purifier quantity query quick tip random rates register reinsurance release reporting reset responsive restore results ribbon rich text right sales search security seo service shadow share shipping shop shopmania slider smarty social networks SQL SSL statistics stock store style subcategory superuser support switcher tab tablet tag tax template text theme tinyMCE tips and tricks tpl tracking translations tree trends trigger tumblr tutorial twitter update upgrade upload variables video visits voucher vulnerability web2print wide widget width window wishlist wysiwyg youtube zip zopim