我怎么能add meta boxes 到front end post submission form? 与创建新帖子的前端帖子提交类似,表单回复应该直接进入帖子元框。
Meta Boxes Code Example for post 从后端到前端
<?php
function admin_init(){
add_meta_box("mia_post_meta", "Information", "mia_post_meta", "post", "normal", "high");
}
add_action("admin_init", "admin_init");
function mia_post_meta($callback_args) {
global $post;
$post_type = $callback_args->post_type;
$temp_array = array();
$temp_array = maybe_unserialize(get_post_meta($post->ID,\'mia_ev_settings\',true));
$mia_ev_bday = isset( $temp_array[\'mia_ev_bday\'] ) ? $temp_array[\'mia_ev_bday\'] : \'\';
echo \'<script type="text/javascript">jQuery(document).ready(function(){jQuery("#mia_ev_bday").simpleDatepicker();});</script>\';
?>
<div id="mia_custom_settings" style="margin: 13px 0 17px 4px;">
<div class="mia_fs_setting" style="margin: 13px 0 26px 4px;">
<label for="mia_ev_bday" style="color: #000; font-weight: bold;"> Birthday: </label>
<input type="text" class="small-text" value="<?php echo $mia_ev_bday; ?>" id="mia_ev_bday" name="mia_ev_bday" size="67" />
<br />
<small style="position: relative; top: 8px;">ex: <code>13 Jan, 2011</code></small>
</div>
</div>
<?php
}
add_action(\'save_post\', \'save_details\');
function save_details($post_id){
global $post;
$temp_array = array();
if ( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE )
return $post_id;
$temp_array[\'mia_ev_bday\'] = isset($_POST["mia_ev_bday"]) ? $_POST["mia_ev_bday"] : \'\';
update_post_meta( $post->ID, "mia_ev_settings", $temp_array );
}
function mia_bday_scripts(){
wp_register_script(\'datepicker\', get_bloginfo(\'template_directory\').\'/js/date/datepicker.js\', array(\'jquery\'));
wp_enqueue_script(\'datepicker\');
}
function mia_style_css(){
wp_register_style( \'datepicker\', get_bloginfo(\'template_directory\').\'/js/date/datepicker.css\');
wp_enqueue_style(\'datepicker\');
}
add_action(\'admin_print_scripts\', \'mia_date_scripts\');
add_action(\'admin_print_styles\', \'mia_style_css\');
?>
mia\\u ev\\u bday也应由前端用户填写。
FORM EXAMPLE on Front-end 看见Image
用户在表单中添加的信息应显示在帖子中,并显示在自定义元框的后端元框中。
SO网友:jaredwilli
您可以创建一个具有表单处理php脚本操作的表单。在php脚本中,可以检查表单是否已提交,如果有操作,则使用$\\u POST变量为表单字段创建变量。然后创建$POST=array()以设置POST元数组,并使用wp\\u insert\\u POST($POST);插入立柱。
下面是一个表单处理代码的示例,我用于上使用的前端post表单http://WPHonors.com. 它适用于我为其创建的自定义post类型,该类型设置了一个基本表单,其中有一个隐藏的post\\u type字段,该字段的值为post type。
if( $_POST[\'post_type\'] == \'site\' ) {
$title = $_POST[\'title\'];
$desc = $_POST[\'description\'];
$siteurl = $_POST[\'siteurl\'];
$cat = array( $_POST[\'cat\'] );
$tags = trim( $_POST[\'tags\'] );
if(!isset($title)) { echo \'<div class="error">Title required.</div>\'; }
if(!isset($desc)) { echo \'<div class="error">Description required.</div>\'; }
if(!isset($siteurl)) { echo \'<div class="error">URL required.</div>\'; }
if($cat == -1) { echo \'<div class="error">Select a category.</div>\'; }
if(!isset($tags)) { echo \'<div class="error">Must use at least one tag.</div>\'; }
if (!current_user_can( \'publish_posts\')) { $poststatus = \'draft\'; } else { $poststatus = \'publish\'; }
//$insert = new TypeSites();
$post = array(
\'post_title\' => $title,
\'post_content\' => $desc,
\'siteurl\' => $siteurl,
\'post_category\' => $cat,
\'tags_input\' => $tags,
\'post_status\' => $poststatus,
\'post_type\' => \'site\'
);
wp_insert_post( $post );
}
SO网友:Micah Wood
最好的解决方案是将现有的元框拉到前端的页面上,而不必重新创建它们。无需执行任何其他工作,只需使用以下代码:
// Load necessary admin files
include( ABSPATH . \'wp-admin/includes/template.php\' );
include( ABSPATH . \'wp-admin/includes/post.php\' );
// Add meta boxes
do_action(\'add_meta_boxes\', $post_type, $post);
do_action(\'add_meta_boxes_\' . $post_type, $post);
// Show Meta Boxes
do_meta_boxes( $post_type, \'normal\', $post );
do_meta_boxes( $post_type, \'advanced\', $post );
do_meta_boxes( $post_type, \'side\', $post );
只要您设置了其他$\\u POST变量并使用wp\\u insert\\u POST(),就会调用“save\\u POST”操作,并保存元数据库中的数据。