具有wp_head功能的输出文本区域


Output textarea with wp_head function

我正在尝试在我的Wordpress网站的头标签之间回显来自Metabox文本区域的值,以便网站所有者可以插入页面单个脚本。

我已经用成功创建了我的元框:

<?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' ); 

现在我尝试从我的头标签之间的元框中输出值,如下所示:

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' );

但是在我的网站上,这会导致在我的打开正文标签之后立即输出 Metabox(看看我下面的示例或这个打印屏幕)。

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

在我的头标签中不是一个好的脚本。这很奇怪,因为当我按如下方式测试脚本时,它就像一个魅力:

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' );

我认为这与文本区域有关,不是吗?有什么想法可以解决这个问题吗?已经谢谢大家了!

问题是get_the_ID()在你的脑海中不起作用(在调用the_post之前它不起作用)。

修改如下:

if ( ! function_exists( 'onm_wp_head' ) ){
    function onm_wp_head() {
        global $post;
        $head_scripts_meta = get_post_meta( $post->ID, 'head_scripts' , TRUE );
        if ( $head_scripts_meta != '' ) {
            echo $head_scripts_meta, "'n";
        }
    }
}
add_action( 'wp_head', 'onm_wp_head' );
一段时间

后我找到了解决方案。我不得不在标题中的输出之前去除斜杠。所以我的函数变成了以下内容,然后它起作用了:

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' );

感谢您帮助大家!