首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将日期和时间输入数据与WPAlchemy元数据结合到一个字段中

将日期和时间输入数据与WPAlchemy元数据结合到一个字段中
EN

Stack Overflow用户
提问于 2011-03-26 10:45:03
回答 1查看 1.4K关注 0票数 0

我正在尝试将当前使用的日期和时间的metabox转换为WPAlchemy元数据。

我目前正在将开始日期和开始时间合并到一个字段中保存。

这是旧的保存函数:

代码语言:javascript
复制
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 );

以下是可供参考的新功能和字段:

代码语言:javascript
复制
$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。

到目前为止,这就是我所拥有的:

代码语言:javascript
复制
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;endif;$updateendd = strtotime ($_POST”enddate“)。$_POST“结束时间”);update_post_meta($post->ID,"enddate",$updateendd );

代码语言:javascript
复制
    // filters must always continue the chain and return the data (passing it through the filter)
    return $meta;

}

这个能行吗?是save_filter还是save_action?

任何有识之士;)

EN

回答 1

Stack Overflow用户

发布于 2011-03-26 16:35:29

如果您正在使用WPAlchemy,而您所需要的只是在元数据中添加新值或更新值。您可以通过向$meta数组添加附加值来实现这一点。当您像使用save_filter时一样返回它时,WPAlchemy将处理数据的保存。

save_filtersave_action之间的主要区别在于,对于过滤器,您必须传回$meta值,但是您可以在这样做之前修改数组,这允许您保存隐藏的值。

使用这些选项的强大功能是,您可以在更新后和用户输入的值中操作WordPress的其他方面。

save_filter中传回save_filter中的WPAlchemy告诉WPAlchemy停止而不是保存。这两者的另一个不同之处还在于,save_filter发生在保存之前,save_action发生在保存之后。

这里是我试图调整您上面的代码,显然您将不得不触摸它来使它为您工作,请阅读我已经包括的评论。

代码语言:javascript
复制
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;

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5441790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档