Advertisement
  1. Code
  2. WordPress
Code

A Beginners Guide to Titan Framework: Adding an Enable Type Option

by
Difficulty:IntermediateLength:MediumLanguages:
This post is part of a series called A Beginner's Guide to Titan Framework.
A Beginners Guide to Titan Framework: Automatically Generate CSS for Your Options

Providing end users with the option to enable or disable certain features of a premium plugin or a theme is pretty common. Titan Framework has recently pushed a new option to its core which does exactly that. Let's see how you can add an enable/disable toggle button inside an admin panel, a metabox or a theme customizer section with Titan Framework (TF).

The Enable Type Option in Titan Framework

There is an enable type option in Titan Framework to create enable/disable toggle buttons. In the dashboard, the enable type option appears like this: 

Adding enable Type Options

Here are its parameters:

  • name: It assigns the display name of an enable type option.
  • id: This parameter specifies a unique name which gets saved options values.
  • desc: It adds a brief description with the option name.
  • default: (Optional) It assigns the default value.
  • enabled: (Optional) It is the label for the button of the enabled state. The default is set to Enabled.
  • disabled: (Optional) It is the label for the button of the disabled state. The default is set to Disabled.
  • livepreview: (Optional) Each time you add an enable type option in a theme customizer section, this parameter allows you to live preview the changes you make.

All parameters are string by type, except default which is boolean.

Available Containers for the Enable Type Option

You can add this option inside:

  • Admin Panel
  • Admin Tab
  • Metabox
  • Theme Customizer Section

To add an enable type option in these containers, follow these steps:

  • Get an instance via the getInstance() function.
  • Create an option via the createOption() function.
  • Get saved values via the getOption() function.

If you're interested in learning how to create these containers with TF, then read the previous articles of this series.

Creating an Enable Type Option Inside an Admin Panel

Example Declaration

Let's create this option in an admin panel first.

At line #8, I added an enable type option via the createOption() function in an admin panel $aa_panel. As this function takes up an array, I've defined the values of the parameters name, type, id, desc and default. The value of ID must be unique, i.e. aa_enable_opt. The value of default is set to true, which means the option's status is set to 'enabled' by default.

The Enable Type option in the Neat Options

In the above screenshot, there is an enable type option named Allow Custom CSS in an admin panel Neat Options.

Example Usage

Let's get the saved values.

To get the saved value, first get an instance via the getInstance() function (line #3). Then at line #6, the getOption() function retrieves the saved value by registering the option ID aa_enable_opt as its parameter. I saved the result inside a new variable $aa_enable_opt_val. Then I used an if-else check which prints the saved value at the front-end.  

Now the enable type option can hold two values: either 1/0 or true/false. The value is either '1' (enabled state) or '0' (disabled state). Considering this fact, I wrote lines 15 to 20 of the code, which checks the value of the variable $aa_enable_opt_val. If it is '1' then it prints 'Custom CSS in admin panel is enabled' at the front-end. If the value of the variable is '0' then it prints 'Custom CSS in admin panel is disabled'.

Displaying the Result at the Front-End

Suppose I enabled the option and saved it.

Saving the value for the enable type option

Here is a screenshot of the final result which appeared at the front-end.

The result of the option as viewed on the front-end

Creating an Enable Type Option Inside an Admin Tab

Example Declaration

Next I'll create this option in an admin tab.

Here, I added this option in an admin tab $aa_tab1. The value of the ID is aa_enable_opt_in_tab. This time, I've also defined new labels for the enabled and disabled parameters. 

Adding the enable type option in a tabbed interface

In the image, there is an enable type option inside Tab 1 of panel Neat Options 2. Also note the newly defined labels 'On' for enable and 'Off' for disable.

Example Usage

Now I'll get saved values.

The code is pretty much similar to what I wrote above. So, let me summarize all the steps:

  • Get a unique instance via the getInstance() function at line #4.
  • Get the saved value via the getOption() function at line #7.
  • Print the saved value at the front-end via the if-else check statement on lines 15 to 20.

Displaying the Result at the Front-End

This time I toggled the option to Off. The front-end prints the following line:

Displaying the result of the option on the front-end

Creating an Enable Type Option Inside a Metabox

Example Declaration

Now I'll create an enable type option in a metabox.

Here I added yet another enable type option in a metabox $aa_metbox. Its ID is aa_enable_opt_in_metabox. The value of default is set to false.

Adding the enable type option in a meta box

In the above screenshot, you can find this option in a metabox. It appears on all page and post editing screens.

Example Usage

Let's get the saved value for this option.

I've written almost the same code again. The only difference can be spotted at line #6. There exists a new parameter inside the getOption() function. This is the get_the_ID() function which is used to retrieve a specific page or post ID.

Displaying the Result at the Front-End

Let's again enable the option and save it. The front-end appears like this:

Viewing the result of the option as saved in the meta box

Creating an Enable Type Option Inside a Theme Customizer Section

Example Declaration

Finally, I'll create this option in a theme customizer section.

Here I created this option in a theme customizer section $aa_section1. Its unique ID is aa_enable_opt_in_sec. The rest of the parameters are the same.

Adding the enable type option into the Customizer

In the above image, there's an enable type option in a theme customizer section named My Section.

Example Usage

Use the following code to get saved values.

Once again, the code is exactly the same. Only the ID and variable names are different.

Displaying the Result at the Front-End

Here is the screenshot, which displays the changes in live preview mode.

Viewing the enable type option in the Customizer

Conclusion

From an end user's point of view, the enable type option is quite useful, as you can enable or disable any of the features with just a single click. So go through this tutorial and share your views about it in the comments section.

At this point, we've covered all there is to cover about the Titan Framework. Please remember to checkout the series page if you have any questions or comments about any of the topics that we've covered. Or feel free to reach out to me on Twitter.

Advertisement
Advertisement
Looking for something to help kick start your next project?

Envato Market has a range of items for sale to help get you started.