I'm fairly new to WordPress development.
I am developing a theme and I have registered a new post type called 'notes' that users can register in site and see their own notes and create/edit/remove them via WordPress REST API.
I want to limit my users so that they couldn't have more than 4 notes.
For this purpose, I used a function in functions.php and hooked it into 'wp_insert_post_data' filter hook and I used
Код: Выделить всё
die();Код: Выделить всё
fetch()However, the callback doesn't return this custom error message. Instead, it gives another error message :
"Internal Server Error" with "status: 500"
Thank you in advance for your guidance.
This is the code I'm using in functions.php:
Код: Выделить всё
function noteSettings($data) { if($data['post_type']=== 'note') { if(count_user_posts(get_the_user_id(), 'note') > 4) { die('your notes count has reached to Limit; please delete a note'); } $data['post_content']= sanitize_textarea_field($data['post_content']); $data['post_title']= sanitize_text_field($data['post_title']); } if($data['post_type']=== 'note' AND $data['post_status'] !== 'trash') { $data['post_status']= 'private'; } return $data; } add_filter('wp_insert_post_data', 'noteSettings'); Код: Выделить всё
async createNote(event){ fetch(`${themeData.root_url}/wp-json/wp/v2/note`, { method: 'POST', headers : { 'content-type': 'application/json', 'X-WP-Nonce': themeData.nonce }, body : JSON.stringify({ 'title': currentTitle.value, 'content': currentBody.value, 'status': 'private' }) } ) .then(res=>{ res.json() console.log(res); }) .then(data => this.makeNoteTemplate(currentTitle, currentBody, data.id)) } My problem has solved and solution was that we need to wrap the error text in json_encode() php function to could send it for javascript! thank you all
Источник: https://stackoverflow.com/questions/780 ... sponse-why
Мобильная версия