Do you know you can try any of our premium WordPress themes for free?Show me the themes
Convert hex color to rgb or rgba using PHP

Convert hex color to rgb or rgba using PHP


As far as we know, many of WordPress theme developers use a lot of design options inside their themes, just as we do. Which means that you usually provide color pickers so the users may easily change color settings for their websites.

This usually means that you will need to create dynamic CSS files instead of a hard-coded CSS, so the user’s color changes may apply on the website instantly. As for HTML5/CSS3 standards, hex colors can’t always do all the work so you need to work with RGB or RGBA color codes.

While we have been struggling with this sometimes, we have made a little PHP helper function which converts hex color code string into RGB or even RGBA color. For example, if you want to add opacity to the color of some element  and basically achieve “transparent hex code”.

Top WP Hosting recommended by Meks

Convert hex color to rgba – PHP function

Here’s the code:

So far so good! Now we are going to show you how this function can be helpful whilst creating dynamic CSS with a simple example below.

Usage example:

Bojan Petrovic

Co-founder and WordPress developer at Meks. He has been involved in the business since 2009. creating themes, plugins and services on top of WordPress, continuously aiming to choose best approaches and work smarter, not harder.

Try theme now for free!
  • Access to a copy of the demo website
  • Full access to the theme options
  • No credit card required

* We will send you an occasional newsletter with special offers, news & theme updates but you can unsubscribe at any time.