Avviso array_filter - wp-includes/post.php riga 3148

13 dic 2017, 15:02:57
Visualizzazioni: 2.1K
Voti: 1

Ho inserito il seguente codice, vedi sotto, per permettere l'invio di un post dal front-end. Quando lo invio, appare questo avviso:

Warning: array_filter() expects parameter 1 to be an array, string given in [...]/wp-includes/post.php on line 3148

E non riesco a capire da dove provenga l'avviso, quale array è una stringa?

function ty_front_end_form() {

    ?>
    <form id="custom-post-type" name="custom-post-type" method="post" action="">

    <p><label for="title">Titolo Post</label><br />

    <input type="text" id="title" value="" tabindex="1" size="20" name="title" />

    </p>

    <p><label for="description">Descrizione Post</label><br />

    <textarea id="description" tabindex="3" name="description" cols="50" rows="6"></textarea>

    </p>

    <p><?php wp_dropdown_categories( 'show_option_none=Categoria&tab_index=4&taxonomy=category' ); ?></p>

    <p><label for="post_tags">Tag Post</label>

    <input type="text" value="" tabindex="5" size="16" name="post-tags" id="post-tags" /></p>

    <p align="right"><input type="submit" value="Pubblica" tabindex="6" id="submit" name="submit" /></p>

    <input type="hidden" name="post-type" id="post-type" value="custom_posts" />

    <input type="hidden" name="action" value="custom_posts" />

    <?php wp_nonce_field( 'name_of_my_action','name_of_nonce_field' ); ?>

    </form>
    <?php

    if($_POST){
        ty_save_post_data();
    }

}
add_shortcode('custom-post','ty_front_end_form');

function ty_save_post_data() {

    if ( empty($_POST) || !wp_verify_nonce($_POST['name_of_nonce_field'],'name_of_my_action') )
    {
       print 'Spiacente, la verifica del nonce non è riuscita.';
       exit;

    }

    else{ 

        // Validazione di base del form per assicurarsi che ci siano contenuti
        if (isset ($_POST['title'])) {
            $title =  $_POST['title'];
        } else {
            echo 'Inserisci un titolo';
            exit;
        }
        if (isset ($_POST['description'])) {
            $description = $_POST['description'];
        } else {
            echo 'Inserisci il contenuto';
            exit;
        }

        if(isset($_POST['post_tags'])){
        $tags = $_POST['post_tags'];
        }else{
        $tags = "";
        }

        // Aggiungi il contenuto del form a $post come array
        $post = array(
            'post_title' => wp_strip_all_tags( $title ),
            'post_content' => $description,
            'post_category' => $_POST['cat'],  // Utilizzabile anche per tassonomie personalizzate
            'tags_input' => $tags,
            'post_status' => 'draft',           // Scegli: publish, preview, future, etc.
            'post_type' => 'listings'  // Usa un custom post type se necessario
        );
        wp_insert_post($post);  // http://codex.wordpress.org/Function_Reference/wp_insert_post

        $location = home_url(); // URL di reindirizzamento, dovrebbe essere la pagina di login 

        echo "<meta http-equiv='refresh' content='0;url=$location' />"; exit;
    } // fine IF

}
1
Commenti

Dovresti sempre aggiungere il codice, se è necessario per la domanda. Se gli utenti lo leggono in seguito e il link esterno è rotto, la domanda/risposta non è utile.

bueltge bueltge
13 dic 2017 15:19:00
Tutte le risposte alla domanda 1
1

Se controlli la riga dell'errore noterai che in WP 4.9.1 viene utilizzata la funzione array_filter, che richiede un array come parametro (vedi il codice). Il valore all'interno del tuo array per un post necessita di un array per 'post_category', mentre attualmente è una stringa.

Dal codice sorgente di WordPress:

* @type array  $post_category         Array di nomi, slug o ID delle categorie.  
*                                     Di default prende il valore dell'opzione 'default_category'.  

Nel tuo esempio sorgente dovresti modificare questo punto e dovrebbe funzionare: 'post_category' => array( $_POST['cat'] ),.

// Aggiungi il contenuto del form a $post come array  
$post = array(  
    'post_title' => wp_strip_all_tags( $title ),  
    'post_content' => $description,  
    'post_category' => array( $_POST['cat'] ), // Utilizzabile anche per tassonomie personalizzate  
    'tags_input' => $tags, // Necessita un array  
    'post_status' => 'draft', // Scegli tra: publish, preview, future, ecc.  
    'post_type' => 'listings', // Usa un custom post type se necessario  
);  
wp_insert_post( $post );  
13 dic 2017 15:23:42
Commenti

inoltre tags_input è un array per i tag, probabilmente è meglio usare explode con la virgola per accettare più di un tag

Shibi Shibi
13 dic 2017 15:31:29