繁体   English   中英

获取值后更新Wordpress ACF字段

[英]Wordpress ACF field update after getting the value

当我在图像编辑器中裁剪图像并更新db记录中的值(使用update_field()时,我有此操作可检索裁剪值。因此,我可以更新数据库中字段的值,但我不这样做知道如何在帖子编辑器中设置字段的值。字段值保持空白,并且当用户更新帖子时,该值被null值覆盖。

add_action( 'wp_save_image_editor_file', 'save_crop_data');
function save_crop_data(){
    $attachment_id = $_REQUEST['postid'];
    $parent = get_post_ancestors($attachment_id);
    $post_id = $parent[0];
    update_field('crop_data', $_REQUEST['history'], $post_id);
    return $saved;
}

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 (文件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;";
    };
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM