At the beginning of wp_insert_post
, the function that saves/updates a post, there is a filter called wp_insert_post_empty_content
. By default this filter checks whether the title, editor, and excerpt fields are all empty, in which case the save process will be halted.
However, since all the fields to be saved are passed to this filter, you can expand this filter to include any other test to determine whether the post should be considered empty. It would be something like this:
add_filter ('wp_insert_post_empty_content','wpse312975_check_unique_url',10,2);
function wpse312975_check_unique_url ($maybe_empty, $postarr) {
// extract custom field from $postarr, check uniqueness
if ($unique) return false else return true;
}
Note: the function must return 'true' to halt the saving process.
If the custom field is not unique you may also want to echo a warning.
132_url
where132
is the post ID. Than you always have a unique value here. Besides from that: Custom fields should be saved in thesave_post
action. In this action you could check the custom field (if its not empty and has a unique value for example), and based on thisupdate_post_meta
or not. I guess you could also check the custom field and if it has no unique value, set thepost-status
todraft
or something else, to disable publishing. Otherwise I think you need jQuery to do this.