add_filter: swp_options

  1. Home
  2. Knowledge Base
  3. Developers
  4. add_filter: swp_options

Not the solution you were looking for?

Click the link below to submit a support ticket. If you are a Social Warfare - Pro license holder, you can log into your Account page to submit a ticket.
Submit Ticket

Description

This filter hook modifies the options presented on the Social Warfare options pages. The filter is passed an array of options that can be modified and then returned to add, remove, or modify options.

Example Usage

// Queue up your filter to be ran on the swp_options hook.
add_filter('swp_options', 'swp_options_function', 10);

// Create the function that will filter the options
function swp_options_function($swp_options) {
    
    // Declare the options tab and tab name
    $swp_options['tabs']['links']['tabname'] = 'The Tab Name';
 
    // Add an array of options to the tab we created above
    $swp_options['options']['tabname'] = array(

        // The first option on this page: A checkbox
        'googlePlus' => array(
            'type' => 'checkbox',
            'content' => 'Google Plus',
            'default' => 1
        ),

        // The second option on this page: A color selector
        'floatBgColor' => array(
            'type' => 'colorselect',
            'name' => 'Floating Background Color',
            'default' => '#ffffff'
        )
    );

    // Be sure to return the modified options array or the world will explode
    return $swp_options
};

Parameters

This function makes use of WordPress default add_filter function.

$tag
(string) (required) The name of the existing Filter to Hook the $function_to_add argument to. In this instance, you will use swp_options.
Default: None

$function_to_add
(callback) (required) The name of the function to be called when the custom Filter is applied.
Default: None

$priority
(integer) (optional) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the filter.
Default: 10

The following are the built in options pages and their priorities. This guide can allow you to place your options tab wherever you feel most appropriate.

  • 1: Display Settings
  • 2: Display Locations
  • 3: Visual Options
  • 4: Link Shortening
  • 5: Frame Buster
  • 6: Analytics
  • 7: Click to Tweet
  • 99: Registration
  • 100: System Status

$accepted_args
(integer) (optional) The number of arguments the function(s) accept(s). This one is irrelevant to this filter because Social Warfare will always pass only one parameter to your function.
Default: 1

Arguments Overview

$swp_options
(array) An array of existing options will be passed into your filter function. After modification, this array must be returned.

Although the options array contains a lot of information, there are only two sections that need to be modified in order to add your options.

$swp_options['tabs']['links']['yourtabname'] = 'Your Tab Name';

and

$swp_options['options']['yourtabname'] = array();

The first item declares the new options tab and assigns the name that will be displayed on it.

The second contains an array of arrays that contain all the information necessary to create each option that will appear on that page. The key (yourtabname) for these must match in order for the options to be added to that tab.

There are many options available to be added. Each option is added by passing an array of parameters into your options array. This can accept as many arrays as you wish to add to any given page.

Arguments per Option

Each options array contains between two and five arguments necessary to work. I’ll introduce each argument here and then provide more specific usage examples for each option type below.

  • key: A key is used to define the variable that we’ll be able to use to retrieve the option value later. The key must be completely unique because it will be used to fetch the user’s preference for the option on the other side. It is recommended that you prefix your keys with a 2 or 3 digit prefix to avoid conflicts with core options or other addons. For example, if you create a multilingual addon, you might use ml_option.
  • type: This defines which type of option you are adding to the page. This can include the following: title, paragraph, textbox, select, sortable, checkbox, and colorselect.
  • content: Although this varies based on the type selected, this argument generally defines the information that the end user will be able to see and select from. For items with multiple items like a select option, this will require an associative array of values.
  • name: This will provide the label or title that the end user will see for certain option types.
  • default: For many option types, you may want to select a default value. This will set the option to this value until an end user adjusts it to their own preference.

Option Type: Title

The title is used to add h2 tags throughout your options tab and only requires two parameters: type and content. This is usually the first item added to a page to allow users to identify what page/tab they are on.

'title' => array(
    'type' => 'title',
    'content' => 'Your Title Goes Here'
)

Option Type: Paragraph

The paragraph is used to add a paragraph of information to the page in order to help guide users as to what they can do on your options tab. It requires two parameters: type and content.

'description' => array(
    'type' => 'paragraph',
    'content' => 'Your paragraph of text goes here. You can even include some html here'
)

Option Type: Textbox

The textbox provides users a place to input text that you can use on your addon. This field requires three parameters: type, key, and content.

'twitterID' => array(
    'type' => 'textbox',
    'content' => 'Twitter Username'
 )

Option Type: Select

The select provides users a place to select one choice from multiple options. This field requires five parameters: type, name, key, content, and default. The content parameter must be an array of key/label choices for the user to select.

'locationPost' => array(
    'type' => 'select',
    'name' => 'Location on Posts',
    'content' => array(
        'choiceone' => 'Choice One',
        'choicetwo' => 'Choice Two'
    ),
    'default' => 'both'
 )

Option Type: Sortable

The sortable option provides a drag-and-drop list of options whereupon users can resort from a set of options that you provide. This field requires five parameters: type, name, key, and content.

'orderOfIcons' => array(
    'type' => 'sortable',
    'name' => 'Order of Icons',
    'content' => array(
        'googlePlus' => 'Google Plus',
        'Twitter' => 'Twitter',
        'Facebook' => 'Facebook',
        'Pinterest' => 'Pinterest',
        'LinkedIn' => 'LinkedIn'
    )
)

Option Type: Checkbox

The checkbox allows users to turn options on or off. This field requires four fields: type, key, content, default. The default accepts either 0 for off (unchecked) or 1 for on (checked).

'totesEach' => array(
    'type' => 'checkbox',
    'content' => 'Show Individual Network Shares',
    'default' => 1
)

Option Type: Color Select

The color select option provides users with a color wheel allowing them to dynamically select or directly input a hexidecimal color code. This field requires four fields: type, key, name, and default.

'floatBgColor' => array(
    'type' => 'colorselect',
    'name' => 'Floating Background Color',
    'default' => '#ffffff'
)

Add an Option to an Existing Tab

See Function: swp_add_option_after() for documentation on how to add options in the middle of an existing options page.

Conclusion

This should provide you everything that you need in order to create the options necessary to power the cool addon that you want to create for Social Warfare. For further information, you can dig through the source file in the plugin to see how it all works.

/functions/options-page.php

Was this article helpful?

Related Articles