1

Wondering if it's possible to create categories outside control panel. I know its not possible with safecracker. Any add-ons or hack to achieve this?

Thanks in advance.

2

The categories table structures are fairly simple. With EE's ability to run PHP in templates, you can enable PHP, and then use its Database class to insert new data.

$data = array('site_id' => 1, 'group_id' => 2, 'parent_id' => $parent_cat, cat_url_title => 'some_new_category_name', $cat_name => 'Some New Category Name', cat_order = 5);

$sql = $this->EE->db->insert_string('exp_categories', $data);

$this->EE->db->query($sql);

A few things you'd need to do:

  • Dig into EE's code to find a function that converts a cat_name to a cat_url_title, or write your own.
  • Get the MAX cat_order of the categories under the parent category you're in, add 1 to that for the new cat_order. Note that if there's an existing cat_url_title that matches, you'll need to add a number at the end.

You could build any sort of front end you want: A nested display of all categories with "Add Child" links next to each would work. Clicking that link could send you to a form page with the parent ID on the URL. You could do a Select query with that parent ID to find the details you need. (By enabling PHP on output, you can get the ID from the URL using its segment variable, and read that into your PHP.) Then let the user enter a category name, and upon submitting the form, run those values through PHP to create the new category. You could also send that link to a lightbox/modal so that the user doesn't leave the page, and set that up to refresh the page when you close it. Their new category would then be in the list.

We've written PHP code similar to this, with the lookups for existing categories, to batch load categories from CSV files (along with other data), creating categories on the fly as they're needed.

1
  • you can also take a look at models/category_model for somewhat helping methods. the CP has it's own (elaborate) method category_update in the file controllers/cp/admin_content
    – GDmac
    Apr 22 '13 at 11:57

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.