2

got a question about allowing empty comments with worpdress v.4.4.1

to achieve this i use the action 'pre_comment_on_post'. in WP 4.3.1 i could do this easily:

if (isset($_POST['comment']) && $_POST['comment'] == "") {

                    $_POST['comment'] = "dummy content";
}

the dummy content would be filtered out later via another add_filter call ... in wp 4.3.1 in wp-comments-post.php is the following line of code:

$comment_content      = ( isset($_POST['comment']) ) ? trim($_POST['comment']) : null;

so my add_action works perfectly ;)

now with wp 4.4.1 the core devs changed the code and introduced a new function with 4.4.0 in /wp-includes/comment.php on line 2627

function wp_handle_comment_submission( $comment_data ) {


$comment_post_ID = $comment_parent = 0;
$comment_author = $comment_author_email = $comment_author_url = $comment_content = $_wp_unfiltered_html_comment = null;

if ( isset( $comment_data['comment_post_ID'] ) ) {
    $comment_post_ID = (int) $comment_data['comment_post_ID'];
}
if ( isset( $comment_data['author'] ) && is_string( $comment_data['author'] ) ) {
    $comment_author = trim( strip_tags( $comment_data['author'] ) );
}
if ( isset( $comment_data['email'] ) && is_string( $comment_data['email'] ) ) {
    $comment_author_email = trim( $comment_data['email'] );
}
if ( isset( $comment_data['url'] ) && is_string( $comment_data['url'] ) ) {
    $comment_author_url = trim( $comment_data['url'] );
}
if ( isset( $comment_data['comment'] ) && is_string( $comment_data['comment'] ) ) {
    $comment_content = trim( $comment_data['comment'] );

}
if ( isset( $comment_data['comment_parent'] ) ) {
    $comment_parent = absint( $comment_data['comment_parent'] );
}
if ( isset( $comment_data['_wp_unfiltered_html_comment'] ) && is_string( $comment_data['_wp_unfiltered_html_comment'] ) ) {
    $_wp_unfiltered_html_comment = trim( $comment_data['_wp_unfiltered_html_comment'] );
}

$post = get_post( $comment_post_ID );

if ( empty( $post->comment_status ) ) {

    /**
     * Fires when a comment is attempted on a post that does not exist.
     *
     * @since 1.5.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( 'comment_id_not_found', $comment_post_ID );

    return new WP_Error( 'comment_id_not_found' );

}

// get_post_status() will get the parent status for attachments.
$status = get_post_status( $post );

if ( ( 'private' == $status ) && ! current_user_can( 'read_post', $comment_post_ID ) ) {
    return new WP_Error( 'comment_id_not_found' );
}

$status_obj = get_post_status_object( $status );

if ( ! comments_open( $comment_post_ID ) ) {

    /**
     * Fires when a comment is attempted on a post that has comments closed.
     *
     * @since 1.5.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( 'comment_closed', $comment_post_ID );

    return new WP_Error( 'comment_closed', __( 'Sorry, comments are closed for this item.' ), 403 );

} elseif ( 'trash' == $status ) {

    /**
     * Fires when a comment is attempted on a trashed post.
     *
     * @since 2.9.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( 'comment_on_trash', $comment_post_ID );

    return new WP_Error( 'comment_on_trash' );

} elseif ( ! $status_obj->public && ! $status_obj->private ) {

    /**
     * Fires when a comment is attempted on a post in draft mode.
     *
     * @since 1.5.1
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( 'comment_on_draft', $comment_post_ID );

    return new WP_Error( 'comment_on_draft' );

} elseif ( post_password_required( $comment_post_ID ) ) {

    /**
     * Fires when a comment is attempted on a password-protected post.
     *
     * @since 2.9.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( 'comment_on_password_protected', $comment_post_ID );

    return new WP_Error( 'comment_on_password_protected' );

} else {

    /**
     * Fires before a comment is posted.
     *
     * @since 2.8.0
     *
     * @param int $comment_post_ID Post ID.
     */
    do_action( 'pre_comment_on_post', $comment_post_ID );

}



// If the user is logged in
$user = wp_get_current_user();
if ( $user->exists() ) {
    if ( empty( $user->display_name ) ) {
        $user->display_name=$user->user_login;
    }
    $comment_author       = $user->display_name;
    $comment_author_email = $user->user_email;
    $comment_author_url   = $user->user_url;
    $user_ID              = $user->ID;
    if ( current_user_can( 'unfiltered_html' ) ) {
        if ( ! isset( $comment_data['_wp_unfiltered_html_comment'] )
            || ! wp_verify_nonce( $comment_data['_wp_unfiltered_html_comment'], 'unfiltered-html-comment_' . $comment_post_ID )
        ) {
            kses_remove_filters(); // start with a clean slate
            kses_init_filters(); // set up the filters
        }
    }
} else {
    if ( get_option( 'comment_registration' ) ) {
        return new WP_Error( 'not_logged_in', __( 'Sorry, you must be logged in to post a comment.' ), 403 );
    }
}

$comment_type = '';

if ( get_option( 'require_name_email' ) && ! $user->exists() ) {
    if ( 6 > strlen( $comment_author_email ) || '' == $comment_author ) {
        return new WP_Error( 'require_name_email', __( '<strong>ERROR</strong>: please fill the required fields (name, email).' ), 200 );
    } elseif ( ! is_email( $comment_author_email ) ) {
        return new WP_Error( 'require_valid_email', __( '<strong>ERROR</strong>: please enter a valid email address.' ), 200 );
    }
}

if ( '' == $comment_content ) {
    return new WP_Error( 'require_valid_comment', __( '<strong>ERROR</strong>: please type a comment.' ), 200 );
}

$commentdata = compact(
    'comment_post_ID',
    'comment_author',
    'comment_author_email',
    'comment_author_url',
    'comment_content',
    'comment_type',
    'comment_parent',
    'user_ID'
);

$comment_id = wp_new_comment( wp_slash( $commentdata ) );
if ( ! $comment_id ) {
    return new WP_Error( 'comment_save_error', __( '<strong>ERROR</strong>: The comment could not be saved. Please try again later.' ), 500 );
}

return get_comment( $comment_id );

}

and the superglobal $_POST gets passed to the function in wp-comments-post.php

$comment = wp_handle_comment_submission( wp_unslash( $_POST ) );

so, now in wp 4.4.1 assigning a value to $_POST['comment'] doesn't have any effect as in the new function they map the $_POST['comment'] to $comment_content

so i was thinking of simply making $comment_content global in my add_action function:

    function action_pre_comment_on_post($comment_post_ID) {

    global $comment_content;


                if (isset($_POST['comment']) && $_POST['comment'] == "") {

                    $comment_content = "dummy comment";

                return;

            }

}   

but this sadly doesn't work :( as $comment_content is not declared in a global scope ;(

if i modify the core code and define the $comment_content in a global scope:

function wp_handle_comment_submission( $comment_data ) {

global $comment_content;

my add_action function can now pass a value to the $comment_content and all is working fine. but you actually should never modify core code! should i declare the $comment_content global in functions.php or via an add_action 'init'? or???

so ... how would you best solve this case?

any help is appreciated ;)

thanks & all the best becki

3
  • Comments are not really designed to have empty comments text. Why not just add your dummy comment content in a hidden form input field? Or why not just modify $_POST data earlier, e.g. on the init hook?
    – swissspidy
    Commented Jan 20, 2016 at 12:48
  • @swissspidy, why don't you make an answer out of your comment... Commented Jan 20, 2016 at 14:10
  • @swissspidy thanks for your suggestion. but i finally took a different approach, see my answer below ... Commented Jan 21, 2016 at 7:59

1 Answer 1

1

)

first thanks a lot to @swisspidy and the suggestion to modify $_POST on the init hook.

but after a bit of more thinking and looking at the code i took a different approach.

instead of fixing things afterwards, after the form submit, i decided to fix things via jQuery during the form submit and do the necessary checks about the comment input field there.

as i already do form validation via the jquery validation plugin:

http://jqueryvalidation.org/

and it does provide a custom submit handler

http://jqueryvalidation.org/documentation/

i decided to fix things in the custom submitHandler and assign #comment a value if the other checkbox #addTimeSheet is checked and the #comment field is not filled in:

jQuery(document).ready(function() {jQuery("#commentform").validate({

   submitHandler: function(form) {
    // do other things for a valid form

        if(jQuery("#addTimeSheet").is(":checked") == true) {

            if(jQuery("#comment").val() == "") {


                jQuery("#comment").val("%comment-dummy%");

            }

        }          

    form.submit();
  }

,

this way i don't need to fix things afterwards and i think it is a more elegant solution ;)

anyway, thanks again for your help & wish you all the best

greetings becki

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.