Wordpress add message verification

This is confusing, but still I am surprised to see that by default when validating a new message is not added to wordpress. When I do not enter the headline and even the content, but simply click โ€œPublishโ€, he says that the publication is published, and when I look at the interface, there is no new message.

How can Wordpress skip a simple check to add a message? At least I was expecting a server side check (if not client side). I donโ€™t know why validation is omitted. This is prior to Wordpress, regardless of whether they include it in new versions.

But I want to know how to add javascript (or jquery) to add a message to wordpress. I know this is not at all difficult. But, being new to wordpress, I would like to get some hint.

From Firebug, I could see that the form looks like:

<form id="post" method="post" action="post.php" name="post"> ... </form> 

Where should I put my javascript validation code?

+4
source share
2 answers

This is not a mistake, WordPress intentionally does this (to save AFAIK changes). In any case, this may work, but it may be better than this (insert the file into your functions.php )

 add_action( 'admin_notices', 'custom_error_notice' ); function custom_error_notice(){ global $current_screen, $post; if ( $current_screen->parent_base == 'edit' ){ if((!$post->post_name && !$post->post_content) && $_GET['post']) { wp_redirect(admin_url('post-new.php?empty=1')); } if($_GET['empty']) echo '<div class="error"><p>Warning - Please fill up all fields correctly!</p></div>'; } } 

Also, it is necessary to use wp_redirect and avoid header already sent error message , insert this into your functions.php at the top of all other code

 function callback($buffer) { // You can modify $buffer here, and then return the updated code return $buffer; } function buffer_start() { ob_start("callback"); } function buffer_end() { ob_end_flush(); } // Add hooks for output buffering add_action('init', 'buffer_start'); add_action('wp_footer', 'buffer_end'); 
+3
source

Well, you're right, WordPress has no verification on the post editing screen,

Why don't you try the Post require Fields plugin where you can easily Check What you need to check, you do not need to write a single line of javascript or PHP code.

Here is a screenshot for Backend Management

enter image description here

+1
source

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


All Articles