WordPress.org

Ready to get started?Download WordPress

Forums

Define custom taxonomy in plugin instead of theme? (2 posts)

  1. niska
    Member
    Posted 4 years ago #

    In all examples that I've seen about the new custom taxonomy in WP 3.0, they all put the code for custom taxonomy in the themes function.php. But what if I decide to switch theme after a while?? Wouldn't it be better to define the custom taxonomy in a plugin? Why using the theme?

  2. westondeboer
    Member
    Posted 4 years ago #

    so wherever it says in the instructions to put the code into functions.php file, you can just create a plugin for it. example, to create a plugin you just create a file in your plugins folder called mycustomtaxonomy.php and in that file you put....

    <?php
    /*
    Plugin Name: Custom Taxonomy Plugin
    Plugin URI: http://wordpress.org
    Description: Shows my custom taxonomy
    Author: WordPress
    Version: 1
    Author URI: http://wordpress.org
    */
    
    // put custom taxonomy code below here
    // this is just the example from:
    // http://codex.wordpress.org/Custom_Taxonomies
    
    function people_init() {
      // create a new taxonomy
      register_taxonomy(
        'people',
        'post',
        array(
          'label' => __('People'),
          'sort' => true,
          'args' => array('orderby' => 'term_order'),
          'rewrite' => array('slug' => 'person'),
        )
      );
    }
    add_action( 'init', 'people_init' );
    ?>

    Then all you do is enable the plugin and there ya go, it will always be there no matter you change themes or not.

Topic Closed

This topic has been closed to new replies.

About this Topic