0

The Google's limit for the meta description, from some months to today, is around 300 characters.

So, I'd like to increase the limit of 255 chars in Magento 1.9

How can I do it in a clean way? :)

p.s. My question is different from this 'cause I want to know if there is a way to maintain the meta description field on the DB as a numeric field, but only with an increased limit.

  • 7
    Possible duplicate of Magento attribute change backend_type from varchar to text – sv3n Mar 5 '18 at 18:22
  • If you make this change, you also have to change field validation .... see: magento.stackexchange.com/questions/212555/… – sv3n Mar 5 '18 at 18:23
  • My question is not a duplicate. I ask if it's possible to increase the numeric limit, not to change a numeric field into a string one. – Roberto Bianchi Mar 6 '18 at 9:40
  • Meta description is stored in catalog_product_entity_varchar. This table is limited to 255 chars. If you want a longer description, you have change it to catalog_product_entity_text ... IMHO it is a duplicate :P – sv3n Mar 6 '18 at 9:51
1

Update meta description maximum length via upgrade script

Use below code for update maximum length

/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer = $this;

$entityTypeId = $installer->getEntityTypeId('catalog_product');
$installer->updateAttribute($entityTypeId, 'meta_description', 'frontend_class', 'validate-length maximum-length-300');

OR

$installer  = Mage::getResourceModel('catalog/setup', 'catalog_setup');
$installer->startSetup();

$entityTypeId = $installer->getEntityTypeId('catalog_product');
$installer->updateAttribute($entityTypeId, 'meta_description', 'frontend_class', 'validate-length maximum-length-300');

$installer->endSetup();

By Default magento1 set maximum length: 255

Please see here defualt code app\code\core\Mage\Catalog\sql\catalog_setup\mysql4-upgrade-1.4.0.0.39-1.4.0.0.40.php

  • Hello and thanks for your response. So, to use this code I've to create the same .php file under app/code/local... Is correct? – Roberto Bianchi Mar 6 '18 at 16:23
0

This is what worked for me. Firstly, run the following code to change the attributes from varchar to text and move them into the right tables. Make sure you back up your website and database first!

START TRANSACTION;
SET @attribute_id = (SELECT attribute_id FROM rds_eav_attribute WHERE entity_type_id = 4 AND attribute_code = "meta_description");
UPDATE rds_eav_attribute SET frontend_class = 'validate-length maximum-length-XXX' WHERE attribute_code = 'meta_description';
UPDATE rds_eav_attribute SET backend_type = 'text' WHERE attribute_id = @attribute_id;
UPDATE rds_eav_attribute SET note = 'Maximum XXX chars' WHERE attribute_code = 'meta_description';
INSERT INTO rds_catalog_product_entity_text SELECT * FROM rds_catalog_product_entity_varchar WHERE attribute_id = @attribute_id;
DELETE FROM rds_catalog_product_entity_varchar where attribute_id = @attribute_id;
COMMIT;

Because it was the meta description I'm changing, I also had to change app\code\core\Mage\Adminhtml\Block\Catalog\Product\Edit\Tab\Attributes.php lines 117-119 from:

if ($form->getElement('meta_description')) {
    $form->getElement('meta_description')->setOnkeyup('checkMaxLength(this, 255);');
}

to:

if ($form->getElement('meta_description')) {
    $form->getElement('meta_description');
}

Instead of hacking the core file though, I'd copy the file to app\code\local\Mage\Adminhtml\Block\Catalog\Product\Edit\Tab\Attributes.php (although even this isn't recommended, but it's a workaround for the time being. Hopefully we'll see this changed in the Magento core at a later date.

  • Hi Liam and thanks for your response :) So there is no way to do this operation without to change the DB I see... – Roberto Bianchi Jun 5 '18 at 11:28
  • The database definitely needs changing unfortunately because the varchar type is set in the database that needs changing to text, and the current records need moving to the text table to prevent any database discrepancies. It's a pain, unfortunately! – Liam McArthur Jun 5 '18 at 11:39
0

@Abdul is right.

You can also update the note bellow the field.

<?php

/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */
$installer = $this;

$entityTypeId = $installer->getEntityTypeId('catalog_product');
$installer->updateAttribute($entityTypeId, 'meta_description', 'frontend_class', 'validate-length maximum-length-300');
$installer->updateAttribute($entityTypeId, 'meta_description', 'note', 'Maximum 300 chars');

And you also need an observer to change the front side validation.

/**
 * @var $observer Varien_Event_Observer
 */
public function updateMetaDescription(Varien_Event_Observer $observer)
{
    /**
     * @var $form Varien_Data_Form
     */
    $form = $observer->getForm();
    $metaDescription = $form->getElement('meta_description');
    if($metaDescription){
        $metaDescription->setOnkeyup('checkMaxLength(this, 300);');
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.