Last active
April 25, 2016 06:47
-
-
Save rodica-andronache/ae614756c938694d5f86 to your computer and use it in GitHub Desktop.
Woocommerce extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://speakinginbytes.com/2014/07/woocommerce-settings-tab/ | |
| If you’re building a WooCommerce extension the easiest thing you can do to improve your UI is to put all WooCommerce settings where users can find them – on a new WooCommerce settings tab. | |
| 1 – Add a New WooCommerce Settings Tab | |
| You just need to add one extra array item to the woocommerce_settings_tabs_array filter. | |
| <?php | |
| class WC_Settings_Tab_Demo { | |
| public static function init() { | |
| add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 ); | |
| } | |
| public static function add_settings_tab( $settings_tabs ) { | |
| $settings_tabs['settings_tab_demo'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' ); | |
| return $settings_tabs; | |
| } | |
| } | |
| WC_Settings_Tab_Demo::init(); | |
| 2 – Add Your Settings to the Settings Tab | |
| That creates a brand new tab for you to use. Now you need to populate it with settings. There’s a handy function, woocommerce_admin_fields, that you can call and pass in an array of settings. There’s a trick here that you can use to make the next step easier. Create one function that only returns your array of settings and then another function that calls the woocommerce_admin_fields function. You’ll thank me in the next step. | |
| You’ll see below that I’m passing in four “settings”. Two of them are actual settings, one it a title, and one is a section end. You’ll want to look through output_fields() in class-wc-admin-settings.php to see all of the available options. | |
| It should look something like this: | |
| <?php | |
| add_action( 'woocommerce_settings_tabs_settings_tab_demo', 'settings_tab' ); | |
| function settings_tab() { | |
| woocommerce_admin_fields( get_settings() ); | |
| } | |
| function get_settings() { | |
| $settings = array( | |
| 'section_title' => array( | |
| 'name' => __( 'Section Title', 'woocommerce-settings-tab-demo' ), | |
| 'type' => 'title', | |
| 'desc' => '', | |
| 'id' => 'wc_settings_tab_demo_section_title' | |
| ), | |
| 'title' => array( | |
| 'name' => __( 'Title', 'woocommerce-settings-tab-demo' ), | |
| 'type' => 'text', | |
| 'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ), | |
| 'id' => 'wc_settings_tab_demo_title' | |
| ), | |
| 'description' => array( | |
| 'name' => __( 'Description', 'woocommerce-settings-tab-demo' ), | |
| 'type' => 'textarea', | |
| 'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ), | |
| 'id' => 'wc_settings_tab_demo_description' | |
| ), | |
| 'section_end' => array( | |
| 'type' => 'sectionend', | |
| 'id' => 'wc_settings_tab_demo_section_end' | |
| ) | |
| ); | |
| return apply_filters( 'wc_settings_tab_demo_settings', $settings ); | |
| } | |
| 3 – Save Your Settings | |
| So by now you should be able to see your settings on your brand new tab. But if you press the save button you’ll notice nothing happens. You still need to hook up the save functionality. Hopefully you took my advice in the last section and you created a separate function which returns an array of settings you just need to pass that exact same array into a save function, woocommerce_update_options(). | |
| <?php | |
| add_action( 'woocommerce_update_options_settings_tab_demo', 'update_settings' ); | |
| function update_settings() { | |
| woocommerce_update_options( get_settings() ); | |
| } | |
| <?php | |
| /** | |
| * Plugin Name: WooCommerce Settings Tab Demo | |
| * Plugin URI: https://gist.github.com/BFTrick/b5e3afa6f4f83ba2e54a | |
| * Description: A plugin demonstrating how to add a WooCommerce settings tab. | |
| * Author: Patrick Rauland | |
| * Author URI: http://speakinginbytes.com/ | |
| * Version: 1.0 | |
| * | |
| * This program is free software: you can redistribute it and/or modify | |
| * it under the terms of the GNU General Public License as published by | |
| * the Free Software Foundation, either version 3 of the License, or | |
| * (at your option) any later version. | |
| * | |
| * This program is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| * GNU General Public License for more details. | |
| * | |
| * You should have received a copy of the GNU General Public License | |
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| * | |
| */ | |
| class WC_Settings_Tab_Demo { | |
| /** | |
| * Bootstraps the class and hooks required actions & filters. | |
| * | |
| */ | |
| public static function init() { | |
| add_filter( 'woocommerce_settings_tabs_array', __CLASS__ . '::add_settings_tab', 50 ); | |
| add_action( 'woocommerce_settings_tabs_settings_tab_demo', __CLASS__ . '::settings_tab' ); | |
| add_action( 'woocommerce_update_options_settings_tab_demo', __CLASS__ . '::update_settings' ); | |
| } | |
| /** | |
| * Add a new settings tab to the WooCommerce settings tabs array. | |
| * | |
| * @param array $settings_tabs Array of WooCommerce setting tabs & their labels, excluding the Subscription tab. | |
| * @return array $settings_tabs Array of WooCommerce setting tabs & their labels, including the Subscription tab. | |
| */ | |
| public static function add_settings_tab( $settings_tabs ) { | |
| $settings_tabs['settings_tab_demo'] = __( 'Settings Demo Tab', 'woocommerce-settings-tab-demo' ); | |
| return $settings_tabs; | |
| } | |
| /** | |
| * Uses the WooCommerce admin fields API to output settings via the @see woocommerce_admin_fields() function. | |
| * | |
| * @uses woocommerce_admin_fields() | |
| * @uses self::get_settings() | |
| */ | |
| public static function settings_tab() { | |
| woocommerce_admin_fields( self::get_settings() ); | |
| } | |
| /** | |
| * Uses the WooCommerce options API to save settings via the @see woocommerce_update_options() function. | |
| * | |
| * @uses woocommerce_update_options() | |
| * @uses self::get_settings() | |
| */ | |
| public static function update_settings() { | |
| woocommerce_update_options( self::get_settings() ); | |
| } | |
| /** | |
| * Get all the settings for this plugin for @see woocommerce_admin_fields() function. | |
| * | |
| * @return array Array of settings for @see woocommerce_admin_fields() function. | |
| */ | |
| public static function get_settings() { | |
| $settings = array( | |
| 'section_title' => array( | |
| 'name' => __( 'Section Title', 'woocommerce-settings-tab-demo' ), | |
| 'type' => 'title', | |
| 'desc' => '', | |
| 'id' => 'wc_settings_tab_demo_section_title' | |
| ), | |
| 'title' => array( | |
| 'name' => __( 'Title', 'woocommerce-settings-tab-demo' ), | |
| 'type' => 'text', | |
| 'desc' => __( 'This is some helper text', 'woocommerce-settings-tab-demo' ), | |
| 'id' => 'wc_settings_tab_demo_title' | |
| ), | |
| 'description' => array( | |
| 'name' => __( 'Description', 'woocommerce-settings-tab-demo' ), | |
| 'type' => 'textarea', | |
| 'desc' => __( 'This is a paragraph describing the setting. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda. Lorem ipsum yadda yadda yadda.', 'woocommerce-settings-tab-demo' ), | |
| 'id' => 'wc_settings_tab_demo_description' | |
| ), | |
| 'section_end' => array( | |
| 'type' => 'sectionend', | |
| 'id' => 'wc_settings_tab_demo_section_end' | |
| ) | |
| ); | |
| return apply_filters( 'wc_settings_tab_demo_settings', $settings ); | |
| } | |
| } | |
| WC_Settings_Tab_Demo::init(); | |
| https://github.com/woothemes/woocommerce/blob/5dcd19f5fa133a25c7e025d7c73e04516bcf90da/includes/admin/class-wc-admin-settings.php#L195 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment