2

I'm implementing a form for a frontend post with a shortcode. The form is processed in the same function, reloading the page:

function adicionar_ninhada(){
$user_id = get_current_user_id();
if (isset($_POST['adicionar_cachorro'])){
    wp_verify_nonce( $_POST['_wpnonce'],  'brg_add_dog'.$user_id );
    $args = array(
      'post_title'    => $_POST['raca'] . " nascidos em " . $_POST['nascimento'] ,
      'post_content'  => '',
      'post_status'   => 'publish',
      'post_author'   => $user_id,
      'post_type'   => 'ninhada',
    );
    $post_id = wp_insert_post( $args );
    if(isset($_FILES['foto_upload'])){
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
        require_once( ABSPATH . 'wp-admin/includes/media.php' );
        $attachment_id = media_handle_upload( 'foto_upload', $post_id );
        if ( is_wp_error( $attachment_id ) ) {
            echo "erro na imagem";
        } else {
            update_post_meta($post_id, '_thumbnail_id', $attachment_id);
        }
    }
    update_post_meta($post_id, 'raca', $_POST['raca']);
    update_post_meta($post_id, 'machos', $_POST['machos']);
    update_post_meta($post_id, 'femeas', $_POST['femeas']);
    update_post_meta($post_id, 'data', $_POST['nascimento']);
    update_post_meta($post_id, 'pai', $_POST['pai']);
    update_post_meta($post_id, 'mae', $_POST['mae']);
    echo "Adicionado com sucesso!";
}else{
    echo "<form enctype=\"multipart/form-data\" method=\"post\">
    Raça: "; 
    wp_dropdown_categories(
        array(
            'taxonomy' => 'raca', 
            'hide_empty' => 1,
            'show_option_none'   => 'SRD',
            'option_none_value'  => 'Outro',
            'name' => 'raca',
        )
    );
echo "<br />
    Machos: <input type=\"number\" name=\"machos\" /><br />
    Fêmeas: <input type=\"number\" name=\"femeas\" /><br />
    Data de nascimento: <input type=\"text\" name=\"nascimento\" class=\"brgdatepicker\"/><br />
    <label for=\"pai\">Pai: </label><select name=\"pai\">";
    $args = array( 'post_type' => 'pai', 'posts_per_page' => -1, 'post_status' => 'published', 'post_parent' => null); 
    $pais = get_posts( $args );
    foreach ($pais as $pai){
            echo '<option value="'.$pai->ID.'">' . get_the_title($pai->ID) . '</option>';
        }
        echo'</select><br />';
    echo "<label for=\"mae\">Mãe: </label><select name=\"mae\">";
    $args = array( 'post_type' => 'mae', 'posts_per_page' => -1, 'post_status' => 'published', 'post_parent' => null); 
    $maes = get_posts( $args );
    foreach ($maes as $mae){
            echo '<option value="'.$mae->ID.'">' . get_the_title($mae->ID) . '</option>';
        }
        echo'</select><br />';
    echo "
    <input type=\"hidden\" name=\"adicionar_cachorro\" value=\"1\">
    Foto: <input type=\"file\" name=\"foto_upload\" id=\"foto_upload\"  multiple=\"false\" /><br />
    <button type=\"submit\" name=\"enviar\">Salvar</button>
    ";
    wp_nonce_field( 'brg_add_dog'.$user_id );
    echo "</form>";
wp_reset_query();
}
}
add_shortcode('adicionar_ninhada', 'adicionar_ninhada');

I've tried already wp_reset_postdata() after each get_posts() foreach loop , wp_reset_postdata() in the end of the function and both scenarios with wp_reset_query() too. But the original page query is always broken after form submission and it says "Nothing found". If I comment out the get_posts() everything works fine.

12
  • 2
    Shortcodes must return content, not echo. wp_reset_postdata restores the global $post, wp_reset_query restores the global $wp_query. get_posts alters neither of those vars on its own, your problem is elsewhere.
    – Milo
    Commented Feb 5, 2016 at 22:47
  • Yes, you're right about shortcodes. I've changed the code to return an output and it's still with the same error... and why does it work when I comment out get_posts line? Commented Feb 6, 2016 at 9:59
  • When you get the "not found" result, are you sure the main query is intact before the Shortcode renders? var_dump($wp_query) on those requests and look at what it contains.
    – Milo
    Commented Feb 6, 2016 at 17:11
  • @Milo the var_dump($wp_query) gave me a clue of what's happening, and that looks really weird now. The $_POST array from the form is loaded as $wp_query->query_vars when the form is submitted. The same thing happens with the $args array from get_posts(). Since one of the $args key is post_type WordPress try to load the page_id where the form is but with the post_type that was used in get_posts(), instead of 'page' post_type. That's why it outputs "Not found". When I comment out get_posts() it works, because there's no post_type in the query _vars. Any idea why is that happening? Commented Feb 9, 2016 at 16:37
  • 1
    I don't think you're understanding me. You are not submitting anything named post_type with your form, it is getting added by WordPress because you are sending a form element named mae, which is a post type. WordPress thinks you are trying to query for a post of type mae with slug that matches whatever is submitted with your form. That's why you can't use any form element names that conflict with query vars.
    – Milo
    Commented Feb 9, 2016 at 19:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.