My requirements are bit different I was able to hide email and website field with filter but what I want to know is there a way to make comment text un-required when I am giving comment without comment text area filled its giving me error that comment is required please guide me how i can make comment text unrequired.
add_action('pre_comment_on_post', 'dump_comment');
function dump_comment($post_id, $author=null, $email=null) {
$comcnt = $cmntcount = comments_number( '#0', '#1', '#%' );
$comment = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
if (!$comment) {
$_POST['comment'] = 'Design' . $comcnt;
}
}
Updated and working code.
add_action('pre_comment_on_post', 'dump_comment');
function dump_comment($post_id, $author=null, $email=null) {
$comment = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
if (!$comment) {
$_POST['comment'] = 'Design #' . get_comments_number();
}
}
making the comment unique everytime. by adding the value of comment count.
My requirements are bit different I was able to hide email and website field with filter but what I want to know is there a way to make comment text un-required when I am giving comment without comment text area filled its giving me error that comment is required please guide me how i can make comment text unrequired.
add_action('pre_comment_on_post', 'dump_comment');
function dump_comment($post_id, $author=null, $email=null) {
$comcnt = $cmntcount = comments_number( '#0', '#1', '#%' );
$comment = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
if (!$comment) {
$_POST['comment'] = 'Design' . $comcnt;
}
}
Updated and working code.
add_action('pre_comment_on_post', 'dump_comment');
function dump_comment($post_id, $author=null, $email=null) {
$comment = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;
if (!$comment) {
$_POST['comment'] = 'Design #' . get_comments_number();
}
}
making the comment unique everytime. by adding the value of comment count.
Share Improve this question edited Jun 2, 2013 at 11:56 Cid Ubaid asked Jun 1, 2013 at 9:17 Cid UbaidCid Ubaid 741 silver badge8 bronze badges 5- It sounds like (based on a comment to @toscho's answer) that you have a lot of requirements that you have not specified in the question. For one, it sounds like you are trying to replace the text comment box with something else? Please edit the question to explain exactly what you are doing rather than just asking how to remove the comment box. – s_ha_dum Commented Jun 1, 2013 at 17:15
- I have already achieved all the things expect the removal of requirement of the Comment text area as toscho answered that if somebody going to re-comment with any thing it will be duplicated and hence making it not allowed. So right now I want a guide that how I can make Comment text are not required. – Cid Ubaid Commented Jun 1, 2013 at 18:47
- Yes, but your proposed solution to this last issue is problematic. Removing the comment text requirement is causing trouble. There is at least a chance of this being an XY problem. Explain what the goal is, not what you imagine the solution to be. There may be another way to do this. – s_ha_dum Commented Jun 1, 2013 at 18:57
- In case anyone want's this to work updating the code above. – Cid Ubaid Commented Jun 2, 2013 at 11:55
- If you have an answer for your question then please post it in the answer box and not as an edit to the question. – s_ha_dum Commented Jun 2, 2013 at 13:47
3 Answers
Reset to default 3You can circumvent the check for empty comments easily by adding the uploaded image as HTML to the comment text:
add_action( 'pre_comment_on_post', 'allow_empty_comment_text' );
function allow_empty_comment_text( $text = '' )
{
if ( ! isset ( $_POST['comment'] ) or '' === trim( $_POST['comment'] ) )
{
$img = '/* Process uploaded image here, create an <img> tag. */'
$_POST['comment'] = '<img>'; // use a real img tag here
}
}
function rei_preprocess_comment($comment_data) {
if ($comment_data['comment_content'] == '%dummy-text%') {
$comment_data['comment_content'] = ''; // replace dummy text.
}
return $comment_data;
}
add_filter('preprocess_comment', 'rei_preprocess_comment');
function rei_wp_footer() {
?>
<script>
jQuery(function($){
var comment = $('textarea#comment');
comment.removeAttr('required'); // remove required attribute of textarea.
$('#commentform').on('submit',function(){
if (comment.val() == '') {
comment.css('text-indent','-999px').val('%dummy-text%'); // change to dummy text.
}
});
});
</script>
<?php
}
add_action( 'wp_footer', 'rei_wp_footer' );
This function allows the user to submit a comment without any text in the textarea. Then all you need to do is hide the comment textarea with some display:none;
css.
function rei_preprocess_comment($comment_data) {
if ($comment_data['comment_content'] == '%dummy-text%') {
$comment_data['comment_content'] = ''; // replace dummy text.
}
return $comment_data;
}
add_filter('preprocess_comment', 'rei_preprocess_comment');
function rei_wp_footer() {
?>
<script>
jQuery(function($){
var comment = $('textarea#comment');
comment.removeAttr('required'); // remove required attribute of textarea.
$('#commentform').on('submit',function(){
if (comment.val() == '') {
comment.css('text-indent','-999px').val('%dummy-text%'); // change to dummy text.
}
});
});
</script>
<?php
}
add_action( 'wp_footer', 'rei_wp_footer' );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745247119a4618460.html
评论列表(0条)