Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a built a Wordpress site that enables comments on pages and I am using them as reviews, what I would like to do is for someone to be able to make a comment on a page without being on that page.

http://universitycompare.com/university-guide/anglia-ruskin-university/

On the link above, the top right, there is a link that says have your say about a university, it would be good to have a HTML form and then a dropdown box of all the child pages of university-guide; which is where all the universities are stored, and then I can put my comments.php file into this.

So my HTML form appears and when a page is picked from a dropdown box, the comment will then be assigned to that page.

Any help or direction?


New Question for Toscho:

Below is your tweaked plugin to fit into my website, as you can see I have added the select option yearSelect and need this to appear when the comments appear. How would I go about doing this? - Could you provide ay help please?

    <?php
/* Plugin Name: haveyoursay */

/**
 * 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">
<h3>Leave us your student perspective <img src="/wp-content/themes/blue-and-grey/images/hmepage-tri.png"></h3>
    <p>
        <label for="author"><?php _e( 'Name' ); ?></label>&nbsp;&nbsp;&nbsp;&nbsp;
        <input id="author" name="author" type="text" size="40" />
    </p>
    <p>
        <label for="email"><?php _e( 'Email' ); ?></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input id="email" name="email" type="text" size="40" />
    </p>
    <p>
        <label for="<?php echo $select_id; ?>">
            <div class="clear"></div>
            Pick a university from the list: &nbsp;&nbsp;
        </label>
        <?php
        // Ordered by page name automatically
        wp_dropdown_pages(
            array (
                'name' => 'comment_post_ID',
                'id'   => $select_id,
                'child_of' => 2,
            )
        );
        ?>  
    </p>
    <div class="clear"></div>
    <p style="margin:10px 0px 0px 0px;">Please select your year of study: &nbsp;&nbsp;
    <select name="yearSelect">
    <option value="1st">First Year</option>
    <option value="2nd">Second Year</option>
    <option value="2nd">Third Year</option>
    </select>
    </p>
    <br />
    <p>
        <textarea name="comment" cols="45" rows="8"></textarea>
    </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" style="margin:15px 0px 0px 0px; padding:4px 10px;" />
    </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];
}
?>
share|improve this question
    
Do not change the meaning of a question after it was answered. That’s very hard to understand. Ask a new question instead. –  toscho Oct 9 '12 at 3:32
    
@toscho - okay, I will go back to the original question I asked and delete the 'new' stuff above. here is a link to my new question: wordpress.stackexchange.com/questions/67570/… - would appreciate any help you may be able to offer if you can. –  Owen O'Neill Oct 9 '12 at 3:51

1 Answer 1

up vote 2 down vote accepted

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. :)

share|improve this answer
    
wow! pretty good tosco! I'm going to give that a try now, now with the label's etc I can add them and have them display as part of the comments right? –  Owen O'Neill Oct 9 '12 at 0:53
    
must say! Brilliant plug-in and something I am really happy with! - Cannot thank you enough, but is there a way that it can only display the child pages of the page id '2' - How would I go about doing this? –  Owen O'Neill Oct 9 '12 at 1:10
1  
Look up wp_dropdown_pages(): the parameter child_of will do what you need. –  toscho Oct 9 '12 at 1:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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