I have a custom filed called xxxx_url. xxxx_url should be unique.

So, before publishing the post, I want to make sure that xxxx_url is unique or not? If it isn’t unique, publishing the post should be reject.

I tried publish_post. But that isn’t the correct one, since it triggers when we publish the post. I want to run my code just before the publish.

4 s
4

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.

Leave a Reply

Your email address will not be published. Required fields are marked *