I am trying to check the fields in a custom message type on the edit page of the admin page.
When the user clicks Publish, I want to check the fields in the POST data and change post_status to "pending" if the data does not pass the tests. When this happens, I would also like to add errors to the page in the admin notification area.
I tried this with an added application to the wp_insert_post action, which also saves our own data. I'm not sure about the order of operations, but I assume that wp_insert_post events occur first, and then my function is called via hook.
The problem is that this is a Wordpress function that performs publishing actions to publish, so by the time I get the data confirmation, Wordpress has already saved the message with the status of "publish". I need to either prevent this update or change the status to “pending,” but I have little chance of finding a way to do this in the API.
So, here is the order of operations that I would like to perform:
1. admin user edits post data and clicks "Publish" 2. via wp_insert_post, my data validation and post meta save routine is called 3. If data passes validation, post status is "published" 4. Otherwise, post status set to "pending" & message shown in admin notice area
Of course, someone did this, but extensive Googling just leads me to the same seemingly inappropriate pages. Can someone point me in the right direction? Thank you in advance -
UPDATE:
So, RichardML really was right, the binding to the wp_insert_post_data filter gave me the right place to check the fields of the admin post editing page. I am updating this, however, to note that the rest of the solution, in particular, is to get the reason specified in the admin notification area.
Firstly, you cannot just output data or set a field because the admin page is the result of a redirect, and by the time you get the admin page again, the admin_notices action has already disappeared. The trick was what I took from another forum, and it breaks, but it works.
What you need to do is in your validation filter function, if you decide that you need to display errors, use set_option () to add a blog option with a unique name (I used publish_errors). It must be HTML code in a div with a class of "error".
You will also need to add a quest for the admin_notices action, pointing to a function that checks for the publish_errors option, and if it detects it, prints it on the page and removes it with delete_option ().