我用注释表单添加了一个自定义字段。下面是我的代码。使用var\\u dump函数时,第一个函数返回post id。但最后一个函数总是返回NULL。
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
挂钩
$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\' );
我不知道是什么问题。
SO网友:Krzysiek Dróżdż
第一个函数工作正常,因为注释表单显示在post之后。所以它是相同的请求和全局变量post
仍包含当前帖子。
另一方面comment_post
在另一个请求中运行-包含新添加注释的POST请求。因此没有全局post对象,因为不应显示post。
这就是为什么这个函数takes comment_ID
as first param. 此评论已在DB中,因此您可以选择它,然后获取分配给它的帖子。
如果要使用post incomment_post
, 您应该根据其参数进行操作:
add_action( \'comment_post\', \'show_message_function\', 10, 2 );
function show_message_function( $comment_ID, $comment_approved ) {
$comment = get_comment( $comment_ID );
$post_ID = $comment->comment_post_ID;
...
}
所以。。。它与OOP无关。这是关于何时何地调用给定函数,以及哪些全局变量可用。