-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathunique-title-checker.js
More file actions
50 lines (44 loc) · 1.34 KB
/
Copy pathunique-title-checker.js
File metadata and controls
50 lines (44 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Check for the Classic Editor
*
* @package unique-title-checker
*/
document.addEventListener( 'DOMContentLoaded', function () {
document.getElementById( 'title' ).addEventListener( 'blur', function () {
const title = this.value;
// Show no warning on empty titles.
if (title === '') {
return;
}
const postId = document.querySelector( '#post_ID' )?.value;
const postType = document.querySelector( '#post_type' )?.value;
// Build the request payload
const requestData = new URLSearchParams( {
action: 'unique_title_check',
ajax_nonce: unique_title_checker.nonce,
post_id: postId || '',
post_type: postType || '',
post_title: title
} );
// Perform the AJAX request
fetch( ajaxurl + '?' + requestData.toString() )
.then( ( response ) => response.json() )
.then( ( data ) => {
const messageElement = document.getElementById( 'unique-title-message' );
if (messageElement) {
messageElement.remove();
}
if (data.status === 'error' || !unique_title_checker.only_unique_error) {
document.getElementById( 'post' ).insertAdjacentHTML(
'beforebegin',
`<div id="unique-title-message" class="${ data.status }">
<p>${ data.message }</p>
</div>`
);
}
} )
.catch( ( error ) => {
console.error( 'Error fetching unique title check:', error );
} );
} );
} );