780-633-0415

MENU

CLOSE

Toggle navigation

about a
1 minute read

How to Keep Formatted Content with WordPress’ ‘get_post_meta’

Custom fields are a must for any custom WordPress theme that has any level of complexity in layout. But out-of-the-box, WordPress strips formatting from any content that is pulled from custom fields with 'get_post_meta', which makes your life as a WordPress developer harder than it should be. Here's how to fix that problem.

If you're a WordPress developer, creating custom themes from scratch, you've been there before. You need to use custom fields in order to make for an easy content management...

June 17, 2014

If you’re a WordPress developer, creating custom themes from scratch, you’ve been there before. You need to use custom fields in order to make for an easy content management experience on the admin side, but pulling content from custom fields on certain posts and pages into other page templates can be quite a tricky process – and keeping formatting is even harder.

Well, it was harder.

Problem:

WordPress, out-of-the-box, strips formatting on content when pulling from custom fields with ‘get_post_meta’. This isn’t good – custom fields are super handy for complex page layouts.

Solution:

Add the following code to your ‘functions.php’ file. This recreates the default WordPress filters on content.

I presume by this point, you’ve already created and populated your custom fields, but you’re struggling with formatting that content. Take this custom field and save it as a PHP variable, using ‘get_post_meta’. Use this code in your theme’s custom page template file. If you’re unfamiliar with the ‘get_post_meta’ function, get your knowledge on here.