0

I have a custom field for a WordPress page to display a subtitle. How do I get the following function to accept HTML code such as elements?

function cms_page_sub_title(){
    global $cms_meta, $post;

    if(!empty($cms_meta->_cms_page_title_sub_text)){
        echo '<div class="page-sub-title">'.esc_attr($cms_meta->_cms_page_title_sub_text).'</div>';
    } elseif (!empty($post->ID) && get_post_meta($post->ID, 'post_subtitle', true)){
        echo '<div class="page-sub-title">'.esc_attr(get_post_meta($post->ID, 'post_subtitle', true)).'</div>';
    }
}

output is

<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12"><?php cms_page_sub_title(); ?></div>

Thanks

5
  • 1
    Can you explain a little more? "How do I get the following function to accept HTML code such as elements?" is a little broad. Maybe provide an example of what you want to happen - do you mean just calling it like cms_page_sub_title('<strong>HTML here</strong>') ?
    – phatskat
    Nov 30, 2015 at 15:30
  • my apologies. That code works fine for outputting on the page. I'm talking about the custom field in the back end of wordpress when I edit the page. I'd like to place: building address, <span class="number">contact number here</span> but when I do the html is outputted also. So on the front end I get <span class="number">contact number</span> when I want the span to just style the contact number
    – sc2015
    Nov 30, 2015 at 15:37
  • The WP custom fields won't allow you to input HTML. You could perhaps write a plugin that provides a custom field input that accepts HTML, but as it stands WP will escape characters in custom fields. If you look in the database at that particular meta value, it will likely look something like `&lt;span class=\"number\"&gt;" from encoding and escaping. There may be a hook somewhere that can override that, I'll dig some and let you know
    – phatskat
    Nov 30, 2015 at 15:44
  • Sorry, it doesn't appear that WordPress has any hooks or filters when adding meta values via the Custom Fields box. Your only solution to allow HTML here would be to either write a plugin to add your own metabox (similar to smashingmagazine.com/2011/10/…) or to create a separate custom field just for that data and wrap it in HTML in the template.
    – phatskat
    Nov 30, 2015 at 16:02
  • Please try to edit some of this conversation into the question body.
    – s_ha_dum
    Nov 30, 2015 at 16:24

1 Answer 1

0

Trying to store markup in the database is generally a bad idea-- some reasons being:

  1. Certain characters need to be escaped to be put in the database
  2. Having markup in the database will negatively effect any searches you may need to run on the data, now or in the future.
  3. Having markup in the database will complicate attempts to reuse the data in different contexts, now or in the future.

Just store the raw data in the database and format it on output. The basic code would look like:

$meta = get_post_meta($post->ID, 'post_subtitle', true);
if (!empty($meta)) {
  $mstr = '<span class="some-class"'.$meta.'</span>';
}

You can automate that by creating a shortcode or by inserting it using the the_content filter, for example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.