Global $post not working in OOP function WordPress
I added a custom field with comment form. Below is my code.First function return post id when using var_dump function.But the last function return NULL always.
class Post_Rating_Public_Helper {
public static function prp_change_comment_form_defaults($commentdata, $comment_id = null ) {
global $check;
global $wpdb, $post;
//// get rating value from database
$ratingValues = $wpdb-get_results( "SELECT meta_value FROM ".$wpdb-prefix."commentmeta WHERE meta_key = 'rating'");
$results = $wpdb-get_results( "SELECT option_value FROM ".$wpdb-prefix."options WHERE option_name = 'prp_settings'");
var_dump($post-ID); // return post id
$a = unserialize($results[0]-option_value);
$b = $a['checkbox1'];
$check = $a['checkbox1'];
$stars_label = $a['rtlabel'];
if($stars_label !== ''){
$st_label = $stars_label;
}else{
$st_label = 'Please rate';
}
$arraytype= array();
$post_type = get_post_type($post-ID);
if(!empty($a)){
foreach($a as $key = $type){
if($type === $post_type){
$arraytype[] = $type;
}
}
}
if($b == 'yes'){
if(!empty($arraytype)){
if (in_array($post_type, $arraytype)) {
if(!isset($_GET['replytocom'])){
echo 'fieldset class="rating"
legend'.$st_label.'span class="required"*/span/legend
input type="radio" id="star5" name="rating" value="5" /label for="star5" title="Rocks!"5 stars/label
input type="radio" id="star4" name="rating" value="4" /label for="star4" title="Pretty good"4 stars/label
input type="radio" id="star3" name="rating" value="3" /label for="star3" title="Meh"3 stars/label
input type="radio" id="star2" name="rating" value="2" /label for="star2" title="Kinda bad"2 stars/label
input type="radio" id="star1" name="rating" value="1" /label for="star1" title="Sucks big time"1 star/label
/fieldset';
}
}
}
}
}
//////// save comment meta data ////////
public static function prp_save_comment_meta_data( $comment_id ) {
if ( ( isset( $_POST['rating'] ) ) ( $_POST['rating'] != "") )
$rating = wp_filter_nohtml_kses($_POST['rating']);
add_comment_meta( $comment_id, 'rating', $rating );
}
///////// validate meta field /////////
public static function prp_validate_comment_meta_data() {
global $post;
global $wpdb;
$results = $wpdb-get_results( "SELECT option_value FROM ".$wpdb-prefix."options WHERE option_name = 'prp_settings'");
$getPostID = $wpdb-get_results( "SELECT comment_post_ID FROM ".$wpdb-prefix."comments WHERE comment_post_ID = '".$post-ID."' ");
$a = unserialize($results[0]-option_value);
$check = $a['checkbox1'];
var_dump($post-ID); // return null
if ( $a['checkbox1'] == 'yes' ) {
$comment_post = get_post($commentdata['comment_post_ID']); //Get post object
$post_type = $comment_post-post_type;
$arraytype= array();
//exit;
foreach($a as $type){
if($type === $post_type){
$arraytype[] = $type;
}
}
//if(!empty($arraytype)){
//if (in_array($post_type, $arraytype)) {
if ( !isset( $_POST['rating'] ) || empty( $_POST['rating'] ) ){
if($_POST['comment_parent'] == 0){
wp_die( __( 'Error: Please rate this post.' ) );
}
}
//}
//}
}
//return $commentdata;
}
} //end of class
Hooks
$this-loader-add_action( 'comment_form_top', $plugin_public, 'prp_change_comment_form_defaults');
$this-loader-add_action( 'pre_comment_on_post', $plugin_public, 'prp_validate_comment_meta_data', 9);
$this-loader-add_action( 'comment_post', $plugin_public, 'prp_save_comment_meta_data' );
I didn't figure out what's the issue.
Topic globals comment-form plugin-development custom-field posts Wordpress
Category Web