douyanyan1123 2014-11-20 16:17
浏览 46
已采纳

从HTML表单更新高级自定义字段

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...

  • 写回答

1条回答 默认 最新

  • donglu953744 2014-11-20 18:32
    关注

    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:

    enter image description here

    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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题