动态设置分类术语并在POST保存时显示管理通知

时间:2017-08-04 作者:mattbru

我有一个“日期”分类法,列出了一年中的所有月份。我有一个自定义字段“start\\u date”。我想动态更新“日期”分类法,以便用户在设置帖子时不需要同时单击这两个分类法。到目前为止,我的努力还没有取得成果。我做错了什么?

/**
 * Check value of date, set date taxonomy on post save, then show admin notice
 * @param  string $id post id
 * @return void
 */
function trip_location_post_published_notification( $ID, $post ) {

    // get acf value of start date
    $date = get_field( \'start_date\', $ID );

    // get the month off the beginning of the string (ex. 11/01/2017), 11 is month
    $moNum = explode(\'/\',$date);
    $moNum = $moNum[0];
    $moList = array("January","February","March","April","May","June","July","August","September","October","November","December");
    $monthName = $moList[$moNum];

    // set taxonomy to this post ID
    wp_set_object_terms($ID,$monthName,\'date\');

    // setup notice message via WP_Error class?
    if ( $date ) {
        $notice = new WP_Error(\'date_notice\',\'This trip location start date month is set to \'.$monthName);
    } else {
        $notice = new WP_Error(\'date_notice\',\'You did not set a trip start date.\');
    }

    add_filter(\'redirect_post_location\', function( $location ) use ( $notice) {
        return add_query_arg( \'date_notice\', $notice->get_error_code(), $location );
    });

    // now add an admin notice
    add_action( \'admin_notices\', \'tax_date_set_notice\' );
    function tax_date_set_notice(){
        if ( array_key_exists( \'date_notice\', $_GET) ) { 
            $notice = $_GET[\'date_notice\'];
        ?>
        <div class="update-nag notice is-dismissable">
            <p><?php _e( $notice, \'mydomain\' ); ?></p>
        </div>
        <?php
        }
    }
}
add_action( \'save_post_trip-location\', \'trip_location_post_published_notification\', 12, 2 );

1 个回复
最合适的回答,由SO网友:mattbru 整理而成

好吧,万一有人需要我的帮助。。。这是我的解决方案。我没有使用php/wordpress解决方案,而是使用javascript。如果有人想发布php/wordpress解决方案,我很想看看。

<?php
// Admin Custom JS
add_action(\'admin_head\', \'admin_custom_js\');
function admin_custom_js() {
?>
<script>

    // update date (month) taxonomy term on change of start date acf field
    jQuery(function($){

        var $startDateInput = $(\'div[data-name="start_date"] input\');
        if ($startDateInput.length) {
            $startDateInput.on(\'change\',function(){

                var val = $(this).val();
                var moList = ["January","February","March","April","May","June","July","August","September","October","November","December"];

                if (val === \'\'){

                } else {
                    var moNum = val.substr(0,2);
                        moNum = moNum.replace(\'0\',\'\');
                        moNum = Number(moNum) - 1;
                    var moName = moList[moNum];

                    var termVal = $(\'#datechecklist\').find(\'label:contains("\'+moName+\'")\').find(\'input\').val();
                    var msg = \'Start date month set to \'+moName;

                    $(\'#datechecklist input\').prop(\'checked\',false);
                    $(\'input[value="\'+termVal+\'"]\').prop(\'checked\',true);
                    $(\'div[data-name="start_date"] .acf-label p\').remove();
                    $(\'div[data-name="start_date"] label\').after(\'<p class="description" style="font-weight:bold;color: #0a81d2">\'+msg+\'</p>\');

                }

            });

        }

    });
</script>
<?php
}

结束