2017-01-02 157 views
0

我有这个操作,当我在图像编辑器中裁剪图像并更新db记录中的值时检索裁切值(update_field(), 因此,我可以更新但我不知道如何在帖子编辑器中设置字段的值,字段值保持空白,当用户更新帖子时,值被空值覆盖。 我怎样才能怎么办?获取值后Wordpress ACF字段更新

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; 
} 

回答

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