I'm using a custom php code to allow visitors to add content to my website by creating posts, using a form.
It works fine.
here is my PHP code :
<?php
if(isset($_POST['submit'])){
$err = array();
$err['status'] = true;
$output = "";
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,
'tags_input' => htmlentities($_POST['tags']),
);
// $tags = $_POST['post_tags'];
$post_id = wp_insert_post($insert);
if($post_id != 0){
$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 />';
}
}
?>
and my HTML form :
<form method="post" action="<?php echo site_url().'/ajouter'; ?>">
<p><label for="pseudo">Nom</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="tags">Tags</label><input type="text" name="tags" id="tags" value="" /></p>
<p><label for="ACF_texte">ACF_texte</label><input type="text" name="ACF_texte" id="ACF_texte" value="" /></p>
<p><input type="submit" name="submit" value="enregistrer" /></p>
</form>
On every posts, I have advanced custom fields. One is for example called "text_ACF". Using the same method, I would like to be abble to get text from my form, and insert it inside my Advance Custom Field (text_ACF).
something like this in my php :
'ACF_custom_field_name' => htmlentities($_POST['text_ACF']),
and in my HTML :
<p><label for="text_ACF">ACF_texte</label><input type="text" name="text_ACF" id="text_ACF" value="" /></p>
I don't know how to do it, how to refer to my advanced custom field. Can anybody help me with this ?
Thanks a lot,
And also I would like the user to be abble to upload images also, to images fields...
I think you simply need to use the ACF function update_field($field_key, $value, $post_id)
In your case it would be something like:
update_field(
'field_4fc5ab37e1819lol',
htmlentities($_POST['text_ACF']),
$post_id
);
In order to find the field key, go to the Field Group edit screen, and click Screen Options on the top-right corner, as you can see in this picture:
You definitely should take a look at the official ACF Docs for this functions, where you'll find a complete exaplanation as well as lots of examples of usge...