用户提交表单后,使用注册表中的字段创建自定义帖子类型的帖子

时间:2020-04-10 作者:junior

作为这个项目的一部分,我已经创建了一个学院自定义帖子类型,我可以在管理仪表板中看到它。

我现在要做的是将其链接到注册表。逻辑是,当用户注册时,将有一个Institute字段。在该学院字段中,他们将写下自己的学院名称。

我只是想在考虑重复、变体拼写等问题之前,尝试一下让它发挥作用。

当他们点击提交时,该机构名称将继续在机构自定义帖子类型中创建帖子,我们可以在管理仪表板中看到该帖子。

表单已经建立,这是迄今为止能够制定的代码iv:

    $my_institute = array (
   \'post_type\' => \'Institute\',
   \'post_title\' => $_POST[\'field_12\'][\'text\'], /* field 12 is the text area */
   \'post_content\' => \'This description is not needed\',
   \'post_status\' => \'publish\',
);

    wp_insert_post( $my_institute );
如果这是正确的,我假设这是简单的,并打算复制和粘贴这个。

1 个回复
SO网友:Himad

首先,将自定义字段添加到注册表中:

add_action( \'register_form\', function(){
    ?>
    <p>
        <label for="institute">Institute</label>
        <input type="text" name="institute" class="input" value="" size="25">
    </p>
    <?php
} );

然后,钩住user_register 成功注册用户后调用的操作。

add_action(\'user_register\',function($user_id){
   $my_institute = array (
     \'post_type\' => \'Institute\',
     \'post_title\' => $_POST[\'institute\'], // Custom field value
     \'post_content\' => \'This description is not needed\',
     \'post_status\' => \'publish\',
    );
    wp_insert_post( $my_institute );
});