@msdoc11

При размещении поста с фронта, дублируется запись, как поправить?

Здравствуйте, есть код формы для отзывов:

function themedomain_post_if_submitted() {
// Stop running function if form wasn't submitted
    if ( !isset($_POST['title']) ) {
        return;
    }

    // Check that the nonce was set and valid
    if( !wp_verify_nonce($_POST['_wpnonce'], 'wps-frontend-post') ) {
        echo 'Did not save because your form seemed to be invalid. Sorry';
        return;
    }

    // Do some minor form validation to make sure there is content
    if (strlen($_POST['title']) < 3) {
        echo 'Please enter a title. Titles must be at least three characters long.';
        return;
    }
    // Add the content of the form to $post as an array
    $post = array(
        'post_title'    => $_POST['title'],
        'post_content'  => $_POST['content'],
        'post_category' => array($_POST['cat']), 
        'post_status'   => 'draft',   // Could be: publish
        'post_type' 	=> 'reviews' // Could be: `page` or your CPT
    );
    wp_insert_post($post);
    echo 'Спасибо, Ваш отзыв отправлен на проверку!';
} 
add_action('after_setup_theme', 'themedomain_post_if_submitted');

 add_shortcode( 'themedomain_frontend_post', 'themedomain_frontend_post' );
    function themedomain_frontend_post() { ?>
    
   
<div class="max-w-screen-sm mx-auto my-12">
    <form id="new_post" class="grid gap-2" name="new_post" method="post" enctype="multipart/form-data">
 <? themedomain_post_if_submitted(); ?>
        <p><label for="title" class="text-sm"><?php echo esc_html__('Ваше имя','theme-domain'); ?></label><br />
            <input type="text" id="title" class="w-full border-2 border-primary rounded-md" value="" tabindex="1" size="20" name="title" placeholder="Имя Фамилия" required />
        </p>
        <p><label for="content" class="text-sm"><?php echo esc_html__('Ваш отзыв','theme-domain'); ?></label><br />
            <textarea name="content" id="content" class="w-full border-2 border-primary rounded-md" cols="30" rows="4" placeholder="Напишите как все прошло" required></textarea>
        </p>
        <p class="-mt-1"><label for="cat" class="text-sm"><?php echo esc_html__('Категория','theme-domain'); ?></label><br />
        <span class="review-categories"><?php wp_dropdown_categories( 'tab_index=4&taxonomy=reviews-categories' ); ?></span></p>

       <?php wp_nonce_field( 'wps-frontend-post' ); ?>
        <p class="mt-4"><input type="submit" name="submit" value="Отправить" tabindex="6" id="submit" class="px-12 py-2 text-white bg-gradient-to-t from-primary to-primary/90 text-lg rounded-md mt-2 transition hover:shadow-lg hover:scale-95 cursor-pointer" name="submit" /></p>

    </form>
</div>
<?php
    }


Код работает, но в админке появляется два поста и форма отправляется снова при перезагрузке страницы, как это исправить?
  • Вопрос задан
  • 70 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы