我正在尝试将我正在使用的日期和时间的元数据转换为WPAlchemy元数据集。
我目前正在保存时将开始日期和开始时间合并到一个字段中。
这是旧的保存功能:
add_action ('save_post', 'save_event');
function save_event(){
global $post;
// - still require nonce
if ( !wp_verify_nonce( $_POST['event-nonce'], 'event-nonce' )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// - convert back to unix & update post
if(!isset($_POST["startdate"])):
return $post;
endif;
$updatestartd = strtotime ( $_POST["startdate"] . $_POST["starttime"] );
update_post_meta($post->ID, "startdate", $updatestartd );
if(!isset($_POST["enddate"])):
return $post;
endif;
$updateendd = strtotime ( $_POST["enddate"] . $_POST["endtime"]);
update_post_meta($post->ID, "enddate", $updateendd );
以下是新功能和参考字段:
$custom_event_metabox = new WPAlchemy_MetaBox(array
(
'id' => '_custom_event_meta',
'title' => 'Event Information',
'template' => /event_meta.php',
'types' => array('event'),
'context' => 'normal',
'priority' => 'high',
'mode' => WPALCHEMY_MODE_EXTRACT,
'save_filter' => 'event_save_filter',
'prefix' => '_my_' // defaults to NULL
));
<li><label>Start Date</label>
<?php $mb->the_field('startdate'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsadate" />
</li>
<li><label>Start Time</label>
<?php $mb->the_field('starttime'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsatime" />
<span><em>Use 24h format (7pm = 19:00)</em></span>
</li>
<li><label>End Date</label>
<?php $mb->the_field('enddate'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsadate" />
</li>
<li><label>End Time</label>
<?php $mb->the_field('endtime'); ?>
<input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="tsatime" />
<span><em>Use 24h format (7pm = 19:00)</em></span>
我面临的问题是我不能完全确定我是否应该使用save_filter或save_action,或者我应该如何处理这个ala WPAlchemy。
这就是我到目前为止:
function event_save_filter($meta, $post_id)
{
// the meta array which can be minipulated
var_dump($meta);
// the current post id
var_dump($post_id);
// fix: remove exit, exit here only to show you the output when saving
//exit;
// - convert back to unix & update post
if(!isset($_POST["startdate"])):
return $post;
endif;
$updatestartd = strtotime ( $_POST["startdate"] . $_POST["starttime"] );
update_post_meta($post->ID, "startdate", $updatestartd );
如果(isset($ _ POST [ “结束日期”])!): 返回$ post; 万一; $ updateendd = strtotime($ _POST [“enddate”]。$ _POST [“endtime”]); update_post_meta($ post-&gt; ID,“enddate”,$ updateendd);
// filters must always continue the chain and return the data (passing it through the filter)
return $meta;
}
这会有用吗?它应该是save_filter还是save_action?
任何有识之士; - )
答案 0 :(得分:2)
如果您使用的是WPAlchemy,您只需在元数据中添加新值或更新值即可。您可以通过向$meta
数组添加其他值来实现此目的。当您使用save_filter
时应该始终返回时,WPAlchemy将处理数据的保存。
save_filter
与save_action
之间的主要区别在于,对于过滤器,您必须传回$meta
值,但您可以在执行此操作之前修改数组,这样您就可以保存隐藏的值。
使用上述任何选项的强大之处在于,您可以在更新后以及用户输入的值中操纵WordPress的其他方面。
在false
中传回save_filter
告诉WPAlchemy停止而不是保存。两者之间的另一个区别是save_filter
在保存之前发生,save_action
之后发生。
以下是我尝试调整上面的代码,显然您必须触摸它以使其适用于您,请阅读我已包含的评论。
function event_save_filter($meta, $post_id)
{
// the meta array which can be minipulated
var_dump($meta);
// the current post id
var_dump($post_id);
// fix: remove exit, exit here only to show you the output when saving
//exit;
// at this time WPAlchemy does not have any field validation
// it is best to handle validation with JS prior to form submit
// If you are going to handle validation here, then you should
// probably handle it up front before saving anything
if( ! isset($meta['startdate']) OR ! isset($meta['enddate']))
{
// returning false stops WPAlchemy from saving
return false;
}
$updatestartd = strtotime($meta['startdate'] . $meta['starttime']);
// this is an example of setting an additional meta value
$meta['startdate_ts'] = $updatestartd;
// important:
// you may or may not need the following, at this time,
// WPAlchemy saves its data as an array in wp_postmeta,
// this is good or bad depending on the task at hand, if
// you need to use query_post() WP function with the "startdate"
// parameter, your best bet is to set the following meta value
// outside of the WPAlchemy context.
update_post_meta($post_id, "startdate", $updatestartd );
$updateendd = strtotime ($meta['enddate'] . $meta['endtime']);
// similar note applies
update_post_meta($post_id, "enddate", $updateendd );
// filters must always continue the chain and return the data (passing it through the filter)
return $meta;
}