douzhuo3233 2017-01-02 19:32
浏览 160
已采纳

获取值后,Wordpress ACF字段更新

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;
}
  • 写回答

1条回答 默认 最新

  • dongpai6552 2017-01-05 16:23
    关注

    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;";
        };
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型