2

I am trying to build my own lightweight plugin that will register Custom Post Types. The problem I am having is that I can create a post type but I can't figure out how to save the previous post type and then create another one with each new entry.

function create_custom_post_type() {

if (isset($_POST['tme_post_type_singular'])) {
    update_option('tme_post_type_singular', $_POST['tme_post_type_singular']);
    $value = $_POST['tme_post_type_singular'];
} 

$value = get_option('tme_post_type_singular', '');

if (isset($value)) {

    $custom_posts = array(
        "slug" => $value,
        "name" => $value . 's',
        "singular_name" => $value,
        "is_public" => true,
        "has_archive" => false,
    );

    register_post_type( $value,
        array(
            'labels' => array(
                'name' => __( $custom_posts['name'] ),
                'singular_name' => __( $custom_posts['singular_name'] )
            ),
        'public' => true,
        'has_archive' => true,
        )
    );


}

}
add_action( 'init', 'create_custom_post_type' );

How can change this code to allow me to create more than one post type every time I input new entries?

0

Instead of saving the CPT in the options table, try using register_post_type immediately when you submit the form, then display a success confirmation plus the empty form again. This will have two benefits - one being you're not bloating the options table, the other being that you'll immediately register the CPT and be able to submit the form with the next CPT immediately.

| improve this answer | |
  • I'm not sure I understand what you mean, when you use the register_post_type function when I submit the form. Should I use $_POST rather than get_option(); ? @WebElaine – Henzly Meghie Sep 21 '17 at 16:57
  • Yes, exactly. Use the $_POSTed variables from your form and immediately create the CPT, rather than storing anything in the database. (You would want some data validation - check if all the required POST variables are set and if not, display an error message and the form with what you've filled out so far - but if all required info is there create the CPT immediately without storing the data, and display the form again so you can create as many as you need.) – WebElaine Sep 21 '17 at 18:38
  • You are right that I can create a CPT, but when I want to create another CPT it just replaces the previous CPT rather than creating an additional CPT. Any Suggestions? @WebElaine – Henzly Meghie Sep 22 '17 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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