使用 WPAlchemy 元框将日期和时间输入数据合并到一个字段中
我正在尝试将当前使用的日期和时间元框转换为 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,或者如何使用我应该像 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 );
if(!isset($_POST["enddate"])): 返回$post; 结束; $updateendd = strtotime ( $_POST["enddate"] . $_POST["endtime"]); update_post_meta($post->ID, "enddate", $updateendd );
// filters must always continue the chain and return the data (passing it through the filter)
return $meta;
}
这行得通吗?它应该是 save_filter 还是 save_action?
任何见解表示赞赏;-)
I'm trying to convert a metabox for date and time I'm currently using into a WPAlchemy metabox.
I am currently combining the start date and start time into one field upon save.
This is the old save function:
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 );
Here are the new functions and fields for reference:
$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>
The issue I'm facing is I'm not entirely sure if I should be using the save_filter or the save_action, or how I should handle doing this ala WPAlchemy.
This is what I have thus far:
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 );
if(!isset($_POST["enddate"])):
return $post;
endif;
$updateendd = strtotime ( $_POST["enddate"] . $_POST["endtime"]);
update_post_meta($post->ID, "enddate", $updateendd );
// filters must always continue the chain and return the data (passing it through the filter)
return $meta;
}
Will this work? And should it be a save_filter or a save_action?
Any insight appreciated ;-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 WPAlchemy,您所需要做的就是在元数据中添加新值或更新值。您可以通过向
$meta
数组添加其他值来实现此目的。当您像使用 save_filter 时应做的那样返回它时,WPAlchemy 将处理数据的保存。save_filter
与save_action
之间的主要区别在于,使用过滤器时,必须传回$meta
值,但可以在之前修改数组这样做可以让您保存隐藏值。使用这些选项的强大之处在于,您可以在更新后根据用户输入的值操纵 WordPress 的其他方面。
在
save_filter
false > 告诉 WPAlchemy 停止而不保存。两者之间的另一个区别还在于save_filter
发生在保存之前,而save_action
发生在保存之后。这是我调整上面代码的尝试,显然您必须对其进行修改才能使其适合您,请阅读我包含的评论。
If you are using WPAlchemy, and all you need is to add new values or update values in your meta data. You can achieve this by adding additional values to the
$meta
array. When you return it as you should always do when using thesave_filter
, WPAlchemy will handle the saving of the data.The main difference between
save_filter
vssave_action
is that with the filter, you must pass back the$meta
value, but you can modify the array before doing so, which allows you to save hidden values.The power in using any of these options is that you can manipulate other aspects of WordPress during a post update and per the values that a user enters in.
Passing back
false
in thesave_filter
tells WPAlchemy to stop and not save. An additional difference between the two is also thatsave_filter
happens before the save andsave_action
happens afterwards.Here is my attempt at adjusting your code above, obviously you will have to touch it up to make it work for you, please read the comments I've included.