admin管理员组文章数量:1416050
This is embarrassing, but yet i am surprised to see that there is no validation by default while adding a new post in wordpress. When i don't enter a title and even content, and just hit Publish, it says that the post is published, and when i view the front end, there is no new post.
How could wordpress skip the simple validation for adding a post? Atleast i expected a server side validation (if not client side). Don't know why the validation is skipped. It is upto wordpress, whether they incorporate it in the new versions.
But i want to know how can i add a javascript (or jquery) validation for adding a post in wordpress. I know it must not at all be difficult. But being new to wordpress, i would like to get some hint.
From Firebug, i could see the form is rendering like:
<form id="post" method="post" action="post.php" name="post">
...
</form>
Where shall i put my javascript validation code?
This is embarrassing, but yet i am surprised to see that there is no validation by default while adding a new post in wordpress. When i don't enter a title and even content, and just hit Publish, it says that the post is published, and when i view the front end, there is no new post.
How could wordpress skip the simple validation for adding a post? Atleast i expected a server side validation (if not client side). Don't know why the validation is skipped. It is upto wordpress, whether they incorporate it in the new versions.
But i want to know how can i add a javascript (or jquery) validation for adding a post in wordpress. I know it must not at all be difficult. But being new to wordpress, i would like to get some hint.
From Firebug, i could see the form is rendering like:
<form id="post" method="post" action="post.php" name="post">
...
</form>
Where shall i put my javascript validation code?
Share Improve this question asked Jun 27, 2013 at 4:18 shasi kanthshasi kanth 7,10226 gold badges111 silver badges164 bronze badges2 Answers
Reset to default 3It's not a bug, WordPress
intentionally do this (to save revisions AFAIK). anyways, this may work but could be better ways than this (paste in your functions.php
file)
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, this is required to use wp_redirect
and to avoid header already sent error message
, paste this in 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');
Well, You are right, WordPress don't have validation in Post Edit Screen,
Why don't you try Post require Fields plugin, Where you can easily Check What to validate, You don't require to write single line of code of javascript or PHP.
Here is screenshot for Backend Management
本文标签: javascriptwordpress add post validationStack Overflow
版权声明:本文标题:javascript - wordpress add post validation - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745247970a2649646.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论