0

我正在尝试将当前使用的日期和时间的元框转换为 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 );

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?

任何见解表示赞赏;-)

4

1 回答 1

2

如果您使用的是 WPAlchemy,您只需要在元数据中添加新值或更新值。$meta您可以通过向数组添加其他值来实现此目的。当您按照使用 时应始终执行的操作返回它时save_filter,WPAlchemy 将处理数据的保存。

save_filtervs的主要区别在于save_action,使用过滤器时,您必须将值传回$meta,但您可以在这样做之前修改数组,这样您就可以保存隐藏的值。

使用这些选项中的任何一个的强大之处在于,您可以在后期更新期间以及根据用户输入的值来操作 WordPress 的其他方面。

传回false告诉save_filterWPAlchemy 停止而不是保存。两者之间的另一个区别也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;

}
于 2011-03-26T16:35:29.787 回答