php wp_insert data on front using a form

I'm using a form on my website to allow user to create posts on my wordpress admin. It works fine, but I have an issue, I don't get the tags meta inside my admin.

here is my php code :

?php 
if(isset($_POST['submit'])){

    $err = array();
    $err['status'] = true;
    $output = "";

    if(empty($_POST['pseudo'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Pseudo" ne peut être vide.';
    }
    if(empty($_POST['mail']) || !preg_match('/[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}/',$_POST['mail'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champs "Mail" est mal renseigné';
    }
    if(empty($_POST['title'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Titre" ne peut être vide.';
    }
    if(empty($_POST['content'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Article" ne peut être vide.';
    }

    if($err['status']){

        $insert = array(
            'post_status'   = 'publish',
            'post_title'    = htmlentities($_POST['title']),
            'post_content'  = htmlentities($_POST['content']),
            'post_category' = array(11),
            'post_author'   = 999,
        );

        $post_id = wp_insert_post($insert);

        if($post_id != 0){

            /*
            TAGS
            */
            if(!empty($_POST['keywords'])){

                $keywords = explode(',',$_POST['keywords']);

                foreach($keywords as $k=$v){
                    $tag = trim(strip_tags($v));
                    wp_insert_term(
                        $tag,
                        'post_tag',
                        array(
                            'slug' = sanitize_title($tag)
                        )
                    );
                }

            }
            $user_meta_values = array(
                'pseudo'    = htmlentities($_POST['pseudo']),
                'mail'      = $_POST['mail']
            );
            $output = add_post_meta($post_id, "user_meta", json_encode($user_meta_values)) ? 'Article inséré avec succès.' : 'Une erreur est survenue lors de l\enregistrement.' ;
        }

    }

    else{
        foreach($err['msg'] as $k=$v)
            $output .= $v . 'br /';
    }
}

my html form :

form method="post" action="?php echo site_url().'/ajouter'; ?"
    plabel for="pseudo"Pseudo/labelinput type="text" name="pseudo" id="pseudo" value="" //p
    plabel for="mail"Mail/labelinput type="text" name="mail" id="mail" value="" //p
    plabel for="title"Titre/labelinput type="text" name="title" id="title" value="" //p
    plabel for="content"Article/labeltextarea name="content" id="content" rows="10" cols="15"/textarea/p
    plabel for="keywords"Mots clés/labelinput type="text" name="keywords" id="keywords" value="" /nbsp;( séparez les mots clés par des virgules )/p
    pinput type="submit" name="submit" value="enregistrer" //p
/form

Anyone knows what I'm doing wrong ? can't find out why it's not working,

thanks for your help

Topic wp-insert-post post-meta tags Wordpress

Category Web


Use 'tags_input' instead of 'post_tags'.

See here in the notes: http://codex.wordpress.org/Function_Reference/wp_insert_post

You could use wp_set_post_tags() function: http://codex.wordpress.org/Function_Reference/wp_set_post_tags

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.