У меня есть массив:
$userPosts
Затем у меня есть форма, и я проверяю, есть ли идентификатор текущей страницы, если это так, покажите кнопку удаления, в противном случае отобразите кнопку добавления
<form id="savedPosts" action="" method="POST" class="" autocomplete="off">
<input type="hidden" name="save_post" value="<?php echo $postId; ?>">
if (in_array($postId, $userPosts)) {?>
<button id="remove_post" name="remove_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box">Remove</button>
<?php } else {
?>
<button id="save_post" name="save_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box">Add</button>
<?php }
</form>
* 1006.* Затем я создал бит отправки:
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['save_post'])) {
$userPosts = implode(',', $userPosts);
$userPosts = trim($userPosts);
$userPosts = ltrim($userPosts, ",");
update_user_meta( $user_id, 'save_post', $userPosts );
}
if (isset($_POST['remove_post'])) {
$userPosts = explode(',', $userPosts);
if (($key = array_search($postId, $userPosts)) !== false) {
unset($userPosts[$key]);
}
$userPosts = implode(',', $userPosts);
$userPosts = trim($userPosts);
$userPosts = ltrim($userPosts, ",");
update_user_meta( $user_id, 'save_post', $userPosts );
}
}
Это работает, однако, когда я нажимаю add
или remove
, если id
находится в array
, страница отправляет правильное значение, ноэто показывает мне старую кнопку.Мне нужно вручную обновить, чтобы отобразить правильную кнопку.
Похоже, что-то вроде кэширования.Я пытался использовать
ob_start();
$myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header('Location: '.$myurl);
ob_end_flush();
Но я все еще вижу старые кнопки, не уверенный, поместил ли этот бит в неправильное положение, как указано ниже, или это просто неправильно, как я использовал его
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['save_post'])) {
$userPosts = implode(',', $userPosts);
$userPosts = trim($userPosts);
$userPosts = ltrim($userPosts, ",");
update_user_meta( $user_id, 'save_post', $userPosts );
ob_start();
$myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header('Location: '.$myurl);
ob_end_flush();
}
if (isset($_POST['remove_post'])) {
$userPosts = explode(',', $userPosts);
if (($key = array_search($postId, $userPosts)) !== false) {
unset($userPosts[$key]);
}
$userPosts = implode(',', $userPosts);
$userPosts = trim($userPosts);
$userPosts = ltrim($userPosts, ",");
update_user_meta( $user_id, 'save_post', $userPosts );
ob_start();
$myurl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header('Location: '.$myurl);
ob_end_flush();
}
}
ВЕСЬ КОД
<?php
global $bodyClass;
$bodyClass = "single";
/*
Template Name: External
*/
include ('header_single_b.php');
?>
<main role="main" class="flex-shrink-0">
<?php
$postId = $_GET['postId'];
$allposts = '';
$user_id = get_current_user_id();
$userPosts= get_user_meta( $user_id, 'save_post', TRUE );
$userPosts = explode(',', $userPosts);
$userPosts = array_values(array_unique($userPosts));
$response = wp_remote_get('https://example.com/wp-json/wp/v2/posts?include='.$postId);
if ( is_wp_error( $response ) ) {
return;
}
$posts = json_decode( wp_remote_retrieve_body( $response ) );
if ( empty( $posts ) ) {
return;
}
// If there are posts.
if ( ! empty( $posts ) ) {
// For each post.
foreach ( $posts as $post ) {
$allposts .= '<a href="' . esc_url( $post->link ) . '" target=\"_blank\">' . esc_html( $post->title->rendered ) . '</a> ' . esc_html( $fordate ) . '<br />'.$post->content->rendered;
?>
<div class="container margin-top-80 margin-bottom-80">
<div class="row">
<div class="col-8">
<form id="savedPosts" action="" method="POST" class="" autocomplete="off">
<input type="hidden" name="save_post" value="<?php echo $postId; ?>">
<?php
if (in_array($postId, $userPosts)) {?>
<button disabled="disabled" type="submit" class="save_post disabled btn btn-outline-dark" data-toggle="tooltip" data-placement="top" title="già nei tuoi favoriti">Già nella box</button>
<button id="remove_post" name="remove_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box">Rimuovi dalla box</button>
<?php } else {
?>
<button id="save_post" name="save_post" type="submit" class="save_post" data-toggle="tooltip" data-placement="top" title="aggiungilo alla tua box">Salva nella box</button>
<?php }
?>
</form>
<hr>
<h2><?php echo esc_html( $post->title->rendered ); ?></h2>
<p><?php echo $post->content->rendered; ?></p>
</div>
</div>
</div>
<?php }
}
if (!in_array($postId, $userPosts)){
array_push($userPosts,$postId);
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (isset($_POST['save_post'])) {
$userPosts = implode(',', $userPosts);
$userPosts = trim($userPosts);
$userPosts = ltrim($userPosts, ",");
update_user_meta( $user_id, 'save_post', $userPosts );
}
if (isset($_POST['remove_post'])) {
$userPosts = explode(',', $userPosts);
if (($key = array_search($postId, $userPosts)) !== false) {
unset($userPosts[$key]);
}
$userPosts = implode(',', $userPosts);
$userPosts = trim($userPosts);
$userPosts = ltrim($userPosts, ",");
update_user_meta( $user_id, 'save_post', $userPosts );
}
}
?>
</main>
<?php include ('footer_single_compare.php'); ?>