0

I'm using a form on my website using wordpress to allow users to create posts. It works perfectly fine, I'm abble to assign the posts to a custom post type, add Advanced Custom Fields, content, tags...

What I'm trying to do is to allow the user to also upload images, using the same coding architecture, as it works fine this way since.

User don't have to be registred, so when uploading an image, He'll be abble to browse is computer and then upload the image.

the image should then be saved in my media library and database, and assign to the post.

I want to be abble to assign images to post_tumbnails for example, or to custom fields.

8 new inputs for 8 images in my form.

image_1 image_2, and so on...

like this :

<input type="file" id="image_1" name="image_1" size="40">

, and then being abble to use the image object inside my insert array.

here is my php code :

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

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

    if(empty($_POST['nom'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Nom" ne peut être vide.';
    }

    if(empty($_POST['prenom'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Nom" ne peut être vide.';
    }

    if(empty($_POST['telephone'])){
        $err['status'] = false;
        $err['msg'][] = 'Le champ "Nom" 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($err['status']){

        $insert = array(
            'post_status'   => 'publish',
            'post_title'    => $_POST ['nom'] . ' ' . $_POST ['prenom'],
            'post_category' => array(11),
            'post_author'   => 999,
            'tags_input' => htmlentities($_POST['tags']),
            'post_type' => 'vendre',        
            );

        $post_id = wp_insert_post($insert);

        if($post_id != 0){

/* CUSTOM FIELDS */

update_field('field_546f2983c57ae',htmlentities($_POST['civilite']),$post_id);
update_field('field_546f22db11f3e',htmlentities($_POST['nom']),$post_id);
update_field('field_546f22e011f3f',htmlentities($_POST['prenom']),$post_id);
update_field('field_546f22e611f40',htmlentities($_POST['adresse']),$post_id);
update_field('field_546f22f311f41',htmlentities($_POST['code_postal']),$post_id);
update_field('field_546f230211f42',htmlentities($_POST['ville']),$post_id);
update_field('field_546f230811f43',htmlentities($_POST['pays']),$post_id);
update_field('field_546f230d11f44',htmlentities($_POST['telephone']),$post_id);
update_field('field_546f231611f45',htmlentities($_POST['@mail']),$post_id);
update_field('field_546f232011f46',htmlentities($_POST['localisation']),$post_id);
update_field('field_546f232611f47',htmlentities($_POST['prix_souhaite']),$post_id);
update_field('field_546f233211f48',htmlentities($_POST['description_']),$post_id);



            $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 />';
    }
}
?>

and my Html :

<form method="post" action="<?php echo site_url().'/vendre'; ?>">    
        <label class="civilite">M.</label><input type="radio" name="civilite" id="civilite" value="monsieur">
        <label class="civilite">Mme</label><input type="radio" name="civilite" id="civilite" value="madame"> <br>
        <label for="nom">Nom</label><input type="text" name="nom" id="nom" value="" /><br>
        <label for="prenom">Prénom</label><input type="text" name="prenom" id="prenom" value="" /><br>
        <label for="adresse">Adresse</label><input type="text" name="adresse" id="adresse" value="" /><br>
        <label for="code_postal">Code Postal</label><input type="text" name="code_postal" id="code_postal" value="" /><br>
        <label for="ville">Ville</label><input type="text" name="ville" id="ville" value="" /><br>
        <label for="pays">Pays</label><input type="text" name="pays" id="pays" value="" /><br>
        <label for="telephone">Téléphone</label><input type="text" name="telephone" id="telephone" value="" /><br>
        <label for="@mail">@mail</label><input type="text" name="@mail" id="@mail" value="" /><br>
        <label for="localisation">Localisation</label><input type="text" name="localisation" id="localisation" value="" /><br>
        <label for="prix_souhaite">Prix souhaité</label><input type="text" name="prix_souhaite" id="prix_souhaite" value="" /><br>
        <label for="description_">Description rapide de votre bien et commentaires</label><textarea name="description_" id="description_" rows="10" cols="15"></textarea><br>





    <p><input type="submit" name="submit" value="envoyer" /></p>
</form>


<div class="ok"><?php echo isset($output) ? $output : '' ; ?></div>

1 Answer 1

-1

If all you do is update ACF fields with your form, why not use the built-in function to generate the form? Create a new ACF field for the images, or simply use the Gallery field type, then display the form using acf_form:

<?php acf_form(array(
    'post_id'       => 'new_post',
    'new_post'      => array(
        'post_type'     => 'vendre',
        'post_status'       => 'publish'
    ),
    'return' => '%post_url%',
    'submit_value'      => 'Create'
)); ?>

This will render a complete form with all of the fields groups that you created in ACF that has the Vendre post type defined. So you don't have to bother creating a form manually, validating them and handle image uploads, everything works out of the box.

You can use the post_title paramter to display a title field in the form. If you want to generate a title based on the other fields submitted(like your code above), you can do something like this:

//Change title on create
function vendre_title_on_save( $data, $postarr ) {

    if($data['post_type'] == 'vendre' && !empty($_POST['acf'])) {

        $nom = $_POST['acf']['field_546f22db11f3e'];
        $prenom = $_POST['acf']['field_546f22e011f3f'];
        $title = $nom.' '.$prenom;

        $data['post_title'] = $title;
    }

    return $data;
}
add_filter('wp_insert_post_data', 'vendre_title_on_save', 90, 2 );

To set the tags or taxonomy terms for the posts, simple create a new field in your ACF Field group using the Taxonomy field type and make sure the "Load value based on the post's terms and update the post's terms on save" option is checked. This way your taxonomy select will be included in the acf_form and the value will be updated on your post too.

3
  • thanks @passatgt, I'm not only using the form to update ACF fields, I'm also updating post title and tags for example. I thought about the ACF field form, but when uploading images, I saw that the upload form opens the media libraby loader, not the default computer loading box, you see what I mean ? maybe you know a solution for me to use the ACF form to achieve what I'm trying to do ? Nov 22, 2014 at 18:08
  • See my updated answer. The only issue is with the media library folder. I think if the user will see the wordpress uploader modal window, its fine, we just need to find a way to hide the media library tab, so they don't have access to the already uploaded photos. I'm currently looking into this, i'll post an update if i found something.
    – passatgt
    Nov 22, 2014 at 19:12
  • thanks a lot @passatgt, I'm gonna try to use your suggestions, yes please let me know if you find a solution for the media library folder Nov 22, 2014 at 20:44

Your Answer

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

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