---
title: "How to create Inceptor or Plugin in Magento 2"
url: "https://dolphinwebsolution.com/how-to-create-inceptor-or-plugin-in-magento-2"
date: "2021-03-19T05:20:42+05:30"
modified: "2023-08-28T05:50:15+05:30"
author:
  name: "Mahesh Makwana"
categories:
  - "Magento"
word_count: 779
reading_time: "4 min read"
summary: "Today we learn how to create Inceptor or plugin in Magento 2.  At development time we need to change some function behavior so Magento provides a plugin for it.

A Plugin or Inceptor allows cha..."
description: "Today we learn how to create Inceptor or plugin in Magento 2.  At development time we need to change some function behavior so Magento provides a plugin for..."
keywords: "create inceptor or plugin in magento 2,create plugin in magento 2,create inceptor in magento 2,create inceptor, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "Remove Products with Duplicate SKU from Magento2"
    url: "https://dolphinwebsolution.com/remove-products-with-duplicate-sku-from-magento2"
  - title: "Region/State is not showing on shipping address box in checkout page"
    url: "https://dolphinwebsolution.com/region-state-is-not-showing-on-shipping-address-box-in-checkout-page"
  - title: "Magento 2 Add UI-select Component with Search Filter in Admin Form"
    url: "https://dolphinwebsolution.com/magento-2-add-ui-select-component-with-search-filter-in-admin-form"
---

# How to create Inceptor or Plugin in Magento 2

_Published: March 19, 2021_  
_Author: Mahesh Makwana_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/Plugin-in-Magento-2-1024x512.jpg)

Today we learn how to create Inceptor or plugin in Magento 2. At development time we need to change some function behavior so Magento provides a plugin for it.

A **Plugin** or **Inceptor** allows change behavior of any public class or method by intercepting a function call and running code. **Plugins are only used for “public methods”**.

### Types Of Plugins In Magento 2
- Before Plugin
- After Plugin
- Around Plugin

#### Why we use Interceptors or [Extension in Magento 2](https://dolphinwebsolution.com/shop/magento-2-extensions.html)?
- we can change the behavior of the same class or method
- The plugin can be called sequentially according to sort order, so conflict with other plugin class can be avoided.
- No issue of rewriting the system
- Customize the same method in different modules.
- Ability to modify the return value of any public method.
- Ability to modify the arguments of any public method.
- Plugins do not conflict with other plugin classes.

#### The Magento 2 Plugins can’t be used with:
- Non-public methods (Protected & Private)
- Static methods
- Final Methods
- Final classes
- Virtual Types
- Any class that has at least one final public method
- Objects that are instantiated before Magento\\Framework\\Interception is bootstrapped
- \_\_construct and \_\_destruct

#### Declaring a plugin
A plugin can be declared in the [di.xml file](https://developer.adobe.com/commerce/php/development/build/dependency-injection-file/) in your module.

**app/code/VendoreName/ModuleName/etc/di.xml**

```
<config>
    <type name="{ObservedType}">
      <plugin name="{pluginName}" type="{PluginClassName}" sortOrder="1" disabled="false" />
    </type>
</config>
```

- **type name**: A class or interface which the plugin observes.
- **plugin name** An arbitrary plugin name that identifies a plugin. Also used to merge the configurations for the plugin.
- **plugin type**: The name of a plugin’s class or its virtual type. Use the following naming convention when you specify this element: \\Vendor\\Module\\Plugin\\<ClassName>.
- **plugin sortOrder:** Plugins that call the same method run them using this order. (Optional)
- **plugin disabled:** To disable a plugin, set this element to true. The default value is false. (Optional)

```
<type name="Dolphin\PluginDemo\Controller\Index\PluginExample">
    <plugin name="demo_plugin" type="Dolphin\PluginDemo\Plugin\PluginDemo" sortOrder="1" disabled="false"  />
</type>
```

For example, the following code defines type name, we created **PluginExample.php** file at **app/code/Dolphin/PluginDemo/Controller/Index/PluginExample.php**

if you don’t know how to create module [click here ](https://dolphinwebsolution.com/how-to-create-module-in-magento-2)

```
<?php

namespace Dolphin\PluginDemo\Controller\Index;

class PluginExample extends \Magento\Framework\App\Action\Action
{
    protected $title;

    public function execute()
    {
        echo $this->setTitle('Welcome Plugin');
        echo $this->getTitle();
    }

    public function setTitle($title)
    {
        return $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }
}
```

Find here for more plugins: [Magento 2 Extension](https://dolphinwebsolution.com/shop/magento-2-extensions.html)

### 1) Before Plugin
- Before plugin are used whenever we need to change the arguments of the method or need to add some behavior before a method is called.
- The first method to run is an observed method.
- Before plugin is called by adding the prefix ‘before’ to the method name and setting the first letter of the original method to capital.
- It lets you modify the parameters that will be used.
- Example

```
<?php

    namespace Dolphin\PluginDemo\Plugin;

    class PluginDemo
    {
        public function beforeSetTitle(\Dolphin\PluginDemo\Controller\Index\PluginExample $subject, $title)
        {
            $title = $title." Before plugin method is calll</br>";
            return [$title];
        }

    }
```

### 2) After Plugin
- After Plugins are used whenever we need to change the arguments of method or need to add some behavior after a method is called.
- Starts running after the observed method is finished
- After Plugin is called by adding the prefix ‘after’ to the method name and setting the first letter of the original method to capital.
- It let you modify the output
- Responsible for editing the results of an observed method in the right way and must have a return value.
- Example

```
<?php

    namespace Dolphin\PluginDemo\Plugin;

    class PluginDemo
    {
        public function afterGetTitle(\Dolphin\PluginDemo\Controller\Index\PluginExample $subject, $result)
        {

            echo "After plugin method call</br>";

            return $result . 'Modify Result';

        }

    }
```

### 3) Around Plugin
- Around Plugin run before and after the observed method.
- Allows overriding a method
- Around Plugin change both the arguments and the returned values of the observed method or add behavior before and after the observed method is called.
- Around listener is called by adding the prefix ‘around’ to the method name and setting the first letter of original method to capital.
- It must have a return value
- Example

```
<?php

    namespace Dolphin\PluginDemo\Plugin;

    class PluginDemo
    {
        public function aroundGetTitle(\Dolphin\PluginDemo\Controller\Index\PluginExample $subject, callable $proceed)
        {
            echo "Around Method Call"
            $result = $proceed();
            echo "Around Method Call over"
            return $result;
        }

    }
```

Plugins are commonly used in [Magento development](https://dolphinwebsolution.com/magento-2-development).

I hope this helps you. For any doubts regarding this topic, please write your doubts in the comments section.


---

_View the original post at: [https://dolphinwebsolution.com/how-to-create-inceptor-or-plugin-in-magento-2](https://dolphinwebsolution.com/how-to-create-inceptor-or-plugin-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 06:54:28 UTC_  
