![](/img/trans.png)
[英]Update the value of specific group field inside repeater field programmatically - Advanced Custom Field (ACF) - Wordpress
[英]Wordpress ACF field update after getting the value
當我在圖像編輯器中裁剪圖像並更新db記錄中的值(使用update_field()
時,我有此操作可檢索裁剪值。因此,我可以更新數據庫中字段的值,但我不這樣做知道如何在帖子編輯器中設置字段的值。字段值保持空白,並且當用戶更新帖子時,該值被null值覆蓋。
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;";
};
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.