Php 检索通过metabox保存到数据库并保存到网页的数据
我开发了一个customer metabox插件,用于将用户详细信息保存到db,并将其检索回前端 我想通过这些文本字段将数据保存到数据库 到 检索并显示到第二张图像中的相关字段前端es 这是我的metabox插件代码 metabox.phpPhp 检索通过metabox保存到数据库并保存到网页的数据,php,html,wordpress,Php,Html,Wordpress,我开发了一个customer metabox插件,用于将用户详细信息保存到db,并将其检索回前端 我想通过这些文本字段将数据保存到数据库 到 检索并显示到第二张图像中的相关字段前端es 这是我的metabox插件代码 metabox.php <?php add_action('add_meta_boxes', 'wpl_owt_register_metabox_cpt'); function wpl_owt_register_metabox_cpt() { global $pos
<?php
add_action('add_meta_boxes', 'wpl_owt_register_metabox_cpt');
function wpl_owt_register_metabox_cpt()
{
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-contact.php' )
{
add_meta_box(
'owt-cpt-id', // $id
'Contact Details', // $title
'wpl_owt_book_function', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
/**********Callback function for metabox at custom post type book******************/
function wpl_owt_book_function( $post ) {
//echo "<p>Custom metabox for custom post type</p>";
define("_FILE_", "_FILE_");
wp_nonce_field( basename(_FILE_), "wp_owt_cpt_nonce");
echo "<label for='txtPhoneNum'>Phone</label><br>";
$phone_num = get_post_meta($post->ID, "telNo" , true);
echo "<input type ='tel' name = 'txtPhoneNum' value = '" . $phone_num . "'' placeholder = 'Phone Number' /><br><br>";
echo "<label for='txtEmail'>Email</label><br>";
$email = get_post_meta($post->ID, "email" , true);
echo "<input type ='email' name = 'txtEmail' value = '" . $email . "'' placeholder = 'Email Address' /><br><br>";
echo "<label for='txtHours'>Hours of Operation</label><br>";
$hours = get_post_meta($post->ID, "hourofOps" , true);
echo "<input type ='text' name = 'txtHours' value = '" . $hours . "'' placeholder = 'Working Hours' /><br><br>";
}
add_action("save_post" , "wpl_owt_save_metabox_data" , 10 , 2);
function wpl_owt_save_metabox_data($post_id, $post){
//verify nonce
if(!isset($_POST['wp_owt_cpt_nonce']) || !wp_verify_nonce($_POST['wp_owt_cpt_nonce'], basename(_FILE_))){
return $post_id;
}
//verify slug value
$post_slug = "page";
if($post_slug != $post->post_type){
return;
}
//save value to db filed
$pub_tel = '';
if(isset($_POST['txtPhoneNum'])){
$pub_tel = sanitize_text_field($_POST['txtPhoneNum']);
}
else{
$pub_tel = '';
}
update_post_meta($post_id, "telNo", $pub_tel);
$pub_email = '';
if(isset($_POST['txtEmail'])){
$pub_email = sanitize_text_field($_POST['txtEmail']);
}
else{
$pub_email = '';
}
update_post_meta($post_id, "email", $pub_email);
$pub_hours = '';
if(isset($_POST['txtHours'])){
$pub_hours = sanitize_text_field($_POST['txtHours']);
}
update_post_meta($post_id, "hourofOps", $pub_hours);
}
?>
<div class="contact-info col-md-4 col-sm-4 margin-top-20 padding-left-20">
<label class="contact-label pull-left width-wide">Contact Info</label>
<p><strong>IT'S SHOWTIME TOWING</strong><br /><br />
Phone: <a href="tel:0450749863">0450749863</a><br /><br />
Email: <a href="mailto:info@itsshowtimetowing.com.au">info@itsshowtimetowing.com.au</a><br /><br />
<strong>Hours of Operation</strong><br /><br />
<a href="#">24/7</a><br /><br /> <strong>Terms And Conditions</strong> <a href="terms_conditions.html">Click Here</a></p>
</div>
<!-- Contact Info -->
<div class="contact-info col-md-4 col-sm-4 margin-top-20 padding-left-20">
<label class="contact-label pull-left width-wide">Contact Info</label>
<p><strong>IT'S SHOWTIME TOWING</strong><br /><br />
<!--Phone: <a href="tel:0450749863">0450749863</a><br /><br /> -->
Phone: <a href="tel:0450749863"><?php echo get_post_meta( $post->ID, 'telNo', true );?></a><br /><br />
<!--Email: <a href="mailto:info@itsshowtimetowing.com.au">info@itsshowtimetowing.com.au</a><br /><br /> -->
Email: <a href="mailto:info@itsshowtimetowing.com.au"><?php echo get_post_meta($post->ID, "email" , true); ?></a><br /><br />
<strong>Hours of Operation</strong><br /><br />
<!--<a href="#">24/7</a><br /><br />-->
<a href="#"><?php echo get_post_meta($post->ID, "hourofOps" , true); ?></a><br /><br />
<strong>Terms And Conditions</strong> <a href="terms_conditions.html">Click Here</a>
</p>
</div><!-- // Contact Info -->
所以经过一个不眠之夜,我终于找到了一种方法。谢谢你的“支持” 这是代码 page contact.php
<?php
add_action('add_meta_boxes', 'wpl_owt_register_metabox_cpt');
function wpl_owt_register_metabox_cpt()
{
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-contact.php' )
{
add_meta_box(
'owt-cpt-id', // $id
'Contact Details', // $title
'wpl_owt_book_function', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
}
}
/**********Callback function for metabox at custom post type book******************/
function wpl_owt_book_function( $post ) {
//echo "<p>Custom metabox for custom post type</p>";
define("_FILE_", "_FILE_");
wp_nonce_field( basename(_FILE_), "wp_owt_cpt_nonce");
echo "<label for='txtPhoneNum'>Phone</label><br>";
$phone_num = get_post_meta($post->ID, "telNo" , true);
echo "<input type ='tel' name = 'txtPhoneNum' value = '" . $phone_num . "'' placeholder = 'Phone Number' /><br><br>";
echo "<label for='txtEmail'>Email</label><br>";
$email = get_post_meta($post->ID, "email" , true);
echo "<input type ='email' name = 'txtEmail' value = '" . $email . "'' placeholder = 'Email Address' /><br><br>";
echo "<label for='txtHours'>Hours of Operation</label><br>";
$hours = get_post_meta($post->ID, "hourofOps" , true);
echo "<input type ='text' name = 'txtHours' value = '" . $hours . "'' placeholder = 'Working Hours' /><br><br>";
}
add_action("save_post" , "wpl_owt_save_metabox_data" , 10 , 2);
function wpl_owt_save_metabox_data($post_id, $post){
//verify nonce
if(!isset($_POST['wp_owt_cpt_nonce']) || !wp_verify_nonce($_POST['wp_owt_cpt_nonce'], basename(_FILE_))){
return $post_id;
}
//verify slug value
$post_slug = "page";
if($post_slug != $post->post_type){
return;
}
//save value to db filed
$pub_tel = '';
if(isset($_POST['txtPhoneNum'])){
$pub_tel = sanitize_text_field($_POST['txtPhoneNum']);
}
else{
$pub_tel = '';
}
update_post_meta($post_id, "telNo", $pub_tel);
$pub_email = '';
if(isset($_POST['txtEmail'])){
$pub_email = sanitize_text_field($_POST['txtEmail']);
}
else{
$pub_email = '';
}
update_post_meta($post_id, "email", $pub_email);
$pub_hours = '';
if(isset($_POST['txtHours'])){
$pub_hours = sanitize_text_field($_POST['txtHours']);
}
update_post_meta($post_id, "hourofOps", $pub_hours);
}
?>
<div class="contact-info col-md-4 col-sm-4 margin-top-20 padding-left-20">
<label class="contact-label pull-left width-wide">Contact Info</label>
<p><strong>IT'S SHOWTIME TOWING</strong><br /><br />
Phone: <a href="tel:0450749863">0450749863</a><br /><br />
Email: <a href="mailto:info@itsshowtimetowing.com.au">info@itsshowtimetowing.com.au</a><br /><br />
<strong>Hours of Operation</strong><br /><br />
<a href="#">24/7</a><br /><br /> <strong>Terms And Conditions</strong> <a href="terms_conditions.html">Click Here</a></p>
</div>
<!-- Contact Info -->
<div class="contact-info col-md-4 col-sm-4 margin-top-20 padding-left-20">
<label class="contact-label pull-left width-wide">Contact Info</label>
<p><strong>IT'S SHOWTIME TOWING</strong><br /><br />
<!--Phone: <a href="tel:0450749863">0450749863</a><br /><br /> -->
Phone: <a href="tel:0450749863"><?php echo get_post_meta( $post->ID, 'telNo', true );?></a><br /><br />
<!--Email: <a href="mailto:info@itsshowtimetowing.com.au">info@itsshowtimetowing.com.au</a><br /><br /> -->
Email: <a href="mailto:info@itsshowtimetowing.com.au"><?php echo get_post_meta($post->ID, "email" , true); ?></a><br /><br />
<strong>Hours of Operation</strong><br /><br />
<!--<a href="#">24/7</a><br /><br />-->
<a href="#"><?php echo get_post_meta($post->ID, "hourofOps" , true); ?></a><br /><br />
<strong>Terms And Conditions</strong> <a href="terms_conditions.html">Click Here</a>
</p>
</div><!-- // Contact Info -->
联系方式
现在是表演时间
电话:
电子邮件:
营业时间
条款和条件
希望这个答案能对像我这样的行业新人有所帮助