Search code examples
phpwordpresstextareameta-boxes

Output textarea with wp_head function


I'm trying to echo the value from a Metabox textarea between the head tags of my Wordpress website, so that the site owner can insert page individual scripts.

I've already created my metabox with succes:

<?php
$post_id_post = isset($_POST['post_ID']) ? $_POST['post_ID'] : '' ;
$post_id = isset($_GET['post']) ? $_GET['post'] : $post_id_post ;

if ( ! function_exists( 'onm_head_add_meta' ) ){
    function onm_head_add_meta(){
        $types = array( 'post', 'page', 'courses', 'wiki', 'press' );
        foreach( $types as $type ) {
            add_meta_box('head-metabox-code', __('Scripts', 'onm_textdomain' ), 'onm_sitewide_meta_box', $type, 'normal', 'low'); 
        }
    }
}
add_action("admin_init", "onm_head_add_meta");

//Create area for extra fields
if ( ! function_exists( 'onm_sitewide_meta_box' ) ){
    function onm_sitewide_meta_box( $post ){ 

        $custom = get_post_custom();
        $head_scripts = isset($custom["head_scripts"][0])?$custom["head_scripts"][0]:'';

        // We'll use this nonce field later on when saving.  
        wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
        ?>  

        <textarea name="head_scripts" type="text" class="widefat" cols="50" rows="3"/><?php echo esc_attr($head_scripts); ?></textarea>
        <p><?php _e('The code in the above textfield will be added between the <code>&lt;head&gt;&lt;/head&gt;</code> tags of the website.', 'onm_textdomain' ); ?></p>

        <?php
    }
}

if ( ! function_exists( 'onm_save_page_meta_box' ) ){
    function onm_save_page_meta_box( $post_id ){ 
        // Bail if we're doing an auto save  
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return; 
        }
        // if our nonce isn't there, or we can't verify it, bail 
        if( !isset( $_POST['head_scripts'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) {
            return; 
        }
        // if our current user can't edit this post, bail  
        if( !current_user_can( 'edit_pages' ) ) {
            return;  
        }
        // now we can actually save the data        
        if( isset( $_POST['head_scripts'] ) )  {
            update_post_meta( $post_id, 'head_scripts', esc_attr( $_POST['head_scripts'] ) ); 
        }
    }
    }

add_action( 'save_post', 'onm_save_page_meta_box' ); 

Now I try to output the value from the Metabox between my head tags as follows:

if ( ! function_exists( 'onm_wp_head' ) ){
    function onm_wp_head() {
        $head_scripts_meta = get_post_meta( get_the_ID(), 'head_scripts' , TRUE );
        if ( $head_scripts_meta != '' ) {
            echo $head_scripts_meta, "\n";
        }
    }
}
add_action( 'wp_head', 'onm_wp_head' );

But on my website this results in the output of the Metabox right after my opening body tag (take a look at my example below or this print screen).

<head></head>
<body>
    "<script type="text/javascript">Test</script>"

Not a nice script within my head tags. It's weird because it works like a charm when I test the script as follows:

if ( ! function_exists( 'onm_wp_head' ) ){
    function onm_wp_head() {
        $head_scripts_meta = '<script type="text/javascript">Test</script>';
        if ( $head_scripts_meta != '' ) {
            echo $head_scripts_meta, "\n";
        }
    }
}
add_action( 'wp_head', 'onm_wp_head' );

I think this has to do with the textarea, isn't it? Any ideas how to solve this? Thanks already guys!


Solution

  • I found the solution after a while. I had to stripslashes before the output in the header. So my function became the following and then it worked:

    if ( ! function_exists( 'onm_script_wp_head' ) ){
        function onm_script_wp_head() {
            $head_scripts_meta = stripslashes(get_post_meta( get_the_ID(), 'head_scripts' , TRUE ));
            if ( $head_scripts_meta != '' ) {
                echo $head_scripts_meta, "\n";
            }
        }
    }
    add_action( 'wp_head', 'onm_script_wp_head' );
    

    Thanks for helping guys!