Moti Moti - 11 months ago 56
PHP Question

WP custom comment field only on certain page

I added an additional field to my comment section of my wordpress page with the help of a little plugin, based on a tutorial

It's only one field I need (ZIP Code).

The plugin works fine, but it adds the custom field on every page. I just want it on one specific page.

I tried to wrap all the plugin code into one function that loads on the specific page:

add_action('template_redirect', 'load-on-certain-page');

function load-on-certain-page(){
if ( is_page(23) ) {
//Complete Plugin Code

Basically that works, but the check for the empty field isn't working anymore.

add_filter( 'preprocess_comment', 'verify_comment_meta_data' );

function verify_comment_meta_data( $commentdata ) {
if (empty( $_POST['title'] ) )
wp_die( __( 'Fehler: Bitte geben Sie Ihre Postleitzahl ein.' ) );
return $commentdata;

If I exclude the field check from my
function it checks the field everywhere so thats no solution. I also tried to add an additional condition to the if-statement (
), but that does not work, too.

Can you point me into the right direction how to make my plugin functions work only on that certain page? And what "best practise" is to do it?


Answer Source

Finally I got it.

The problem is, that is_page() etc. didn't work for the "preprocess_comment" filter. But you can check the page ID the comment will be assigned to by using:

$post_id = $commentdata['comment_post_ID'];