Support » Plugins » Hacks » Add menus to admin using custom taxonomies

  • Hello,

    I’m developing a plugin for e-learning. It is laid out as courses->lessons+evaluations+final exam.

    So I have figured out how to register_post_types as custom tipes (my_course) and add “lessons” (custom posts) to that post type. However, I am finding it hard to organize this way and would like to add a parent to each course (custom_post_type) by using custom taxonomies. How would I create an admin menu for each course_name using custom taxonomies and assign each custom_post_type specific to that course to that taxonomy?

    Thanks in advance.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    Hello again,

    You can associate any taxonomy term to any object with wp_set_object_terms(). But I don’t understand what you are trying to do by creating an admin menu using custom taxonomies. In any case, since each post type gets an admin menu listing, you already have a menu for each course/post type. Why add another using a taxonomy?

    I think I’m missing something again.

    Hi bcworkz,
    It is difficult to dive into the WP way and try to get a grasp on POSTS, PAGES, CUSTOM POSTS, CATEGORIES, TAXONOMIES, etc etc. and how they relate.

    I am having a difficult time ordering my plugin so that my links and everything looks neat. So for instance, in the URL bar you would see: /vcos/course/lesson1, where each link actually has content that the plugin provides. However after searching (a lot) I found that you can do this by creating CATEGORIES instead of TAXONOMIES (which probably can be used as well somehow). SO, I’m looking for a way to dynamically create my CATEGORIES under the PARENT CATEGORY “vcos” based on the courses in my “courses” table.

    Ok, so what I’ve done so far is to create a menu in the admin:

    add_action( 'admin_menu', 'register_vcos_menu_page' );
    function register_vcos_menu_page(){
        	add_menu_page( 'vcOS', 'vcOS', 'edit_theme_options', 'vcos', 'vcos_admin_menu', plugins_url( 'vcos/images/icon.png' ), 30 );

    Then I created a table in the WP database and called it “courses”. So I created custom post types using my table “courses” to list the courses depending on who the user/author is.

    add_action( 'init', 'codex_custom_init' );
    function codex_custom_init() {
    	global $wpdb;
      	global $current_user;
      	$current_user = wp_get_current_user();
    	$userid = $current_user->ID;
    	//Get user's courses
      	$queries = $wpdb->get_results("SELECT courseid, course FROM ".$wpdb->prefix."vcos_courses WHERE ownerid='$userid'");
    		foreach ($queries as $query){
    			$course = $query->course;
    			$courseid = $query->courseid;
    			$args = array(
    				'public' => true,
    				'label' => $course,
    				'show_ui' => true,
    				'hierarchical' => true,
    				'taxonomies' => array('category'),
    				'show_in_menu' => 'vcos',
        			'rewrite' => array('slug' => $course = $query->course),
        			'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'page-attributes', 'tags')
    			register_post_type( $courseid, $args );
      			wp_register_style( 'vcos', plugins_url('/css/style.css', __FILE__), false, '1.0.0', 'all');

    I don’t know if that’s the best way to go about it, but the menu is working.

    BTW, I am looking for anybody interested in the development of vcOS (just in case).
    Thanks again.

    Moderator bcworkz


    You can dynamically add more category terms by using wp_insert_term().

    It is difficult to dive into the WP way and try to get a grasp on POSTS, PAGES, CUSTOM POSTS, CATEGORIES, TAXONOMIES, etc etc. and how they relate.

    Tell me about it! The generic English words that could have very specific WP meanings is a nightmare when trying to communicate with people from various backgrounds whom you’ve never met. Since it relates to what you’re doing, I’ll try to clarify.

    At the highest level of WP entities, there’s only one kind of content… Posts. And only one kind of tags… Taxonomies. Pages, posts, menus, attachments, etc. are all different types of Posts. The actual content is contained in post objects. You can add your own post types as needed, and of course multiple objects of each. Similarly, Tags and categories are different types of taxonomies. You can add your own taxonomies as needed.

    For any given taxonomy, you can add multiple terms. Terms are the actual names assigned within a taxonomy. So whether you use categories or a custom taxonomy, the result can be the same since categories are a type of taxonomy. Your parent category ‘vcos’ is actually a term for the taxonomy “category”. You add additional category terms as children of “vcos” by specifying the vcos term ID as the parent argument when using wp_insert_term(). You then establish a relationship between any term and any post_type post object with wp_set_object_terms().

    Since all taxonomy terms are contained in the database, you can query the database and list the terms found in any way you need, limited only by your ability to write queries and output the returned data. Of course, with categories, there are several functions to make listing categories easier, the main advantage of using categories instead of a custom taxonomy.

    Hopefully this helps you better organize your content.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add menus to admin using custom taxonomies’ is closed to new replies.