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

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)