最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Create a post in custom post type using field in registration form after users submit form

programmeradmin2浏览0评论

As part of this project i have already created an Institute custom post type which i can see in the admin dashboard.

What i want to do now is link it to the registration form. The logic is that when a user registers there will be an Institute field. In that Institute field they will write their Institute name.

I just want to try and get this to work before thinking about duplicates, variation spelling etc.

When they hit Submit, that Institute name will go on to create a post in the Institute custom post type that we will be able to see in the Admin dashboard.

The form is already set up, and this is the code iv been able to work out so far:

    $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 );

Is this correct, I was assuming it is straightforward and was going to copy and paste this.

As part of this project i have already created an Institute custom post type which i can see in the admin dashboard.

What i want to do now is link it to the registration form. The logic is that when a user registers there will be an Institute field. In that Institute field they will write their Institute name.

I just want to try and get this to work before thinking about duplicates, variation spelling etc.

When they hit Submit, that Institute name will go on to create a post in the Institute custom post type that we will be able to see in the Admin dashboard.

The form is already set up, and this is the code iv been able to work out so far:

    $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 );

Is this correct, I was assuming it is straightforward and was going to copy and paste this.

Share Improve this question asked Apr 10, 2020 at 14:50 juniorjunior 213 bronze badges 2
  • Is there a reason you haven't just copied and pasted it and then tested? I'm not sure what the question is, or where that script would live – Tom J Nowell Commented Apr 10, 2020 at 15:04
  • Hi Tom, thanks for the reply! I actually have tried, but nothing happens. Basically i want a field in my registration form to create a post in the admin backend under a specific custom post type - this to happen once a user registers. I am going to put this in the functions.php of a child theme, hope that helps? I am unsure of the code myself and just trying to get it right! – junior Commented Apr 10, 2020 at 15:21
Add a comment  | 

1 Answer 1

Reset to default 2

First, add the custom field to the registration form:

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

Then, hook to user_register action which gets called after a user has been registered succesfully.

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 );
});
发布评论

评论列表(0)

  1. 暂无评论