php 使用JavaScript将数据保存到ACF Repeater字段 - 教程:https://www.22nds.com/acf-repeater-field-javascript/
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 使用JavaScript将数据保存到ACF Repeater字段 - 教程:https://www.22nds.com/acf-repeater-field-javascript/相关的知识,希望对你有一定的参考价值。
var postContent = {
'title': 'Beautiful term',
'terms': {
'term_approved': true,
'term_new': true,
'term_origin': 'Ang. Fake ',
'definitions': [ {
'definition_approved': true,
'definition': 'The best definition ever',
'definition_author': 'Roxy',
'examples': [ {
'example': 'Some strange usage of the word',
'example_author': 'Ruby',
'example_approved': true
},
{
'example': 'What is beautiful?',
'example_author': 'Rocky',
'example_approved': true
}
],
'definition_likes': 0,
},
{
'definition': 'What about this one',
'definition_approved': true,
'definition_author': 'Red',
'examples': [ {
'example': 'Another example',
'example_author': 'Ron',
'example_approved': true
} ],
'definition_likes': 0,
}
],
// related_terms : ''
},
'status': 'publish' // or draft if you don't want to publish it
};
var createPost = new Promise( ( resolve, reject ) => {
var headers = new Headers( {
'Content-Type': 'application/json;charset=UTF-8',
'X-WP-Nonce': magicalData.nonce
} );
console.log( JSON.stringify( postContent ) );
var post = fetch( '<YOUR_WORDPRESS_URL>/wp-json/wp/v2/terms/', {
credentials: 'same-origin',
method: 'POST',
body: JSON.stringify( postContent ),
headers: headers
} )
.then( response => {
if ( response.ok ) {
return resolve( response );
} else {
reject( Error( response ) );
}
} )
.catch( error => {
console.log( error );
} );
} );
createPost.then( data => {
// display the name of the blog
console.log( data );
}, error => {
console.log( error );
} );
// Nonce output - it will have a different number in your output
<script type='text/javascript'>
/* <![CDATA[ */
var magicalData = {"nonce":"397e0b60df"};
/* ]]> */
</script>
// DEFINE the KEY for post meta_keys to send data to - for plugin ACF Pro
add_filter('acf/rest_api/key', function ($key, $request, $type) {
return 'terms';
}, 10, 3);
// WordPress Nonce for Authentication and enqueue my_script.js
function my_resources()
{
wp_enqueue_script(
'my_script',
get_template_directory_uri() . '/my_script.js',
null, // dependency
1.0, // version
true // loads script in footer
);
wp_localize_script(
'my_script', // handle - name of script
'magicalData', // name - name of the object we want to output
array('nonce' => wp_create_nonce('wp_rest')) // data - output
);
}
add_action('wp_enqueue_scripts', 'my_resources');
/* Remove 'Posts' entry from admin menu */
function post_remove() {
remove_menu_page('edit.php');
}
add_action('admin_menu', 'post_remove');
// Register Custom Post Type - Terms with https://generatewp.com/post-type/
function term_post_type()
{
$labels = array(
'name' => _x('Terms', 'Post Type General Name', 'text_domain'),
'singular_name' => _x('Term', 'Post Type Singular Name', 'text_domain'),
'menu_name' => __('Terms', 'text_domain'),
'name_admin_bar' => __('Terms', 'text_domain'),
'archives' => __('Term Archives', 'text_domain'),
'attributes' => __('Term Attributes', 'text_domain'),
'parent_item_colon' => __('Parent Term:', 'text_domain'),
'all_items' => __('All Terms', 'text_domain'),
'add_new_item' => __('Add New Term', 'text_domain'),
'add_new' => __('Add New', 'text_domain'),
'new_item' => __('New Term', 'text_domain'),
'edit_item' => __('Edit Term', 'text_domain'),
'update_item' => __('Update Term', 'text_domain'),
'view_item' => __('View Term', 'text_domain'),
'view_items' => __('View Terms', 'text_domain'),
'search_items' => __('Search Term', 'text_domain'),
'not_found' => __('Not found', 'text_domain'),
'not_found_in_trash' => __('Not found in Trash', 'text_domain'),
'featured_image' => __('Featured Image', 'text_domain'),
'set_featured_image' => __('Set featured image', 'text_domain'),
'remove_featured_image' => __('Remove featured image', 'text_domain'),
'use_featured_image' => __('Use as featured image', 'text_domain'),
'insert_into_item' => __('Insert into term', 'text_domain'),
'uploaded_to_this_item' => __('Uploaded to this term', 'text_domain'),
'items_list' => __('Terms list', 'text_domain'),
'items_list_navigation' => __('Terms list navigation', 'text_domain'),
'filter_items_list' => __('Filter terms list', 'text_domain'),
);
$rewrite = array(
'slug' => 'term',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __('Term', 'text_domain'),
'description' => __('Term Post Type Description', 'text_domain'),
'labels' => $labels,
'supports' => array( 'title', 'excerpt', 'author', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-book',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'post',
'show_in_rest' => true,
'rest_base' => 'terms',
);
register_post_type('term_post_type', $args);
}
add_action('init', 'term_post_type', 0);
以上是关于php 使用JavaScript将数据保存到ACF Repeater字段 - 教程:https://www.22nds.com/acf-repeater-field-javascript/的主要内容,如果未能解决你的问题,请参考以下文章
php 将Gravity Form fileupload保存到ACF Gallery
在帖子创建期间计算 PHP date_diff 并保存到 ACF 字段