admin管理员组文章数量:1290978
I have a created a custom meta box plugin and a function in the functions.php file to output the value on the page.
The meta box seems to be working, however the input text is not being displayed on left side of the image, also when I inspect the element in the browser, the h3 tag is empty.
This is the functions.php file:
// Custom banner feature display in home page
function carolinaspa_banner()
{ ?>
<div class="banner">
<div class="col-4">
<h3><?php homepage_fields_get_meta('homepage_fields_banner_text'); ?></h3>
</div>
</div>
<?php
}
add_action('homepage', 'carolinaspa_banner', 80);
This is the meta box code plugin code:
function homepage_fields_get_meta($value)
{
global $post;
$field = get_post_meta($post->ID, $value, true);
if (!empty($field)) {
return is_array($field) ? stripslashes_deep($field) : stripslashes(wp_kses_decode_entities($field));
} else {
return false;
}
}
function homepage_fields_add_meta_box()
{
add_meta_box(
'homepage_fields-homepage-fields',
'homepage_fields',
'homepage_fields_html',
'page',
'normal',
'high'
);
}
add_action('add_meta_boxes', 'homepage_fields_add_meta_box');
function homepage_fields_html($post)
{
wp_nonce_field('_homepage_fields_nonce', 'homepage_fields_nonce'); ?>
<p>
<label for="homepage_fields_banner_text"><?php _e('Banner Text'); ?></label><br>
<input type="text" name="homepage_fields_banner_text" id="homepage_fields_banner_text" value="<?php echo homepage_fields_get_meta('homepage_fields_banner_text'); ?>" size="30" />
</p><?php
}
function homepage_fields_save($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!isset($_POST['homepage_fields_nonce']) || !wp_verify_nonce($_POST['homepage_fields_nonce'], '_homepage_fields_nonce')) return;
if (!current_user_can('edit_post', $post_id)) return;
if (isset($_POST['homepage_fields_banner_text']))
update_post_meta($post_id, 'homepage_fields_banner_text', esc_attr($_POST['homepage_fields_banner_text']));
}
add_action('save_post', 'homepage_fields_save');
I feel I have done everything correct, but I may be missing something?
I have tried working this out, but its proving to difficult. Any help will be appreciated. Thanks.
I have a created a custom meta box plugin and a function in the functions.php file to output the value on the page.
The meta box seems to be working, however the input text is not being displayed on left side of the image, also when I inspect the element in the browser, the h3 tag is empty.
This is the functions.php file:
// Custom banner feature display in home page
function carolinaspa_banner()
{ ?>
<div class="banner">
<div class="col-4">
<h3><?php homepage_fields_get_meta('homepage_fields_banner_text'); ?></h3>
</div>
</div>
<?php
}
add_action('homepage', 'carolinaspa_banner', 80);
This is the meta box code plugin code:
function homepage_fields_get_meta($value)
{
global $post;
$field = get_post_meta($post->ID, $value, true);
if (!empty($field)) {
return is_array($field) ? stripslashes_deep($field) : stripslashes(wp_kses_decode_entities($field));
} else {
return false;
}
}
function homepage_fields_add_meta_box()
{
add_meta_box(
'homepage_fields-homepage-fields',
'homepage_fields',
'homepage_fields_html',
'page',
'normal',
'high'
);
}
add_action('add_meta_boxes', 'homepage_fields_add_meta_box');
function homepage_fields_html($post)
{
wp_nonce_field('_homepage_fields_nonce', 'homepage_fields_nonce'); ?>
<p>
<label for="homepage_fields_banner_text"><?php _e('Banner Text'); ?></label><br>
<input type="text" name="homepage_fields_banner_text" id="homepage_fields_banner_text" value="<?php echo homepage_fields_get_meta('homepage_fields_banner_text'); ?>" size="30" />
</p><?php
}
function homepage_fields_save($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!isset($_POST['homepage_fields_nonce']) || !wp_verify_nonce($_POST['homepage_fields_nonce'], '_homepage_fields_nonce')) return;
if (!current_user_can('edit_post', $post_id)) return;
if (isset($_POST['homepage_fields_banner_text']))
update_post_meta($post_id, 'homepage_fields_banner_text', esc_attr($_POST['homepage_fields_banner_text']));
}
add_action('save_post', 'homepage_fields_save');
I feel I have done everything correct, but I may be missing something?
I have tried working this out, but its proving to difficult. Any help will be appreciated. Thanks.
Share Improve this question asked Jan 28, 2020 at 5:15 KrysKrys 1353 silver badges9 bronze badges 3 |1 Answer
Reset to default 0I see that you guys might have solved this issue in the comments, but others might end up here, looking for an answer, so let's give it to them! :)
As pointed out by Michael, the problem here is that you didn't echo out the content. But there are also some other issues that should be fixed.
Whenever you are outputting or saving anything in WordPress, make sure you escape it. This is one of the parts of the WordPress Coding Standards, a set of rules made to keep a level of quality and consistency in your code.
In your functions.php
, change this:
<h3><?php homepage_fields_get_meta('homepage_fields_banner_text'); ?></h3>
to this:
<h3><?php echo esc_html(homepage_fields_get_meta('homepage_fields_banner_text')); ?></h3>
The function esc_html
escapes a string from any bad characters, so that you can use it in html without anything breaking.
In the metabox plugin code, replace
_e( 'Banner Text' );
With
esc_html_e( 'Banner Text' );
The function esc_html_e
displays translatable strings, escaped, safe to use in html.
On the row below
<input type="text" name="homepage_fields_banner_text" id="homepage_fields_banner_text" value="<?php echo homepage_fields_get_meta( 'homepage_fields_banner_text' ); ?>" size="30" />
The function esc_attr
escapes strings to be safe to use as attributes.
<input type="text" name="homepage_fields_banner_text" id="homepage_fields_banner_text" value="<?php echo esc_attr(homepage_fields_get_meta( 'homepage_fields_banner_text' ) ); ?>" size="30" />
Good read:
Check out the guide for data sanitization and escaping on WordPress.
本文标签: phpCustom meta box is not displaying value showing tag as empty
版权声明:本文标题:php - Custom meta box is not displaying value showing tag as empty 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741507683a2382420.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
homepage_fields_get_meta()
is returning the result - you need toecho
it to show it on the front. i.e.<h3><?php echo homepage_fields_get_meta('homepage....
– Michael Commented Jan 28, 2020 at 5:30