我正在尝试在wordpress中完成一个简单的星级投票系统,尽管我想将其添加到默认帖子post_type中。这段代码在自定义帖子类型中完美地工作,但是由于某种原因,当我将其添加到默认帖子并尝试更新帖子或发布时,它使我进入空白屏幕,如果我返回上一页,则会保存数据。这是我的代码,希望您能对我有所帮助。
// Agregar metabox Para votaciones de critica
add_action( 'add_meta_boxes_post', 'votacion_critica_metabox' );
function votacion_critica_metabox()
{
add_meta_box( 'metabox-critica-votacion', 'Votacion para Criticas', 'votacion_critica_metabox_callback', 'post', 'normal', 'high' );
}
function votacion_critica_metabox_callback( $post )
{
$post_id = $post->ID;
$values = get_post_meta( $post->ID );
$rating5 = get_post_meta( $post->ID,'rating5', true );
$rating4 = get_post_meta( $post->ID,'rating4', true );
$rating3 = get_post_meta( $post->ID,'rating3', true );
$rating2 = get_post_meta( $post->ID,'rating2', true );
$rating1 = get_post_meta( $post->ID,'rating1', true );
$votaciones = $rating1 + $rating2 + $rating3 + $rating4 + $rating5;
$dividendo = (1 * $rating1) + (2 * $rating2) + (3 * $rating3) + (4 * $rating4) + (5 * $rating5);
$promedio = $dividendo/$votaciones;
$votantes1 = get_post_meta( $post->ID,'votantes', true );
wp_nonce_field( 'critica_nonce', 'mi_critica_nonce' );
?>
<br><input type="hidden" name="votantes" id="my_meta_box_prueba" size="30" placeholder="votantes" value="<?php echo $votantes1; ?>" />
</br>
<input type="hidden" id="promedio" name="promedio" value="1" />
<div class="rating">
<div class="cover" ></div>
<input type="radio" id="star5" name="resultado5" <?php if ( $promedio == 5 ) {echo 'checked';} ?> /><label for="star5" title="Muy bueno">5 stars</label>
<input type="radio" id="star4" name="resultado4" <?php if ( $promedio >= 4 && $promedio < 5 ) {echo 'checked';} ?> /><label for="star4" title="Bueno">4 stars</label>
<input type="radio" id="star3" name="resultado3" <?php if ( $promedio >= 3 && $promedio < 4 ) {echo 'checked';} ?> /><label for="star3" title="Regular">3 stars</label>
<input type="radio" id="star2" name="resultado2" <?php if ( $promedio >= 2 && $promedio < 3 ) {echo 'checked';} ?> /><label for="star2" title="Malo">2 stars</label>
<input type="radio" id="star1" name="resultado1" <?php if ( $promedio >= 1 && $promedio < 2 ) {echo 'checked';} ?> /><label for="star1" title="Muy Malo">1 star</label>
</br>
<?php
if ( is_user_logged_in() ) { $user = get_current_user_id(); }
$votantes = get_post_meta( $post->ID, 'votantes', false); if (in_array($user, $votantes)) {} else {echo
'<input type="radio" id="star5" name="rating5" value="' .$rating5. '" /><label for="star5" title="Muy Bueno">5 stars</label>
<input type="radio" id="star4" name="rating4" value="' .$rating4. '" /><label for="star4" title="Bueno">4 stars</label>
<input type="radio" id="star3" name="rating3" value="' .$rating3. '" /><label for="star3" title="Regular">3 stars</label>
<input type="radio" id="star2" name="rating2" value="' .$rating2. '" /><label for="star2" title="Malo">2 stars</label>
<input type="radio" id="star1" name="rating1" value="' .$rating1. '" /><label for="star1" title="Muy Malo">1 star</label>';} ?>
</div>
</br>
</br>
Número de Votaciones: <?php echo $votaciones; ?>
</br>
<?php if ($votantes1 == "") {echo 'null';} else {echo $votantes1;} ?>
</br>
<?php echo $user; ?>
</br>
<?php if (in_array($user, $votantes)) { echo 'el usuario si está'; } else {echo 'el usuario no está';} ?>
<p>Promedio de Puntuación: <?php echo $promedio; ?></p>
<p>Dividendo: <?php echo $dividendo; ?></p>
</div>
<?php
}
$votantes = get_post_meta( $post->ID, 'votantes', false);
add_action( 'save_post', 'votacion_critica_metabox_save' );
function votacion_critica_metabox_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if Nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['mi_critica_nonce'] ) || !wp_verify_nonce( $_POST['mi_critica_nonce'], 'critica_nonce' ) ) return;
// if Current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// Save the data
// Review data before saving
if ( is_user_logged_in() ) { echo $user = get_current_user_id(); }
if (isset( $_POST['rating5'] ) OR isset( $_POST['rating4'] ) OR isset( $_POST['rating3'] ) OR isset( $_POST['rating2'] ) OR isset( $_POST['rating1'] )) {add_post_meta( $post_id, 'votantes', $user);}
if( empty( $_POST['rating5'] )) {add_post_meta( $post_id, 'rating5', 0, true );}
if (isset( $_POST['rating5'] )) {update_post_meta( $post_id, 'rating5', $rating5 = $_POST['rating5'] + 1 );}
if( empty( $_POST['rating4'] )) {add_post_meta( $post_id, 'rating4', 0, true );}
if (isset( $_POST['rating4'] )) {update_post_meta( $post_id, 'rating4', $rating4 = $_POST['rating4'] + 1 );}
if( empty( $_POST['rating3'] )) {add_post_meta( $post_id, 'rating3', 0, true );}
if (isset( $_POST['rating3'] )) {update_post_meta( $post_id, 'rating3', $rating3 = $_POST['rating3'] + 1 );}
if( empty( $_POST['rating2'] )) {add_post_meta( $post_id, 'rating2', 0, true );}
if (isset( $_POST['rating2'] )) {update_post_meta( $post_id, 'rating2', $rating2 = $_POST['rating2'] + 1 );}
if( empty( $_POST['rating1'] )) {add_post_meta( $post_id, 'rating1', 0, true );}
if (isset( $_POST['rating1'] )) {update_post_meta( $post_id, 'rating1', $rating1 = $_POST['rating1'] + 1 );}
}
答案 0 :(得分:0)
我解决了这个问题,在save_post函数中打印了user_id,但没有注意到。
right here after the save_post add action
// Review data before saving
if ( is_user_logged_in() ) { echo $user = get_current_user_id(); }
changed to:
// Review data before saving
if ( is_user_logged_in() ) { $user = get_current_user_id(); }
// Agregar metabox Para votaciones de critica
add_action( 'add_meta_boxes_post', 'votacion_critica_metabox' );
function votacion_critica_metabox()
{
add_meta_box( 'metabox-critica-votacion', 'Votacion para Criticas', 'votacion_critica_metabox_callback', 'post', 'normal', 'high' );
}
function votacion_critica_metabox_callback( $post )
{
$post_id = $post->ID;
$values = get_post_meta( $post->ID );
$rating5 = get_post_meta( $post->ID,'rating5', true );
$rating4 = get_post_meta( $post->ID,'rating4', true );
$rating3 = get_post_meta( $post->ID,'rating3', true );
$rating2 = get_post_meta( $post->ID,'rating2', true );
$rating1 = get_post_meta( $post->ID,'rating1', true );
$votaciones = $rating1 + $rating2 + $rating3 + $rating4 + $rating5;
$dividendo = (1 * $rating1) + (2 * $rating2) + (3 * $rating3) + (4 * $rating4) + (5 * $rating5);
$promedio = $dividendo/$votaciones;
$votantes1 = get_post_meta( $post->ID,'votantes', true );
wp_nonce_field( 'critica_nonce', 'mi_critica_nonce' );
?>
<br><input type="hidden" name="votantes" id="my_meta_box_prueba" size="30" placeholder="votantes" value="<?php echo $votantes1; ?>" />
</br>
<input type="hidden" id="promedio" name="promedio" value="1" />
<div class="rating">
<div class="cover" ></div>
<input type="radio" id="star5" name="resultado5" <?php if ( $promedio == 5 ) {echo 'checked';} ?> /><label for="star5" title="Muy bueno">5 stars</label>
<input type="radio" id="star4" name="resultado4" <?php if ( $promedio >= 4 && $promedio < 5 ) {echo 'checked';} ?> /><label for="star4" title="Bueno">4 stars</label>
<input type="radio" id="star3" name="resultado3" <?php if ( $promedio >= 3 && $promedio < 4 ) {echo 'checked';} ?> /><label for="star3" title="Regular">3 stars</label>
<input type="radio" id="star2" name="resultado2" <?php if ( $promedio >= 2 && $promedio < 3 ) {echo 'checked';} ?> /><label for="star2" title="Malo">2 stars</label>
<input type="radio" id="star1" name="resultado1" <?php if ( $promedio >= 1 && $promedio < 2 ) {echo 'checked';} ?> /><label for="star1" title="Muy Malo">1 star</label>
</br>
<?php
if ( is_user_logged_in() ) { $user = get_current_user_id(); }
$votantes = get_post_meta( $post->ID, 'votantes', false); if (in_array($user, $votantes)) {} else {echo
'<input type="radio" id="star5" name="rating5" value="' .$rating5. '" /><label for="star5" title="Muy Bueno">5 stars</label>
<input type="radio" id="star4" name="rating4" value="' .$rating4. '" /><label for="star4" title="Bueno">4 stars</label>
<input type="radio" id="star3" name="rating3" value="' .$rating3. '" /><label for="star3" title="Regular">3 stars</label>
<input type="radio" id="star2" name="rating2" value="' .$rating2. '" /><label for="star2" title="Malo">2 stars</label>
<input type="radio" id="star1" name="rating1" value="' .$rating1. '" /><label for="star1" title="Muy Malo">1 star</label>';} ?>
</div>
</br>
</br>
Número de Votaciones: <?php echo $votaciones; ?>
</br>
<?php if ($votantes1 == "") {echo 'null';} else {echo $votantes1;} ?>
</br>
<?php echo $user; ?>
</br>
<?php if (in_array($user, $votantes)) { echo 'el usuario si está'; } else {echo 'el usuario no está';} ?>
<p>Promedio de Puntuación: <?php echo $promedio; ?></p>
<p>Dividendo: <?php echo $dividendo; ?></p>
</div>
<?php
}
$votantes = get_post_meta( $post->ID, 'votantes', false);
add_action( 'save_post', 'votacion_critica_metabox_save' );
function votacion_critica_metabox_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if Nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['mi_critica_nonce'] ) || !wp_verify_nonce( $_POST['mi_critica_nonce'], 'critica_nonce' ) ) return;
// if Current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// Save the data
// Review data before saving
if ( is_user_logged_in() ) { echo $user = get_current_user_id(); }
if (isset( $_POST['rating5'] ) OR isset( $_POST['rating4'] ) OR isset( $_POST['rating3'] ) OR isset( $_POST['rating2'] ) OR isset( $_POST['rating1'] )) {add_post_meta( $post_id, 'votantes', $user);}
if( empty( $_POST['rating5'] )) {add_post_meta( $post_id, 'rating5', 0, true );}
if (isset( $_POST['rating5'] )) {update_post_meta( $post_id, 'rating5', $rating5 = $_POST['rating5'] + 1 );}
if( empty( $_POST['rating4'] )) {add_post_meta( $post_id, 'rating4', 0, true );}
if (isset( $_POST['rating4'] )) {update_post_meta( $post_id, 'rating4', $rating4 = $_POST['rating4'] + 1 );}
if( empty( $_POST['rating3'] )) {add_post_meta( $post_id, 'rating3', 0, true );}
if (isset( $_POST['rating3'] )) {update_post_meta( $post_id, 'rating3', $rating3 = $_POST['rating3'] + 1 );}
if( empty( $_POST['rating2'] )) {add_post_meta( $post_id, 'rating2', 0, true );}
if (isset( $_POST['rating2'] )) {update_post_meta( $post_id, 'rating2', $rating2 = $_POST['rating2'] + 1 );}
if( empty( $_POST['rating1'] )) {add_post_meta( $post_id, 'rating1', 0, true );}
if (isset( $_POST['rating1'] )) {update_post_meta( $post_id, 'rating1', $rating1 = $_POST['rating1'] + 1 );}
}
// Agregar metabox Para votaciones de critica
add_action( 'add_meta_boxes_post', 'votacion_critica_metabox' );
function votacion_critica_metabox()
{
add_meta_box( 'metabox-critica-votacion', 'Votacion para Criticas', 'votacion_critica_metabox_callback', 'post', 'normal', 'high' );
}
function votacion_critica_metabox_callback( $post )
{
$post_id = $post->ID;
$values = get_post_meta( $post->ID );
$rating5 = get_post_meta( $post->ID,'rating5', true );
$rating4 = get_post_meta( $post->ID,'rating4', true );
$rating3 = get_post_meta( $post->ID,'rating3', true );
$rating2 = get_post_meta( $post->ID,'rating2', true );
$rating1 = get_post_meta( $post->ID,'rating1', true );
$votaciones = $rating1 + $rating2 + $rating3 + $rating4 + $rating5;
$dividendo = (1 * $rating1) + (2 * $rating2) + (3 * $rating3) + (4 * $rating4) + (5 * $rating5);
$promedio = $dividendo/$votaciones;
$votantes1 = get_post_meta( $post->ID,'votantes', true );
wp_nonce_field( 'critica_nonce', 'mi_critica_nonce' );
?>
<br><input type="hidden" name="votantes" id="my_meta_box_prueba" size="30" placeholder="votantes" value="<?php echo $votantes1; ?>" />
</br>
<input type="hidden" id="promedio" name="promedio" value="1" />
<div class="rating">
<div class="cover" ></div>
<input type="radio" id="star5" name="resultado5" <?php if ( $promedio == 5 ) {echo 'checked';} ?> /><label for="star5" title="Muy bueno">5 stars</label>
<input type="radio" id="star4" name="resultado4" <?php if ( $promedio >= 4 && $promedio < 5 ) {echo 'checked';} ?> /><label for="star4" title="Bueno">4 stars</label>
<input type="radio" id="star3" name="resultado3" <?php if ( $promedio >= 3 && $promedio < 4 ) {echo 'checked';} ?> /><label for="star3" title="Regular">3 stars</label>
<input type="radio" id="star2" name="resultado2" <?php if ( $promedio >= 2 && $promedio < 3 ) {echo 'checked';} ?> /><label for="star2" title="Malo">2 stars</label>
<input type="radio" id="star1" name="resultado1" <?php if ( $promedio >= 1 && $promedio < 2 ) {echo 'checked';} ?> /><label for="star1" title="Muy Malo">1 star</label>
</br>
<?php
if ( is_user_logged_in() ) { $user = get_current_user_id(); }
$votantes = get_post_meta( $post->ID, 'votantes', false); if (in_array($user, $votantes)) {} else {echo
'<input type="radio" id="star5" name="rating5" value="' .$rating5. '" /><label for="star5" title="Muy Bueno">5 stars</label>
<input type="radio" id="star4" name="rating4" value="' .$rating4. '" /><label for="star4" title="Bueno">4 stars</label>
<input type="radio" id="star3" name="rating3" value="' .$rating3. '" /><label for="star3" title="Regular">3 stars</label>
<input type="radio" id="star2" name="rating2" value="' .$rating2. '" /><label for="star2" title="Malo">2 stars</label>
<input type="radio" id="star1" name="rating1" value="' .$rating1. '" /><label for="star1" title="Muy Malo">1 star</label>';} ?>
</div>
</br>
</br>
Número de Votaciones: <?php echo $votaciones; ?>
</br>
<?php if ($votantes1 == "") {echo 'null';} else {echo $votantes1;} ?>
</br>
<?php echo $user; ?>
</br>
<?php if (in_array($user, $votantes)) { echo 'el usuario si está'; } else {echo 'el usuario no está';} ?>
<p>Promedio de Puntuación: <?php echo $promedio; ?></p>
<p>Dividendo: <?php echo $dividendo; ?></p>
</div>
<?php
}
$votantes = get_post_meta( $post->ID, 'votantes', false);
add_action( 'save_post', 'votacion_critica_metabox_save' );
function votacion_critica_metabox_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if Nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['mi_critica_nonce'] ) || !wp_verify_nonce( $_POST['mi_critica_nonce'], 'critica_nonce' ) ) return;
// if Current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// Save the data
// Review data before saving
if ( is_user_logged_in() ) { echo $user = get_current_user_id(); }
if (isset( $_POST['rating5'] ) OR isset( $_POST['rating4'] ) OR isset( $_POST['rating3'] ) OR isset( $_POST['rating2'] ) OR isset( $_POST['rating1'] )) {add_post_meta( $post_id, 'votantes', $user);}
if( empty( $_POST['rating5'] )) {add_post_meta( $post_id, 'rating5', 0, true );}
if (isset( $_POST['rating5'] )) {update_post_meta( $post_id, 'rating5', $rating5 = $_POST['rating5'] + 1 );}
if( empty( $_POST['rating4'] )) {add_post_meta( $post_id, 'rating4', 0, true );}
if (isset( $_POST['rating4'] )) {update_post_meta( $post_id, 'rating4', $rating4 = $_POST['rating4'] + 1 );}
if( empty( $_POST['rating3'] )) {add_post_meta( $post_id, 'rating3', 0, true );}
if (isset( $_POST['rating3'] )) {update_post_meta( $post_id, 'rating3', $rating3 = $_POST['rating3'] + 1 );}
if( empty( $_POST['rating2'] )) {add_post_meta( $post_id, 'rating2', 0, true );}
if (isset( $_POST['rating2'] )) {update_post_meta( $post_id, 'rating2', $rating2 = $_POST['rating2'] + 1 );}
if( empty( $_POST['rating1'] )) {add_post_meta( $post_id, 'rating1', 0, true );}
if (isset( $_POST['rating1'] )) {update_post_meta( $post_id, 'rating1', $rating1 = $_POST['rating1'] + 1 );}
}