Making a Comment on a page without being on that page?

You have to set the value of $_POST['comment_post_ID'] to the post id of the page:

<input type="hidden" name="comment_post_ID" value="10" />

Then set the action of the form element to /wp-comments-post.php, filter 'comment_post_redirect' and send the visitor back to page where the comment/review was written.

Here is an example, written as plugin:

<?php
/* Plugin Name: Comment on different page */

/**
 * Call the form in your template with:
 * do_action( 'wpse_67527_comment_form' );
 */
add_action( 'wpse_67527_comment_form', 'wpse_67527_comment_form' );

/**
 * Create a review form.
 *
 * @return void
 */
function wpse_67527_comment_form()
{
    $select_id = 'page_select';
    $url="http" . ( is_ssl() ? 's' : '' ) . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $url = esc_url( $url );
    ?>
<form action="<?php echo site_url( '/wp-comments-post.php' ); ?>" method="post">
    <p>
        <label for="<?php echo $select_id; ?>">
            Choose a page
        </label>
        <?php
        // Ordered by page name automatically
        wp_dropdown_pages(
            array (
                'name' => 'comment_post_ID',
                'id'   => $select_id,
                            'child_of' => 2,
            )
        );
        ?>
    </p>
    <p>
        <textarea name="comment" cols="45" rows="8"></textarea>
    </p>
    <p>
        <label for="author"><?php _e( 'Name' ); ?></label>
        <input id="author" name="author" type="text" size="30" />
    </p>
    <p>
        <label for="email"><?php _e( 'Email' ); ?></label>
        <input id="email" name="email" type="text" size="30" />
    </p>
    <p>
        <?php
        wp_nonce_field( 'unfiltered-html-comment_' . get_the_ID(), '_wp_unfiltered_html_comment_disabled', FALSE );
        ?>
        <input type="hidden" name="redirect_to" value="<?php echo $url; ?>" />
        <input type="submit" />
    </p>
</form>
    <?php
}

add_filter( 'comment_post_redirect', 'wpse_67527_redirect' );

/**
 * Strip '#comment-number' from redirect url.
 *
 * @param  string $url
 * @return string
 */
function wpse_67527_redirect( $url )
{
    $parts = explode( '#', $url );
    return $parts[0];
}

I called the action with:

is_front_page() and do_action( 'wpse_67527_comment_form' );

… on the index.php and got a simple comment form that sent its content to another page. 🙂

Leave a Comment