Reputation: 3
I want my users to be able to post from the front end, and to put the post in a category they created. I got everything in my form except the possibility to create a new category.
Does anyone have an idea how to do this?
Thanks!!
Upvotes: 0
Views: 3994
Reputation: 129
If you want to add Category from the front side than there is one easy way: (100% Working)
wp_insert_term('Category Name', 'texonomy_type');
Example:
wp_insert_term('National', 'category');
Upvotes: 1
Reputation: 5060
add_action( 'admin_init', function(){
wp_create_category( 'Custom Category 1' );
wp_create_category( 'Custom Category 2' );
});
Upvotes: 0
Reputation: 1479
assuming you use POST
//Checking if category already there
$cat_ID = get_cat_ID( $_POST['newcat'] );
//If not create new category
if($cat_ID == 0) {
$cat_name = array('cat_name' => $_POST['newcat']);
wp_insert_category($cat_name);
}
//Get ID of newly created category
$new_cat_ID = get_cat_ID($_POST['newcat']);
// Create post object
$new_post = array(
'post_title' => $headline,
'post_content' => $body,
'post_excerpt' => $excerpt,
'post_date' => $date,
'post_date_gmt' => $date,
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array($new_cat_ID)
);
// Insert the post into the database
wp_insert_post( $new_post );'
you can also ty this : $newcat = $_POST['newcat'] and then just replace with $newcat in the code
which maybe looks better :-)
note that wp_insert_category()
and wp-create_categoy()
has teh same function as far as you are concerned (IMHO)
Upvotes: 1
Reputation: 1479
use
<?php wp_create_category( $cat_name, $parent ); ?>
pass variable
wp_create_category($category);
or ,
hook the create_category action
Upvotes: 1