How to add a custom image field to a category in Magento?

How to add a custom image field to a category in Magento?

July 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add a custom image field to a category in Magento?

Adding Custom Image Fields to Magento Category: A Comprehensive Guide

As a newbie to Magento development, you’ve taken an excellent first step by creating a module and attempting to add custom image fields to your category using the installer file. Let’s dive into this issue and ensure your images are saved correctly in the database.

The Current Issue: Images Not Saving to Database

You have correctly defined the attributes for your additional image fields, including the necessary configuration parameters such as input type, visibility, and user-defined status. However, the problem lies in the execution of these operations within the installer file.

Magento 2 requires a specific way to add attributes using the setup resource model. Your current implementation is missing some critical steps that ensure the data is properly saved into the database.

Step-by-Step Solution: Correctly Adding Custom Image Attributes

To resolve this issue, we need to modify your code by following these best practices:

  1. Use the correct attribute type: For image attributes, you should use 'type' => 'varchar' instead of file, and set the backend_model to a specific model like catalog/category_backend_image.

  2. Set the backend model: The backend_model should point to an existing Magento model that handles the image saving process. This is crucial for correctly storing and retrieving the image data.

  3. Configure the image path: You need to specify the path where the images will be stored within your Magento installation using the upload_dir attribute.

  4. Use the correct resource setup method: Instead of using $installer->startSetup() directly, you should create a new instance of the Magento EAV entity setup model and use its methods for adding attributes and groups.

Here’s an updated version of your code incorporating these changes:

$setup = Mage::getModel('eav/entity_setup', 'core_setup');

$entityTypeId     = $setup->getEntityTypeId('catalog_category');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute(
    'catalog_category',
    'image1',
    array(
        'input' => 'image',
        'type' => 'varchar',
        'group' => 'General',
        'label' => 'Additional Image 1',
        'visible' => 1,
        'required' => 0,
        'user_defined' => 1,
        'frontend_input' => '',
        'global' => Mage::getSingleton('eav/config')->getScopedAttributeConfigByCode('catalog_category', 'image1')->getData('is_global'),
        'visible_on_front' => 1,
        'backend_model' => 'catalog/category_backend_image',
        'upload_dir' => 'media/catalog/category/image1'
    )
);

$setup->addAttributeToGroup(
    $entityTypeId,
    $attributeSetId,
    $attributeGroupId,
    'image1',
    '999'
);

Key Points to Consider

  • Make sure the upload_dir is configured correctly based on your Magento installation’s file structure.

  • The backend_model should always match an existing model in your Magento system that can handle image data storage and retrieval.

  • Pay close attention to the configuration parameters required for each attribute type, which can vary between Magento 1 and Magento 2.

Conclusion

Adding custom image fields to Magento categories requires a careful understanding of the EAV (Entity-Attribute-Value) model used by the platform. By following the corrected code provided above, you should now be able to save your additional image attributes properly within the category entity.

Remember that proper attribute configuration is crucial for ensuring data integrity and optimal performance in your Magento store.

For more detailed information on Magento development best practices, I recommend visiting our official blog at [https://laravelcompany.com/] where we regularly publish comprehensive guides and tutorials for Magento developers of all skill levels.

Magento 2 Catalog Database Extensions

Our network