Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Integrate Option Tree for Wordpress theme

Tags:

wordpress

I want to integrate Option Tree framework with Wordpress theme without installing and activating plugin then how to do it ?

like image 776
Umair Ashraf Avatar asked Sep 10 '12 13:09

Umair Ashraf


People also ask

How do I create a custom option in WordPress?

Introduction. Creating custom options panels in WordPress is relatively easy. First, to create the menu item and the new page, see Adding Administration Menus. So long as you stick to this structure, WordPress will handle all of the option creation, update, saving, and redirection for you.


2 Answers

Since version 2.0 the plugin developer has included a number of filters that can be used in your functions.php. These include Theme Mode, and the comments within ot-loader.php state;

   * For developers: Theme mode.
   *
   * Run a filter and set to true to enable OptionTree theme mode.
   * You must have this files parent directory inside of 
   * your themes root directory. As well, you must include 
   * a reference to this file in your themes functions.php.
   * @since     2.0
   */
  define( 'OT_THEME_MODE', apply_filters( 'ot_theme_mode', false ) );

To activate Options Tree in your theme rather than as a plugin you include all the plugin files in the theme's root directory, ie

/wp-content/themes/my-awesome-theme/options-tree

and in functions.php you'd run this filter and then include the ot-loader.php file. I've shown this below, and I have also shown the show_pages filter;

add_filter( 'ot_theme_mode', '__return_true' );
add_filter( 'ot_show_pages', '__return_true' );

require_once ('option-tree/ot-loader.php');

The show_pages filter is useful because after you have set up your theme and your options you would then go in and set it to false so the client isn't given the main Options Tree admin menu and therefore can't start 'tinkering' and trash everything. You change it to;

add_filter( 'ot_show_pages', '__return_false' );
like image 141
McNab Avatar answered Sep 29 '22 02:09

McNab


For anyone using a child theme and getting "failed to open stream" errors when using the OptionTree plugin in Theme Mode, do the following:

ot-loader.php, around line 128, change this:

if ( false == OT_THEME_MODE ) {
        define( 'OT_DIR', plugin_dir_path( __FILE__ ) );
        define( 'OT_URL', plugin_dir_url( __FILE__ ) );
      } else {
        define( 'OT_DIR', trailingslashit( get_template_directory() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
        define( 'OT_URL', trailingslashit( get_template_directory_uri() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
      }

To this:

if ( false == OT_THEME_MODE ) {
        define( 'OT_DIR', plugin_dir_path( __FILE__ ) );
        define( 'OT_URL', plugin_dir_url( __FILE__ ) );
      } elseif ( is_child_theme() ) {
        define( 'OT_DIR', trailingslashit( get_stylesheet_directory() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
        define( 'OT_URL', trailingslashit( get_stylesheet_directory_uri() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
      } else {
        define( 'OT_DIR', trailingslashit( get_template_directory() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
        define( 'OT_URL', trailingslashit( get_template_directory_uri() ) . trailingslashit( basename( dirname( __FILE__ ) ) ) );
      }

The code checks if the theme in use is a child theme ( is_child_theme() ) and sets the dir and url using get_stylesheet_directory() and get_stylesheet_directory_uri().

Hope this helps out anyone else running into this issue.

like image 38
IntricatePixels Avatar answered Sep 29 '22 01:09

IntricatePixels