Hi Everyone, Especially those who are more experienced than others.

I have been teaching myself php over the last 18 months and whilst I can do what I think are basic tasks, the more difficult tasks, well, are more difficult and take much longer to complete.

The reason for my post is this. I am trying to design a bespoke WordPress comment form with additional fields. I have had some help, some of it I understand, the other bits, I am looking into.

What I would like to do, is add my own input validation & error messages, but I haven't got the foggiest idea where to start or how to add them to the WP comments php file.

I have created a replica php file with the same variables as the wp comment variables, Author, Email, Url, Comments & added my own two additional fields.

Now the fun begins.
I have the test form validating required input. ie. Name required, Invalid email address etc.

Here is the post vars - in the test comment form

    $author = $_POST['author'];
    $email = $_POST['email'];
    $url = $_POST['url'];
    $country = $_POST['country'];
    $dialingCode = $_POST['dialingCode'];
    $phone = $_POST['phone'];
    $comment = $_POST['comment'];

Here is how I am checking and displaying validation error messages in the test wp comments form

//author select
        $hint = 'author';
        registerError( $hint, $hint, $error_title, 'Name is required' );

What I would like to know is, how could I add this type of php to the wp comments form.
I have looked over some fo the code and most of it looks like this...

    if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') )
     $phone = wp_filter_nohtml_kses($_POST['phone']);

Am I right in saying that I can add the same type of code as above for author,email & url?
And add the above error message as an elseif statement?

I would, and I am sure other would like to know how to our own validation to wp comments.

Hoping someone can take the timeto explain this to me and others who are less expereinced.

It would be nice if you posted the complete wordpress code. Personally I don't see why you feel the need to assign new variables to each post var when you could just use the post vars themselves? It seems like a bit of unnessasary memory usage. Same with the 5 parameter function registerError, I don't know if that's just me but surely that isn't the best way of passing around variables. Anyway those are just my comments.

        if($_POST['phone'] != ''){
            $phone = wp_filter_nohtml_kses($_POST['phone']);
            echo 'Phone number is empty';
            // Or your error function ..
         echo 'Invalid request';
     // I assume wordpress does this for ever other post var right?

I geuss you could take that approach and then if $i==number of post vars you need then all variables have passed validation. But, to me that's a bit of a code smell.

I'm sorry I can't give you a straight forward answer but I can't see how the comment form is being processed in just 2 lines of php...

Hi user22343234 and thanks for your input -

Here is the code -

What I am trying to do is stop wordpress from displaying wp-comments-post.php
and having the user click back. It's so annoying.

add_action( 'comment_post', 'save_comment_meta_data' );
function save_comment_meta_data( $comment_id ) {
global $wpdb;
    if ( ( isset( $_POST['phone'] ) ) && ( $_POST['phone'] != '') )
     $phone = wp_filter_nohtml_kses($_POST['phone']);

    if ( ( isset( $_POST['country'] ) ) && ( $_POST['country'] != '') )
    $country = wp_filter_nohtml_kses($_POST['country']);

    $res = $wpdb->get_var( $wpdb->prepare( "SELECT `DialingCode` FROM wp_form_country WHERE Country = %s", $country ) );

    $phone = ($country == "United Kingdom") ? $res.substr($phone,1) : $res.substr($phone,0);

    $ref = rand(23456789,98765432); 

    $qry ="UPDATE `".$wpdb->prefix."comments` SET  `comment_country` =  '$country',`comment_number` =  '$phone',`comment_svcode` = '$ref' WHERE  `wp_comments`.`comment_ID` =$comment_id";



What im looking at doing is have error messages for each input, author,email,url,country,phone & comment....

But im trying to have them displayed on the comment form.

Well if you want them displayed neatly on the form you could always use ajax or simply create a session on error and redirect back to the comment form on where a simple error message will display if session is set.

I personally have never used wordpress but would recommend ditching the whole wordpress comment plugin and building it from scratch if that is an option.

Though of course you can make a work-around.
Here is how I would approach your problem (I like making my scripts concise):

class comment{
    private $fieldNames;
    function __construct(){
        $this->fieldNames = array(
            'author' => '',
            'email' => '',
            'url' => '',
            'country' => '',
            'dialingCode' => '',
            'phone' => '',
            'comment' => '',
            'submit' => 'true', // Submit button is also a post variable
        foreach($_POST as $key => $val){
                $this->fieldNames[$key] = $val;
                // An unknown post var was sent, carry on or process?
                echo 'an unknown key was passed';
        $return = array_keys($this->fieldNames,'');
            // carry onto wordpress functions as all where sent
            foreach($return as $key => $val){
                echo 'missing following field: ' . $val . '<br>';
$comment = new comment();

This approach avoids lots of switch and if statements. I wasn't going to write the whole thing up for you and this short snippet only checks if the post variables are being sent and whether or not they're empty.

I hope this can set you on the right path but I'm sure lots of if and else statements would suffice too.

.. and Yes I have tested it, it does work.

This looks brilliant for me to be geting my hads dirty with,
Thanks very much for your time and code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.