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?
1 Answer
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.