0

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'; ?>">
    <p><label for="pseudo">Pseudo</label><input type="text" name="pseudo" id="pseudo" value="" /></p>
    <p><label for="mail">Mail</label><input type="text" name="mail" id="mail" value="" /></p>
    <p><label for="title">Titre</label><input type="text" name="title" id="title" value="" /></p>
    <p><label for="content">Article</label><textarea name="content" id="content" rows="10" cols="15"></textarea></p>
    <p><label for="keywords">Mots clés</label><input type="text" name="keywords" id="keywords" value="" />&nbsp;( séparez les mots clés par des virgules )</p>
    <p><input 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

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.