I'm working on a Custom Post Type that'll function as a landing page. The landing page has several custom fields with meta infromation, theres no problem with that, it works well. But my client wants to insert a facebook tracking code in the header of every LP he'll create, which is a script that looks like this:

<script>(function() { var _fbq = window._fbq || (window._fbq = []); if (!_fbq.loaded) { var fbds = document.createElement('script'); fbds.async = true; fbds.src = '//connect.facebook.net/en_US/fbds.js'; var s = document.getElementsByTagName('script')[0];s.parentNode.insertBefore(fbds, s); _fbq.loaded = true; } _fbq.push(['addPixelId', '1570176543221608']); })(); window._fbq = window._fbq || []; window._fbq.push(['track', 'PixelInitialized', {}]); </script> <noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?id=15701765432&amp;ev=PixelInitialized" /></noscript>   

I created a <textarea> custom field. The text area is showing in the post editor for the landing_page_posts CPT, and it works properly if I fill it with text (it saves the content inside the textarea when the post saves and it displays it if I call get_post_meta($post->ID, "the_textarea_content", true); But if I paste the facebook tracking code script inside the textarea, the textarea goes blank after saving the post, and the script do not appear in the header of the post.

Here's the rest of code I'm using to echo the script in the header of the post, this code is placed in functions.php of a child theme:

function facebook_tracking_code() {    
    if(get_post_type() === "landing_page_posts") {
        global $post;
        echo get_post_meta($post->ID, "facebook-tracking-code", true);
add_action( 'wp_head', 'facebook_tracking_code');

Is there any other way to make this work, and do it properly. I read in other thread that there's a plugin, Advanced Custom Fields, or something like that, but I'll like to do it without that plugin. It is better to learn how to do it properly and then, meybe in some other project, use the plugin if it is neccessary. Thanks in advance.


1 Answer 1


There was two problems with my code. The first one was I didn't use esc_textarea(), but when i used it, the problem didn't go away, so I check for the funcion that stores in database the text given in the text area and I found that; as all the other fields in my CPT are text fields, I was sanitizing them with sanitize_text_field() function, and that was removing the HTML tags. I remove sanitize_text_field() function from the text area variable before using it in update_post_meta(), and now all works.

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.