使用 wp_head 函数输出文本区域
Output textarea with wp_head function
我试图在我的 Wordpress 网站的 head 标签之间回显 Metabox 文本区域的值,以便网站所有者可以插入页面单独的脚本。
我已经成功创建了我的 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><head></head></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' );
现在我尝试在我的 head 标签之间输出 Metabox 的值,如下所示:
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 的输出紧跟在我打开的 body 标签之后(看看我下面的示例或 this 打印屏幕)。
<head></head>
<body>
"<script type="text/javascript">Test</script>"
我的 head 标签中的脚本不是很好。这很奇怪,因为当我按如下方式测试脚本时,它就像一个魅力:
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' );
一段时间后我找到了解决方案。我不得不在 header 中的输出前去掉斜线。所以我的函数变成了以下然后它起作用了:
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' );
感谢大家的帮助!
我试图在我的 Wordpress 网站的 head 标签之间回显 Metabox 文本区域的值,以便网站所有者可以插入页面单独的脚本。
我已经成功创建了我的 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><head></head></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' );
现在我尝试在我的 head 标签之间输出 Metabox 的值,如下所示:
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 的输出紧跟在我打开的 body 标签之后(看看我下面的示例或 this 打印屏幕)。
<head></head>
<body>
"<script type="text/javascript">Test</script>"
我的 head 标签中的脚本不是很好。这很奇怪,因为当我按如下方式测试脚本时,它就像一个魅力:
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' );
一段时间后我找到了解决方案。我不得不在 header 中的输出前去掉斜线。所以我的函数变成了以下然后它起作用了:
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' );
感谢大家的帮助!