我正在复制本教程以制作我自己的自定义元盒https://www.sitepoint.com/adding-meta-boxes-post-types-wordpress/。
它工作得很好,但是我需要能够将脚本标记输入到这个元框中,并保存脚本并将其输出到页面上。
当我输入类似于脚本标签或div标签的内容时,它将清除meta字段,而不是在更新页面后保存或输出我输入的内容。
我注意到代码中有以下内容:
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['global_notice'] );
但是即使在我删除了该行之后,它仍然不会在元框中保存或输出我的脚本。
我还需要做些什么来用脚本和标签来完成这个任务吗?这不起作用有什么原因吗?
谢谢。
*编辑
下面是我在插件中用来创建metabox的完整代码和输出metabox内容的短代码。
ID, '_global_notice', true );
echo 'Paste your VBO plugin embed code in the text area below:';
echo '' . esc_attr( $value ) . '';
echo 'Next, copy/paste [vbo-plugin-embed] into the content area where you want the plugin to load on this page.';
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id
*/
function save_global_notice_meta_box_data( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['global_notice_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['global_notice_nonce'], 'global_notice_nonce' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
}
else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['global_notice'] ) ) {
return;
}
// Sanitize user input.
//$my_data = sanitize_text_field( $_POST['global_notice'] );
$my_data = $_POST['global_notice'];
// Update the meta field in the database.
update_post_meta( $post_id, '_global_notice', $my_data );
}
add_action( 'save_post', 'save_global_notice_meta_box_data' );
// Add shortcode [vbo-plugin-embed]
function vbo_embed_sc( $atts ){
global $post;
// retrieve the global notice for the current post
$global_notice = esc_attr( get_post_meta( $post->ID, '_global_notice', true ) );
return $global_notice;
}
add_shortcode( 'vbo-plugin-embed', 'vbo_embed_sc' );
下面是我在https://demo.vbotickets.com/photos/上测试的URL
现在的问题是,我已经获得了要保存在metabox中的script/HTML,但是输出是纯文本,而不是实际运行代码/脚本。也有办法运行吗?
谢谢!
发布于 2018-09-03 07:37:17
使用"esc_attr()“来转义数据,这就是为什么要将其转换为计划文本的原因。如果你用回波,它可能会起作用。但是,将脚本标记保存到数据库中。
我和你一样,仍然在学习的路上,但我会这样做。
保存数据
//$my_data = sanitize_text_field( $_POST['global_notice'] );
//$my_data = $_POST['global_notice'];
$my_data = htmlspecialchars($_POST['global_notice']);
然后输出数据
if(!empty($global_notice)) {
$myvar = htmlspecialchars_decode($global_notice);
return $myvar;
}
如果“返回”不起作用,可以使用"echo“。
希望这能有所帮助。
此外,在创建短代码时,您可能希望了解ob_start()和return_ob_clean();如果您的短代码显示在前端的位置不正确的话。您可以始终使用相同的代码是一个模板文件fyi。
发布于 2018-08-24 00:15:55
您不能只删除行以跳过数据的消毒。这一行是:$my_data = sanitize_text_field( $_POST['global_notice'] );
,如果您删除它,您没有任何发送到$my_data
为了进行测试,尝试将其替换为:$my_data = $_POST['global_notice'];
我建议在一个生活环境中,你可以用某种方式净化它。
https://wordpress.stackexchange.com/questions/312327
复制相似问题