0

現在使用している日付と時刻のメタボックスを WPAlchemy メタボックスに変換しようとしています。

現在、保存時に開始日と開始時間を 1 つのフィールドに結合しています。

これは古い保存関数です:

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 を返します。endif; $updateendd = strtotime ( $_POST["終了日"] . $_POST["終了時間"]); 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 に停止して保存しないように指示します。2 つの追加の違いは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 に答える