photo credit: erika g. via photopin cc

Remove the wrapping paragraph around a widget in Magento

When using widgets in Magento you’ve probably come across the following problem: Magento wraps it’s widgets in paragraphs. This is fine if you have a widget that is supposed to be an inline element, but when you’re using a widget to include a block-type element, you don’t want that anoying wrapping <p></p>  around your HTML. So how can we fix this?

Update: I also wrote an article on how to do this for Magento 2.

Create a module first

For this article, I assume you already know how to create a Magento Module, and have basic understanding on how to use helpers, rewriting models and using events. If not, I suggest you read articles about that first, because this article will not cover basic module creation.

Regular expressions are your friend

Lucky for us, we can use a simple regular expression to remove the wrapping paragraphs around our widgets:

We can also extend this example to, for example, remove wrapping <div> -elements or empty paragraphs:

Using events to tie it all together

A regular expression like this is great, but we are missing one key ingredient: we need an event to manipulate our CMS content before our widget is rendered. Lucky for us, this can easily be done with an event. Add the following code to your modules’ configuration file:

Next, we add a simple function to our helper class (located at Helper/Data.php ):

And in your observer, put the following function:

That’s it! Your widgets will now no longer be wrapped with those annoying paragraph tags!

Bonus: remove wrapping paragraphs in widgets in static blocks

You might have noticed that the above example applies to CMS pages only. Widgets in your static blocks will still be wrapped with paragraphs. This is because static blocks are rendered in a different way than CMS pages. Fixing this requires rewriting Mage_Cms_Block_Block . So let’s just start there, by adding the following line to your configuration file:

The create a file located at Block/Cms/Block.php  and add the following code:

And that’s it! Since we’ve already created the processContent() -function in our helper, this code will work just fine.
So there you have it! No more wrapping paragraphs in widgets in Magento!


Visitors give this article an average rating of 4.7 out of 5.

How would you rate this article?

5 thoughts on “Remove the wrapping paragraph around a widget in Magento”

  1. Hi Giel, thanks for this. Rewrote this to work with Magento 2. So for anyone that needs it: https://gist.github.com/TommyKolkman/3cfba2c991ebaba0b0f6

    1. Giel Berkers says:

      Thanks Tommy! As a matter of fact, I needed this myself today! 😉

    2. Giel Berkers says:

      I made some adjustments to your gist to remove the wrapping paragraph in static blocks as well: https://gist.github.com/kanduvisla/d010b36639f79641bb76

      Expect a blog post about this topic soon! 😉

  2. Tommy Kolkman says:

    Giel, great work! Your update actually solved a big problem for me. Nice one!

Leave a Reply