Change the status of a Wordpress publication in a publication

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 ().

+4
source share
1 answer

You can use the wp_insert_post_data filter to check and modify mail data before inserting it into the database.


In response to your update, I do not think it is necessary to temporarily add a parameter to the database. It should be possible to just add a query string variable to your Wordpress redirect, something like this:

 add_filter('wp_insert_post_data', 'my_post_data_validator', '99'); function my_post_data_validator($data) { if ($data['post_type'] == 'post') { // If post data is invalid then $data['post_status'] = 'pending'; add_filter('redirect_post_location', 'my_post_redirect_filter', '99'); } return $data; } function my_post_redirect_filter($location) { remove_filter('redirect_post_location', __FILTER__, '99'); return add_query_arg('my_message', 1, $location); } add_action('admin_notices', 'my_post_admin_notices'); function my_post_admin_notices() { if (!isset($_GET['my_message'])) return; switch (absint($_GET['my_message'])) { case 1: $message = 'Invalid post data'; break; default: $message = 'Unexpected error'; } echo '<div id="notice" class="error"><p>' . $message . '</p></div>'; } 
+8
source

Source: https://habr.com/ru/post/1339867/


All Articles