获取值后,Wordpress ACF字段更新

时间:2017-01-02 19:32:30

标签: php wordpress advanced-custom-fields

我有这个动作,当我在图像编辑器中裁剪图像并更新db记录中的值(.headingToMakeSmaller { @extend h4; } )时,它会检索裁剪值。 所以我可以更新数据库中字段的值,但我不知道如何在post编辑器中设置字段的值。字段值保持空白,当用户更新帖子时,值将被空值覆盖。 我该怎么办?

update_field()

1 个答案:

答案 0 :(得分:0)

<强> PHP

add_action( 'admin_enqueue_scripts', 'portfolio_admin_script' );
function portfolio_admin_script() {
    global $post_type;
    if( 'portfolio' == $post_type )
        wp_enqueue_script( 'portfolio-admin-script', get_stylesheet_directory_uri() . '/portfolio.js', array( 'jquery', 'media-editor' ), '', true );
}

JAVASCRIPT (file portfolio.js)

jQuery(function ($) {
    $(document).ajaxComplete(function (event, xhr, settings) {
        //intercept the ajax event on media library close
        if (typeof settings.data === 'string' && /action=get-post-thumbnail-html/.test(settings.data) && xhr.responseJSON && typeof xhr.responseJSON.data === 'string') {
            var crop_data_stored = decodeURIComponent(getCookie("crop_values"));
            crop_data_stored = crop_data_stored.replace(/\\"/g, '"');
            if (crop_data_stored != '' && $('#acf-field-crop_data').val() == '') {
                jQuery('#acf-field-crop_data').val(crop_data_stored);
                deleteCookie("crop_values");
            }
        }
    });

    function getCookie(name) {
        var value = "; " + document.cookie;
        var parts = value.split("; " + name + "=");
        if (parts.length == 2) return parts.pop().split(";").shift();
    }
    function deleteCookie(name) {
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:01 GMT;";
    };
});