<p>You'll want to use the <a href="https://docs.craftcms.com/api/v4/craft-base-element.html#event-define-sidebar-html" rel="nofollow noreferrer"><code>EVENT_DEFINE_SIDEBAR_HTML</code> event</a> like so:</p>
<pre><code>use craft\elements\Entry;
use craft\events\DefineHtmlEvent;
use craft\helpers\UrlHelper;
use yii\base\Event;

Event::on(
    Entry::class,
    Entry::EVENT_DEFINE_SIDEBAR_HTML,
    function(DefineHtmlEvent $event) {

        /** @var Entry $entry */
        $entry = $event-&gt;sender;

        // Make sure this is the correct section
        if ($entry-&gt;sectionId == 5)
        {
            // Return the button HTML
            $url = UrlHelper::url('some/path/' . $entry-&gt;id);
            $event-&gt;html .= '&lt;a href=&quot;' . $url . '&quot; class=&quot;btn&quot;&gt;My Button!&lt;/a&gt;';
        }

    }
);
</code></pre>

CC BY-SA 4.0
2022-12-15T07:09:37.470Z
2022-12-15T07:09:37.470Z
Jalen Davenport
<p>I'm trying to add a custom button in the sidebar of one of my entries.</p>
<p>I've found that it could be done in Craft 3 with this template hook: <code>cp.entries.edit.details</code> (e.g. <a href="https://craftcms.stackexchange.com/questions/14516/how-to-add-a-custom-button-at-the-right-section-of-an-entry/14526#14526">How to add a custom button at the right section of an entry?</a>)</p>
<p>In the Craft 4 upgrade documentation, it mentions that this hook <a href="https://craftcms.com/docs/4.x/extend/updating-plugins.html#template-hooks" rel="nofollow noreferrer">has been removed</a>. I found these two events -  <code>EVENT_DEFINE_EDITOR_CONTENT</code> and <code>EVENT_DEFINE_ADDITIONAL_BUTTONS</code> - but so far, I haven't been able to return html to my template.</p>
<p>Is there an easy way to do it in Craft 4?</p>

<control-panel><events><hook>
Adding Custom button to CP
# Adding Custom button to CP

(Asked by: username_0 on 2022-12-14)

I'm trying to add a custom button in the sidebar of one of my entries.

I've found that it could be done in Craft 3 with this template hook: `cp.entries.edit.details` (e.g. [How to add a custom button at the right section of an entry?](https://craftcms.stackexchange.com/questions/14516/how-to-add-a-custom-button-at-the-right-section-of-an-entry/14526#14526))

In the Craft 4 upgrade documentation, it mentions that this hook [has been removed](https://craftcms.com/docs/4.x/extend/updating-plugins.html#template-hooks). I found these two events - `EVENT_DEFINE_EDITOR_CONTENT` and `EVENT_DEFINE_ADDITIONAL_BUTTONS` - but so far, I haven't been able to return html to my template.

Is there an easy way to do it in Craft 4?

**Tags:** control-panel, events, hook

---

## 1 answers

### Answer by username_1 on 2022-12-15. Score: 0 | Accepted answer

You'll want to use the EVENT_DEFINE_SIDEBAR_HTML event like so:
use craft\elements\Entry;
use craft\events\DefineHtmlEvent;
use craft\helpers\UrlHelper;
use yii\base\Event;

Event::on(
    Entry::class,
    Entry::EVENT_DEFINE_SIDEBAR_HTML,
    function(DefineHtmlEvent $event) {

        /** @var Entry $entry */
        $entry = $event->sender;

        // Make sure this is the correct section
        if ($entry->sectionId == 5)
        {
            // Return the button HTML
            $url = UrlHelper::url('some/path/' . $entry->id);
            $event->html .= 'My Button!';
        }

    }
);


---

