使用 WPAlchemy 元框将日期和时间输入数据合并到一个字段中

发布于 2024-10-26 03:40:55 字数 3696 浏览 5 评论 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,或者如何使用我应该像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2024-11-02 03:40:55

如果您使用 WPAlchemy,您所需要做的就是在元数据中添加新值或更新值。您可以通过向 $meta 数组添加其他值来实现此目的。当您像使用 save_filter 时应做的那样返回它时,WPAlchemy 将处理数据的保存。

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

使用这些选项的强大之处在于,您可以在更新后根据用户输入的值操纵 WordPress 的其他方面。

save_filterfalse > 告诉 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;

}

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 the save_filter, WPAlchemy will handle the saving of the data.

The main difference between save_filter vs save_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 the save_filter tells WPAlchemy to stop and not save. An additional difference between the two is also that save_filter happens before the save and save_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.

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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文