Getting Started With Redux Framework ( Wordpress ), Part 2: Sections, Fields & Config

Final product image
https://phpgr.blogspot.com

This article helps you, the developer, to begin using Redux Framework in the most basic of ways: adding fields. In the past, working with Redux required some understanding of PHP Classes and how they work. With Redux 3.5+ the learning curve has all but disappeared. 

I'm one of the creators of Redux Framework, and in this tutorial, I will discuss the inner structure of Redux, how fields and sections work, and global configuration arguments.

Redux ships with a sample-config.php file containing multiple examples of setting up Redux, as well as examples for almost every field we offer. For more extensive understanding, review the sample config or check out the Redux Documentation.

In order to begin using Redux Framework, it must be loaded. There are two ways in which to make this happen. 

First, Redux can easily be installed as a plugin and activated. The plugin is freely available atWordPress.org. Installation is then done through the plugin installer interface. 

The second way to install Redux Framework is to embed it within your theme or plugin. Embedding is fairly easy to do as well. Simply copy the ReduxCore folder to your theme or plugin (renaming the directory to whatever name you desire) and include the framework.php file found within, through standard PHP include functions. 

If these embed instructions are too difficult, Redux also offers the Redux Builder which allows you to output a fully functional theme with Redux already loaded. Best of all, the builder service is completely free. 

Now that Redux Framework is loaded, we can begin using it.

In order to understand how Redux Framework runs, one must first understand what a Redux instance is. Redux can be run by a theme and any number of plugins at the same time, all from within one WordPress install. Each instance has its own unique set of characteristics and arguments; each can behave very differently from other Redux instances.

The reason Redux runs as it does is simply to ensure the greatest level of flexibility. Far too many option frameworks only allowed for one instance of their framework to run. We wanted plugin and theme developers alike to work with Redux unhindered by one another's code.

How exactly does one keep instances separate? It's all through a unique key called the opt_name. This is where your data is stored in WordPress and also counts as your unique key throughout the Redux Framework. It's important to understand that before you begin working with Redux Framework, you'll need to choose a unique opt_name. Once that determination has been made, it's time to dig into global arguments.

The Redux Framework options panel is built using PHP arrays. The global arguments below apply only to our sample instance of Redux Framework. A full list of global arguments can be found on the Redux website. For the purpose of this tutorial, we will work with a very basic arguments array, and show you how to set it.

Once this code is run, Redux Framework will produce a fully functional panel. The picture below explains how each argument affects this panel.

How each argument affects this panel

Now that our panel is running, let's add some sections and fields. Redux uses nested arrays for fields and arguments, though with Redux 3.5+ these procedures are separated easily. Let's begin by making a section which we can add fields to. For more extensive documentation on sections, have at look at the sections documentation.

Developer Mode

The team behind Redux Framework also maintains Elusive Icons. As such, the Elusive icon set is baked directly into the Redux core. Adding icons is as easy as specifying a class, as in the example. If you wish to have another icon pack, such as FontAwesome, there is documentation for those procedures as well.

Sections may also be converted into subsections for easier organization of your panel. Below is an example, with the example source.

Framework Icons

Setting fields is no different than setting sections or arguments; it all begins with an array. Before we go too far, we need to point out that Redux has over 30 different field "types", each with its own set of arguments and a myriad of options. To see the different types, it's once again suggested to read our docs, over on the Redux Documentation on Fields.

Fields can be added to sections in two ways. First, append the field arrays to the setSections function outlined above. In the examples above you can easily add a "fields" argument and add any number of fields to that section.

The second way requires an additional Redux API function call, but it's just as simple.

Let's look at what this does to our example panel.

Our Example Panel

Take some time to thoroughly examine the different field types. Redux Framework contains some really powerful fields. For example, the typography field auto-enqueues the selected Google fonts to your page. The background field—should the output argument be set to your CSS selector—will auto-generate the required CSS and append to your site. Simply stated, Redux Framework truly does a lot of the heavy lifting for you!

The easiest way to get started is to first look at the Redux demo site. It demonstrates all the different types of fields Redux offers with a bunch of their configurations.

What's really useful is that the demo site config ships with Redux. Inside ~/sample/sample-config.php is everything you see on the demo site.

If you're still lost, there is a thriving community behind Redux that will help, though you'll find most everything you need on the Redux Documentation site.

The complete example config presented in this tutorial is attached for you to download from the right sidebar. This is only an example. The embedded Redux sample-config has many more extensive examples.

In this second part of the series, we took a closer look at how Redux works, and how to get started. We also examined ways in which to add sections and fields to an instance of Redux.

We also looked at sections and how they could be displayed differently, based on arguments and how fields are tied to sections, and how they can be easily customized to match your needs.

In the next tutorial of the series, we are going to look at next steps of really using Redux in your themes and plugins. We'll outline how to use Redux's dynamic CSS and font output. We'll also talk about other advanced features Redux has to offer you.


Getting Started With Redux Framework: Introducing It and Integrating It Into Your Theme

Final product image
https://phpgr.blogspot.com

When site owners install a new WordPress theme, the first thing they want to do is to tweak its look and feel to match their branding or personal preference. And most of the time, the people who buy, download, or install WordPress themes have little or no coding experience.

So how do you, as a theme developer, enable the end users of your theme to have as much control as possible over the look and feel of their site without having to learn to code? 

By creating options pages/panels, of course!

However, theme options pages are not easy to create—especially for those developers who don't have much experience with WordPress or programming in general. Luckily, there are options frameworks.

Options frameworks are code libraries, plugins or classes that provide a solid foundation that developers can easily and quickly build upon to create and add options pages to their themes. This enables non-developers to use such panels to quickly customize the look and feel of their site, without touching code.

Options panels can be used to:

  • upload a logo
  • change the background color or any CSS property of an element (such as the header or footer of your theme)
  • select a pre-defined skin or layout
  • add custom CSS or JavaScript
  • and more

Basically, options panels can place the power into the hands of your themes' users by enabling them to change almost any aspect of their site's design.

In this series, we are going to look at a very popular and widely-used options framework—Redux Framework.

This series will be divided into the following parts:

  • Part 1: Introducing Redux Framework and Integrating It Into Your Theme
  • Part 2: Adding Sections and Fields & Working With Configuration Settings
  • Part 3: Using the Options in Your Theme (and Widgets)
  • Part 4: Alternatives to Redux
  • Part 5: Conclusion

Redux Framework is an options framework that was born out of the combination of four frameworks, namely:

  • NPH, which was created by Lee Mason with the aim of creating a framework that was compatible with ThemeForest's submission requirements
  • SMOF
  • SimpleOptions, which is a merger of the above frameworks, created by Dovy Paukstys
  • An earlier version of Redux (Redux 2.0), created by Daniel Griffiths and based on NPH

The Redux core team consists of these three developers. The project is under active development, with updates released quite regularly.

  • Uses the WordPress Settings API
  • Multiple field types
  • Built-in validation classes
  • Extensible
  • Import/export functionality, enabling you to port settings across sites
  • Free: Redux is free and 100% open source. You can use it in both personal and commercial projects without paying a cent.
  • ThemeForest-Compliant: Redux meets Envato's submission requirements, making it a good option for theme developers wishing to develop themes or plugins to be sold in Envato's marketplaces.
  • Popular: Redux is used by over 250 (known) items in the Envato marketplaces that have sold for well over $6 million. This popularity alludes to the power of this framework.
  • Easy to Use: If you understand how arrays work, than you can use Redux. Getting started with Redux is as simple as creating a configuration object that consists of nothing more than nested arrays.
  • Flexible: If it doesn't quite meet your needs, Redux can be extended to create custom fields and validation/error handling.
  • Well-Documented: Redux is well documented. The Redux website has a knowledge base, codex (developer documentation), and video tutorials.
  • Strong Community: A strong community has mushroomed around Redux. If you are having a little trouble and would like help, you'll probably find someone who has encountered the same problem before. And if it's a feature you require, then more than 50 contributors will be willing to listen (and perhaps add it in the next release).

There are several ways to install Redux. But for the purpose of this tutorial, we are going to install it fromWordPress.org.

Install the plugin as you would any plugin:

  • Go to Plugins > Add New.
  • In the search box, enter Redux Framework and then click Search Plugins.
  • From the results page, click Install Now on the first entry.
  • After you install it, proceed to activate it as you would any other plugin.

To activate demo mode, go to Plugins > Installed Plugins and scroll to Redux Framework. You will see something like this:

A Redux Framework entry on the installed plugin list

Click Activate Demo Mode.

This will activate the demo mode and create a sample options panel called Sample Options in the WordPress Dashboard.

WordPress Dashboard menu showing Sample Options and its submenus

Demo Mode and the Sample Options page will allow you to play around with Redux and explore the various field types that it allows you to create.

Redux Options Panel showing the Home Settings Tab

So far we have installed Redux and activated the Demo mode to allow us to explore the different fields that Redux allows us to create.

To integrate Redux into our theme, we have to initialize it with a new configuration file. Follow these steps to initialize a new instance of Redux:

  • Navigate to the redux-framework directory within your WordPress Plugins directory (wp-content/plugins/) and copy the sample directory to your theme.
  • Open your functions.php file and add the following line of code:

When you refresh your WordPress Dashboard and hover over Sample Options, you will notice that nothing has changed—you still have the same sub-menus as before. This is because our configuration is exactly the same as before. We simply moved the config file from the Redux plugin directory to our theme directory.

Furthermore, you might see the following error message:

An error message caused by including duplicate configuration classes

This is due to the fact that we are loading two identical config files. You can fix this by going to the Plugins panel and deactivating Demo Mode.

A Redux Framework entry in the Plugins panel - with Deactivate Demo Mode option

What we want to do is to start with a blank config file. Redux comes with one for your convenience. So let's link to it instead.

To do so, change the line you just added to your functions.php from:

To:

If you go back to your Dashboard, you will realize that our Sample Options panel contains one section—Home Settingsand one field, a media uploader field aptly named Web Fonts.

Sample Options Page with barebones config activated

We now have a barebones config class that we can add sections and fields to. 

In this first part of the series, we looked at Redux Framework, what is and why it's worth using it in your projects. We went ahead and installed a fresh installation of Redux and examined the different features it affords us as theme developers.

We also created our own barebones configuration files to demonstrate how we can begin to tweak Redux to meet our specific needs.

In the next tutorial in the series, we are going to learn how we can add more sections, and also look at adding fields to these sections.


Remove LI Elements From Output of wp_nav_menu

You can remove or change the <ul> container that you get by default withwp_nav_menu (codex) through parameters, but you can't remove the <li>elements that wrap each menu item. This is how you can actually remove them:

$menuParameters = array(    'container'       => false,    'echo'            => false,    'items_wrap'      => '%3$s',    'depth'           => 0,  );    echo strip_tags(wp_nav_menu( $menuParameters ), '<a>' );