How To Add New Option Types To Option Tree?

I am using option tree in theme mode and child theme mode and I am trying to add new option types. The new option types will be based off of the post-select option type, but will only list posts from a specific category.

I created a new file and added it to the array of files to be included that begins on line 178 of ot-loader.php. In the new file I cut and pasted the post-select option type that begins on line 905 of /includes/ot-functions-options-type.php. You can see this new file here.

Read More

In my post select option type I appended a unique slug to the function name. I also put that slug on the css class .type-post-select for the format outer setting wrapper. In the post query post array I added a category parameter.

Then I added my new post-select option type to my theme options, hoping it would show only posts in the category I had set. It showed the posts from all categories. Interestingly enough the option was wrapped in .type-post-select. I also created a regular post-select, which did not have the modified css.

As an experiment I tired adding the category parameter to the original post-select option in the same way and it worked exactly as I expected–only showing posts from that category. Unfortunately I need to create 6 new post-selects each showing posts from a different category.

Clearly I am missing a step, but for the life of me I can not find it. I looked for some other place that option types need to be registered or something, but couldn’t find it…

Related posts

1 comment

  1. What you’re trying to do can be accomplished without ever editing the core files in OptionTree. Add your custom option type functions to your themes functions.php and the following code, as well.

    /**
     * Filter to add custom option types.
     *
     * @param     array     An array of option types.
     * @return    array
     */
    function add_custom_option_types( $types ) {
    
      $types['post_select_a_1'] = 'Post Select option type. (_a_1)';
      $types['post_select_a_2'] = 'Post Select option type. (_a_2)';
      $types['post_select_a_3'] = 'Post Select option type. (_a_3)';
      $types['post_select_a_4'] = 'Post Select option type. (_a_4)';
      $types['post_select_a_5'] = 'Post Select option type. (_a_5)';
      $types['post_select_a_6'] = 'Post Select option type. (_a_6)';
    
      return $types;
    
    }
    add_filter( 'ot_option_types_array', 'add_custom_option_types' );
    

    This will auto load your functions into OptionTree and you don’t need to edit any of the core files. When you add new options, there are two requirements. One, all functions must be prepended with ot_type_. Two, when adding to the array of options your new array keys need to match the function name minus ot_type_, you can use either - or _ when creating the key. So if you have a custom function named ot_type_super_awesome you could add it to the filtered array with either:

    $types['super_awesome'] = 'Super Awesome';
    

    or

    $types['super-awesome'] = 'Super Awesome';
    

    I hope that clears up any confusion. On a side note there are two ot_type_post_select_a_5 in the file you created and I’m assuming the last one should be ot_type_post_select_a_6. Cheers!

Comments are closed.