Update CPT with custom fields with REST API
-
I have a custom post type with custom fields that a site user will complete on the front-end logged in as a subscriber. It will only show the page to a logged in user, will allow them to create, update/edit their profile. My code is working, but not all the way, none of the custom fields are updating, although I can update the title and the page slug. So the issue is the core page properties are updating but the custom fields from the page will not update. The CPT is set to only show the title and custom fields.
My jquery/ajax is in a function run on a click event:
updateProfile() { const ourUpdatedPost = { 'title': this.profileWrapper.find('input[name="first-name"]').val() + ' ' + this.profileWrapper.find('input[name="last-name"]').val(), 'slug': this.profileWrapper.find('input[name="first-name"]').val() + '-' + this.profileWrapper.find('input[name="last-name"]').val(), 'firstName': this.profileWrapper.find('input[name="first-name"]').val(), 'lastName': this.profileWrapper.find('input[name="last-name"]').val(), 'email': this.profileWrapper.find('input[name="email-address"]').val(), 'cellPhone': this.profileWrapper.find('input[name="cell-phone"]').val(), 'homePhone': this.profileWrapper.find('input[name="home-phone"]').val(), 'officePhone': this.profileWrapper.find('input[name="office-phone"]').val(), 'homeAddress': this.profileWrapper.find('input[name="home-address"]').val(), 'city': this.profileWrapper.find('input[name="city"]').val(), 'state': this.profileWrapper.find('input[name="state"]').val(), 'zipCode': this.profileWrapper.find('input[name="zip-code"]').val(), 'gender': this.profileWrapper.find('input[name="gender"]').val(), 'dateOfBirth': this.profileWrapper.find('input[name="date-of-birth"]').val(), 'rolodexId': this.profileWrapper.find('input[name="rolodex-id"]').val(), 'staffId': this.profileWrapper.find('input[name="staff-id"]').val(), 'department': this.profileWrapper.find('input[name="department"]').val(), 'jobTitle': this.profileWrapper.find('input[name="job-title"]').val(), 'supervisorsFirstName': this.profileWrapper.find('input[name="supervisors-first-name"]').val(), 'supervisorsLastName': this.profileWrapper.find('input[name="supervisors-last-name"]').val(), 'userProfileIntro': this.profileWrapper.find('textarea[name="user-profile-intro"]').val() } console.log(ourUpdatedPost); $.ajax({ beforeSend: (xhr) => { xhr.setRequestHeader('X-WP-Nonce', leadershipData.nonce); }, url: leadershipData.root_url + '/wp-json/wp/v2/profiles/' + this.profileWrapper.data('id'), type: 'PUT', data: ourUpdatedPost, success: (response) => { this.makeProfileReadOnly(this.inputFields); console.log('Congrats a success!'); console.log(response); }, error: (response) => { console.log('No Sorry'); console.log(response); } }); }The first two properties for ‘title’ and ‘slug’ will update, but none of the others will. The properties for ‘firstName’ and ‘lastName’ use the same selectors as the title and slug, but they won’t update. It does not throw any error, I get the success response, but none of the custom fields will update and on page refresh have reverted back to the placeholder.
Here is the json response at /wp-json/wp/v2/profiles?id=434:
{ id: 434, date: "2020-04-01T22:36:41", date_gmt: "2020-04-01T22:36:41", guid: { rendered: "http://devleadership.staging.wpengine.com/?post_type=profile&p=434" }, modified: "2020-05-19T06:18:48", modified_gmt: "2020-05-19T06:18:48", slug: "horatio-jones", status: "publish", type: "profile", link: "http://devleadership.staging.wpengine.com/profile/horatio-jones/", title: { rendered: "Horatio Jones" }, template: "", meta: [ ], firstName: "Stephen", lastName: "Jones", email: "sjones@yahoo.com", cellPhone: "322-333-3333", homePhone: "213-555-5555", officePhone: "818-333-3333", homeAddress: "21 Jumping Jack Terrace", city: "Alhambra", state: "CA", zipCode: "90210", gender: "Male", dateOfBirth: "11/04/1984", rolodexId: "679769", staffId: "98798798", department: "Human Resources", jobTitle: "Leader", supervisorsFirstName: "Siddarth", supervisorsLastName: "Girade", userProfileIntro: "Hello my name is Stephen, I like to help people learn and be the best person I can. Go Dodgers!", _links: { self: [ { href: "http://devleadership.staging.wpengine.com/wp-json/wp/v2/profiles/434" } ], collection: [ { href: "http://devleadership.staging.wpengine.com/wp-json/wp/v2/profiles" } ], about: [ { href: "http://devleadership.staging.wpengine.com/wp-json/wp/v2/types/profile" } ], wp:attachment: [ { href: "http://devleadership.staging.wpengine.com/wp-json/wp/v2/media?parent=434" } ], curies: [ { name: "wp", href: "https://api.w.org/{rel}", templated: true } ] } }All of the above custom fields were registered in this pattern:
register_rest_field('profile', 'firstName', array( 'get_callback' => function() { return get_field('first_name'); } ));Any thoughts on what I’m missing?
The topic ‘Update CPT with custom fields with REST API’ is closed to new replies.