031_TF_Herman_How-does-Titan-Framework-make-plugin-and-theme-coding-easierMany other frameworks claim to make coding easier, and Titan Framework is no exception. But what characteristics of a framework would comprise as “easy” in usage?

To demonstrate this, let’s see the typical workflow of making WordPress options menus the classic way, and the Titan Framework way.

Ordinary WordPress Way

1. Create function and hooks for menu making.
2. Write register_setting.
3. Write add_settings_section.
4. Write add_settings_field for every option needed and their needed functions.
5. Write the needed HTML code for those setting fields and options.
6. Write handlers for updating values for the options.
7. Style the options and load those styles.
8. Use get_option to retrieve values.

The Titan Framework Way

1. Include Titan Framework
2. Setup identifier and initial calls (whether we use admin panels, post meta or the like).
3. Create option arrays and their designated forms and values.
4. Include Titan Framework in functions where data will be pulled, and get its values where needed.
5. An understandable and clean recipe for your plugins
6. Profit in selling your creation to the market

Okay, we were sort of forecasting the benefit of including Titan Framework with the extra 2 steps in the Titan Framework Way, but it is not that far-fetched. We would like to emphasize that in Titan Framework, styling and settings building is done by the framework itself, shaving lots of time from making simple setting options. You would only worry about making the options and connecting them together, and nothing more.

The flexibility of styling everything from the ground up in the ordinary WordPress way would be negated by the fact the same is possible with Titan Framework.

But all that enumerating would mean nothing without an actual application.

For that, let’s dissect a plugin that uses typical WordPress options, and we’ll try to convert it to Titan Framework. Along the way, we’ll find out what makes Titan Framework deceptively powerful.

Let’s use this sample plugin that uses a taxonomy for labeling guest posts. Get the sample plugin here and you’ll see it has a settings page that allows to set a customized description.

After you’ve seen it and given it a spin, head over to includes/admin.php and see the source code.

Seen it? Good. See how those functions and callbacks populate most of admin.php? It’s time to change that into something slim.

First, copy the file titan-framework-checker.php to the folder that we will convert.

Now, open your guestauthors.php file and add this line in the row where Load files are.

require_once('titan-framework-checker.php');​

Commit the changes, and move on to includes/admin.php and see what will be replaced.

Using conventional WordPress Options

/** * Functions that happens on the backend. */if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }// Register menu item. function guestposts_admin_menu_setup(){ add_submenu_page( 'options-general.php', 'Guest Authors Settings', 'Guest Authors', 'manage_options', 'guest_posts', 'guestposts_admin_page_screen' ); } // Menu setup. add_action('admin_menu', 'guestposts_admin_menu_setup');// Display page content. function guestposts_admin_page_screen() { global $submenu;// Access page settings. $page_data = array(); foreach($submenu['options-general.php'] as $i => $menu_item) { if($submenu['options-general.php'][$i][2] == 'guest_posts') { $page_data = $submenu['options-general.php'][$i]; } } // Output. ?> <div class="wrap"> <?php screen_icon();?> <h2><?php echo $page_data[3];?></h2> <form id="guestposts_options" action="options.php" method="post"> <?php settings_fields('guestposts_options'); do_settings_sections('guest_posts'); submit_button('Save options', 'primary', 'guestposts_options_submit'); ?> </form> </div> <?php } // Settings link in plugin management screen. function guestposts_settings_link($actions, $file) { if(false !== strpos($file, 'msp-helloworld')) { $actions['settings'] = '<a href="options-general.php?page=guest_posts">Settings</a>'; } return $actions; } // The filter for it. add_filter('plugin_action_links', 'guestposts_settings_link', 2, 2); // Register settings. function guestposts_settings_init(){ register_setting( 'guestposts_options', 'guestposts_options', 'guestposts_options_validate' ); add_settings_section( 'guestposts_authorbox', 'Author's box', 'guestposts_authorbox_desc', 'guest_posts' ); add_settings_field( 'guestposts_authorbox_template', 'Template', 'guestposts_authorbox_field', 'guest_posts', 'guestposts_authorbox' ); } // Action for init on admin_init. add_action('admin_init', 'guestposts_settings_init'); // Validate input. function guestposts_options_validate($input){ global $allowedposttags, $allowedrichhtml; if(isset($input['authorbox_template'])) { $input['authorbox_template'] = wp_kses_post($input['authorbox_template']); } return $input; } // Echo the description text. function guestposts_authorbox_desc(){ echo "<p>Enter the template markup for author box using placeholders: [gauthor_name], [gauthor_url], [gauthor_desc] for name, URL and description of author correspondingly.</p>"; } // Fill output with pre-filled data. function guestposts_authorbox_field() { $options = get_option('guestposts_options'); $authorbox = (isset($options['authorbox_template'])) ? $options['authorbox_template'] : ''; $authorbox = esc_textarea($authorbox); //sanitise output ?> <textarea id="authorbox_template" name="guestposts_options[authorbox_template]" cols="50" rows="5" class="large-text code"> <?php echo $authorbox;?> </textarea> <?php }

The Titan Framework Way

/** * Functions that happens on the backend. */if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }// Register all settings. function guestposts_admin_menu_setup(){$titan = TitanFramework::getInstance( 'guest-posts' );$adminPanel = $titan->createAdminPanel( array( 'parent' => 'options-general.php', 'title' => 'Guest Authors Settings', 'name' => 'Guest Authors', 'capability' => 'manage_options', 'id' => 'guest_posts', ) ); $adminPanel->createOption( array( 'type' => 'custom', 'name' => "Author's box", 'custom' => "Enter the template markup for author box using placeholders: [gauthor_name], [gauthor_url], [gauthor_desc] for name, URL and description of author correspondingly.", ) ); $adminPanel->createOption( array( 'type' => 'textarea', 'name' => 'Template', 'id' => 'authorbox_template_tf', )); $adminPanel->createOption( array( 'type' => 'save', ) ); } add_action( 'tf_create_options', 'guestposts_admin_menu_setup' ); // Settings link in plugin management screen. function guestposts_settings_link($actions, $file) { if(false !== strpos($file, 'guest-posts')) { $actions['settings'] = '<a href="options-general.php?page=guest_posts">Settings</a>'; } return $actions; } // The filter for it. add_filter('plugin_action_links', 'guestposts_settings_link', 2, 2);

Note that the two codes does the exact same thing.

It presents an admin page, styles it, saves info and synchronizes it with saved settings. The only difference is that the other code uses Titan Framework and uses a single class. (That other class is only for showing the settings link in the Plugins admin page, which we retained.)

The mantra of simplicity is written everywhere with Titan Framework, while still achieving functionally identical results. And with that, you’ve managed to shave roughly 55% of the original code length while still doing the same thing! (That’s around 35 lines of raw codes instead when you use Titan Framework, compared to 85!)

Value retrieval is likewise the same, with slight additions. Open the file includes/core.php and make the replacement.

Using conventional WordPress Options

// Create author's box markup. function guestposts_author_block(){     global $post;     $author_terms = wp_get_object_terms($post->ID, 'gauthor');      if(empty($author_terms)) {         return;     }     $name = stripslashes($author_terms[0]->name);     $url = esc_url(get_term_link($author_terms[0]));     $desc = wp_filter_post_kses($author_terms[0]->description);     // Get template from option.     $options = get_option('guestposts_options');     $out = (isset($options['authorbox_template'])) ? $options['authorbox_template'] : '';     $out = str_replace(         array('[gauthor_url]', '[gauthor_name]', '[gauthor_desc]'),         array($url, $name, $desc),         $out     );     return $out; }

The Titan Framework Way

// Create author's box markup. function guestposts_author_block(){     global $post;     $titan = TitanFramework::getInstance( 'guest-posts' );     $author_terms = wp_get_object_terms($post->ID, 'gauthor');     if(empty($author_terms)) {         return;     }     $name = stripslashes($author_terms[0]->name);     $url = esc_url(get_term_link($author_terms[0]));     $desc = wp_filter_post_kses($author_terms[0]->description);     // Get template from option.     $options = $titan->getOption( 'authorbox_template_tf' );     $out = !empty($options) ? $options : '';     $out = str_replace(         array('[gauthor_url]', '[gauthor_name]', '[gauthor_desc]'),         array($url, $name, $desc),         $out     );     return $out; }

In terms of getting value, it’s quite familiar. You can directly refer to the variable while the framework instance disambiguate it. This is very similar to get_option in WordPress.

Instead of writing several functions and callbacks that would confuse and clutter your code, you’d be saving lines of code, your time, and effort by using Titan Framework by writing only one function for calling everything; the rendering of the menu, the input validation, value population, html form, and value fetching. You can also see at a glance what does which in the code.

Get the sample plugins here with the conventional WordPress options and the other using Titan Framework and see for yourself.

Leave a Reply