简体   繁体   中英

Wordpress ACF field update after getting the value

I have this action that retrieves the crop values when I crop an image in the image editor and update the value in the db record (with update_field() . So I can update the value of the field in the database, but I don't know how to set the value of the field in the post editor. The field value remains blank and when the user update the post the value is being overwritten by with null value. How can I do?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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