获取值后 Wordpress ACF 字段更新
Wordpress ACF field update after getting the value
当我在图像编辑器中裁剪图像并更新数据库记录中的值时(使用 update_field()
,我有一个检索裁剪值的操作。
所以我可以更新数据库中字段的值,但是我不知道如何在post编辑器中设置字段的值。字段值保持空白,当用户更新 post 时,该值被空值覆盖。
我该怎么办?
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;";
};
});
当我在图像编辑器中裁剪图像并更新数据库记录中的值时(使用 update_field()
,我有一个检索裁剪值的操作。
所以我可以更新数据库中字段的值,但是我不知道如何在post编辑器中设置字段的值。字段值保持空白,当用户更新 post 时,该值被空值覆盖。
我该怎么办?
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;";
};
});