Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Create New Category on Wordpress Front End WordPress

  • SOLVED

Hi All,
First time poster at WP-Questions.

I'm building a Wordpress powered website where a user can create a post on the front end. I'm using Gravity Forms to do this which is working well.

I'd also like them to be able to add a new category however i'd like to do it in a specific order:

I'd like them to create a new category, typing it into a single line input first, click save, then go to another form and fill out the post information where it gives them an option to post it into the category they've just created in a dropdown. I've managed to do this part using Gravity Forms - not too difficult using their built in form creator.

The problem I have is I have no idea how to go about adding an input to allow users to add a new category on the front end. I've read this post (http://stackoverflow.com/questions/7967737/how-to-create-a-new-wordpress-category-in-front-end) but don't understand it. There is a plugin called Quick Post Widget (http://qpw.famvanakkeren.nl/) that inserts a widget into a sidebar and then allows a user to create a new post INCLUDING the ability to add a new category themselves so I know it is possible to do with Wordpress.

I'd say i'm getting up to intermediate level with Wordpress but really need step-by-step instructions in order to add this to a custom template.

Many thanks for any help in advance!

Answers (3)

2012-06-17

Arnav Joy answers:

Create a new page templete in your theme directory
for eg. add-category.php then add following code to that file
<?php
/*Template Name: Add Category*/
get_header();
if(isset($_POST['submit'])){
$cat_ID = get_cat_ID( $_POST['newcat'] );

//If not create new category
if($cat_ID == 0) {
$cat_name = $_POST['newcat'];
$new_cat_ID = wp_create_category($cat_name);
}
}
?>

<div id="primary">
<div id="content" role="main">
Form for creating category
<form action="" method="post">
Enter Category Title <input type="text" name="newcat" value="" /><br />
<input type="submit" name="submit" value="Submit" />
</form>

</div><!-- #content -->
</div><!-- #primary -->


<?php get_footer(); ?>


then create a new page and choose the "Add Category" as template then view the page


Jack comments:

This works beautifully. Thank you!
Are you able to adapt it as per the conversation with Just Me, below?


Arnav Joy comments:

For the category existent validation , I have checked it in my above code and for this newly added category to make as subcategory , you have to add parent id to the function as follows:-

<?php

/*Template Name: Add Category*/

get_header();

if(isset($_POST['submit'])){

$cat_ID = get_cat_ID( $_POST['newcat'] );



//If not create new category

if($cat_ID == 0) {

$cat_name = $_POST['newcat'];

$parenCatID = 0; // pass here the parent id of the category

$new_cat_ID = wp_create_category($cat_name,$parenCatID);

}

}

?>



Also if you want me to make this as dynamic then let me know


Jack comments:

Thanks but I'm a little lost here.

What I'd like ideally, is for an inline message to display below the input, that says "That category already exists" or something, if typing a duplicate project.

It would also be good to have 2 input boxes and a drop down. The top with the main category input and the bottom with the ability to add a subcategory to one in the middle which is a drop down of recently created categories.

Is that possible?


Arnav Joy comments:

ok i a, doing it.


Arnav Joy comments:

Here is the full template code

<?php

/*Template Name: Add Category*/

get_header();

if(isset($_POST['submit'])){

$cat_ID = get_cat_ID( $_POST['newcat'] );

//If not create new category

if($cat_ID == 0) {

$cat_name = $_POST['newcat'];

$parenCatID = $_POST['cat-parent'];

$new_cat_ID = wp_create_category($cat_name,$parenCatID);

echo 'Category added successfully.';
}

else {echo 'A category with the same name exists';}
}

?>

<div id="primary">

<div id="content" role="main">

Form for creating category

<form action="" method="post">

Enter Category Title <input type="text" name="newcat" value="" /><br />

Select Parent Category
<select name="cat-parent" >
<option value="0"><?php echo esc_attr(__('Select Category')); ?></option>
<?php
$categories= get_categories(array('orderby ' => 'id' , 'order' => 'DESC'));
foreach ($categories as $category) {
$option = '<option value="'.$category->term_id.'">';
$option .= $category->cat_name;
$option .= '</option>';
echo $option;
}
?>
</select><br />

<input type="submit" name="submit" value="Submit" />

</form>

</div><!-- #content -->

</div><!-- #primary -->

<?php get_footer(); ?>


Arnav Joy comments:

i think you are looking for this form

<?php

/*Template Name: Add Category*/

get_header();

if(isset($_POST['submit'])){

$cat_ID = get_cat_ID( $_POST['newcat'] );

//If not create new category

if($cat_ID == 0) {

$cat_name = $_POST['newcat'];

$parenCatID = 0;

$new_cat_ID = wp_create_category($cat_name,$parenCatID);

echo 'Category added successfully.<br />';
}

else {echo 'That category already exists<br />';}


$cat_ID = get_cat_ID( $_POST['newcat'] );

//If not create new category
if(!empty($_REQUEST['newsubcat'])){

$cat_ID = get_cat_ID( $_POST['newcat'] );

if($cat_ID == 0) {

$cat_name = $_POST['newsubcat'];

$parenCatID = $_POST['cat-parent'];

$new_cat_ID = wp_create_category($cat_name,$parenCatID);

echo 'Sub Category added successfully.<br />';
}

else {echo 'That Sub category already exists<br />';}
}
}
?>

<div id="primary">

<div id="content" role="main">

Form for creating category

<form action="" method="post">

Enter New Category Title <input type="text" name="newcat" value="" /><br />

Select Parent Category
<select name="cat-parent" >
<option value="0"><?php echo esc_attr(__('Select Category')); ?></option>
<?php
$categories= get_categories(array('orderby ' => 'id' , 'order' => 'DESC'));
foreach ($categories as $category) {
$option = '<option value="'.$category->term_id.'">';
$option .= $category->cat_name;
$option .= '</option>';
echo $option;
}
?>
</select><br />

Enter New Sub Category Title <input type="text" name="newsubcat" value="" /><br />

<input type="submit" name="submit" value="Submit" />

</form>

</div><!-- #content -->

</div><!-- #primary -->

<?php get_footer(); ?>


Jack comments:

Perfect!

However, I create a new category. Click Submit and it saves and says Category Added.
When I then go down to add a sub-category, I type the sub-category title in the box and click the drop-down menu to attach it to a parent category but the category I just created isn't showing.

Any ideas?


Arnav Joy comments:

did you received any msg for that?

also find out following lines

if(!empty($_REQUEST['newsubcat'])){

$cat_ID = get_cat_ID( $_POST['newcat'] );

see name of the variable "newcat" in above line

changed it to "newsubcat"

so the new code will be

if(!empty($_REQUEST['newsubcat'])){

$cat_ID = get_cat_ID( $_POST['newsubcat'] );

now try it after saving it


Jack comments:

Thanks. Tried it and doesn't work though. I've changed some of the titles a bit (i'm calling them Projects instead of Categories) but the idea is still the same. Here's my code:

<?php if(isset($_POST['submit'])){
$cat_ID = get_cat_ID( $_POST['newcat'] );

//Create new Project
if($cat_ID == 0) {
$cat_name = $_POST['newcat'];
$parenCatID = 0;
$new_cat_ID = wp_create_category($cat_name,$parenCatID);
echo 'Project Added<br />';
}

else {echo 'That Project already exists<br />';}
$cat_ID = get_cat_ID( $_POST['newcat'] );

//Creat new Project Folder
if(!empty($_REQUEST['newsubcat'])){
$cat_ID = get_cat_ID( $_POST['newsubcat'] );

if($cat_ID == 0) {
$cat_name = $_POST['newsubcat'];
$parenCatID = $_POST['cat-parent'];
$new_cat_ID = wp_create_category($cat_name,$parenCatID);
echo 'Project Folder added successfully.<br />';
}

else {echo 'That Project Folder already exists<br />';}
}
}

?>


Arnav Joy comments:

1.are you getting any msg sucess or fail?

2.also share your full code here

3. which wp version are you using


Jack comments:

I'm using Wordpress 3.4.
No message.
Code is here: [[LINK href="http://pastebin.com/YAbGHYs4"]][[/LINK]]


Jack comments:

Ok, i do get a message.

If i type something in the 'Add New Project Folder' (sub-category) field, and click submit, it says:
Project Added
Project Folder added successfully.

In case it's not clear, here's what i'm after:
First field is a single line text input that allows a user to add a new parent category and also check to see if it already exists or not.
Second field is a single line text input that allows a user to add a new sub-category, check to see if it already exists or not and add it to a parent category using a drop down box (3rd field).

At the moment, any new project is not being listed in the drop-down box. I'm guessing this is because it has no posts associated with it and is just an empty category. I really need this to be populated with a list of all categories though, whether empty or not and to show sub-categories too (denominated by a '-' beside them and listed under their parents).


Arnav Joy comments:

so the subcategory part also works , right?


Arnav Joy comments:

find following lines

$categories= get_categories(array('orderby ' => 'id' , 'order' => 'DESC' ));

and replace it with

$categories= get_categories(array('orderby ' => 'id' , 'order' => 'DESC' ,'hide_empty' => 0 ));


Jack comments:

Thanks. That now shows all the categories however, I cannot create a subcategory at all.
I type something in the bottom box (Add a new project folder), click the drop-down and select a parent category but it just adds it as a new parent instead.


Arnav Joy comments:

show me your full code


Arnav Joy comments:

find the following lines


<select name="cat-parent" >
<option value="0"><?php echo esc_attr(__('Select Category')); ?></option>
<?php
$categories= get_categories(array('orderby ' => 'id' , 'order' => 'DESC' ,'hide_empty' => 0 , 'hierarchical' => true));
foreach ($categories as $category) {
$option = '<option value="'.$category->term_id.'">';
$option .= $category->cat_name;
$option .= '</option>';
echo $option;
}
?>
</select>


replace it with
<?php wp_dropdown_categories(array('hide_empty' => 0, 'name' => 'cat-parent' , 'orderby ' => 'id' , 'order' => 'DESC' , 'hierarchical' => true , 'show_option_none' => 'Select category'));?><br />

so you will get category dropdown with parent child relationahip


Jack comments:

Hi Sorry,
Been with a client for most of the day.

I will next have a chance to look at the code on Wednesday. I'll get back to you about it then.

Thanks.
-Jack


Jack comments:

This is great and works perfectly however, it gives me a 'Category Added' message as well as the 'Sub-category added' confirmation message when only creating a new sub-category.


Arnav Joy comments:

ok i will correct it


Arnav Joy comments:

Use following code
<?php



/*Template Name: Add Category*/



get_header();



if(isset($_POST['submit'])){

if(!empty($_REQUEST['newcat'])){

$cat_ID = get_cat_ID( $_POST['newcat'] );



//If not create new category



if($cat_ID == 0) {



$cat_name = $_POST['newcat'];



$parenCatID = 0;



$new_cat_ID = wp_create_category($cat_name,$parenCatID);



echo 'Category added successfully.<br />';

}



else {echo 'That category already exists<br />';}


}



if(!empty($_REQUEST['newsubcat'])){



$cat_ID = get_cat_ID( $_POST['newcat'] );



if($cat_ID == 0) {



$cat_name = $_POST['newsubcat'];



$parenCatID = $_POST['cat-parent'];



$new_cat_ID = wp_create_category($cat_name,$parenCatID);



echo 'Sub Category added successfully.<br />';

}



else {echo 'That Sub category already exists<br />';}

}

}

?>


above following line
<div id="primary">

just replace all the <?php ?> content


Jack comments:

Works perfectly!
Thank you!

2012-06-17

Jatin Soni answers:

Try to use this

<?php wp_create_category( $cat_name, $parent ); ?>

you can pass variable if you want just check for more details

http://codex.wordpress.org/Function_Reference/wp_create_category


Jack comments:

So I just add that to my page template and that should show a 'Create new Category' input with save button?


Jatin Soni comments:

Try something with below
wp_create_category($new_category);
than get value with this variable from input field and you may need to use it into the form action too.


Jack comments:

Ok. I appreciate your reply but...

Firstly, that's practically the same information as on the Stack Exchange website, which I mentioned I didn't understand.
Secondly, as also mentioned, I really need step-by-step instructions on how to implement this feature.

Thanks.


Jatin Soni comments:

Okay let me try to give you detail answer. By the way I use wordpess codex first


Jatin Soni comments:

I may need time to find complete solution. Meanwhile you can check this if it works for you. http://soderlind.no/archives/2011/09/25/front-end-editor-in-wordpress-3-3/#comments

I am sure other expert here will reply you too.


Jatin Soni comments:

Cool Arnav,

I have modified Arnav's code to check either category exists or not and echo message accordingly.

Definatly Arnav has created main code and I am adding other function to fulfill your requirements.

<?php

/*Template Name: Add Category*/

get_header();

if(isset($_POST['submit'])){

$cat_ID = get_cat_ID( $_POST['newcat'] );

//If not create new category

if($cat_ID == 0) {

$cat_name = $_POST['newcat'];
echo '<h2 style="color:#005500;">Category '.$cat_name.' is created.</h2>';
return $cat_name;

} else {
echo '<h2 style="color:#f80000;">Category alread exists.</h2>';
return;
}

}

?>


<div id="primary">

<div id="content" role="main">

Form for creating category

<form action="" method="post">

Enter Category Title <input type="text" name="newcat" value="" /><br />

<input type="submit" name="submit" value="Submit" />

</form>



</div><!-- #content -->

</div><!-- #primary -->


<?php get_footer(); ?>

2012-06-17

Just Me answers:

Arnav Joy provided a good start.

Just wanted to point out that you might want to check whether the category already exists before adding it.
And maybe also provide a feature to add subcategories.

Another thing to think about is how to go about wrongly created (misspelled) categories.


Jack comments:

Just Me, all your points are excellent ones.
Can you adapt the above code to incorporate them?


Just Me comments:

maybe Arnav Joy would like to have a go at it first. I don't want to get in anyone's way.