Opt is a framework for creating WordPress plugins, it eases the creation of advanced option pages, shortcodes and WordPress editor buttons.
Let's assume that you want to use Opt in your plugin called "My plugin" (and whose slug is most probably my_plugin)
- Drop the
/optfolder somewhere inside your plugin folder - Include the plugin bootstrap file in your plugin, make sure you get the path right, here is an example, it assumes that the
/optfolder sits on the root of your plugin:
<?php
// wp-content/my_plugin/my_plugin.php
// Include Opt
include dirname( __FILE__ ) . '/opt/opt.php';
/**
* My plugin code
*/- You can access options you defined like this:
<?php
// wp-content/my_plugin/somewhere.php
$all_my_options = opt();
var_dump( $all_my_options ); <?php
// wp-content/my_plugin/somewhere.php
$my_option = opt( 'my_option_id' );
var_dump( $my_option );If an option has a default value, and that option has not yet been set, opt( 'option_id' ) will return the default value for option_id.
This comes in handy when you want to know the default value of an option that has already been set, for example.
<?php
// wp-content/my_plugin/somewhere.php
$my_option = opt_d( 'my_option_id' );
$my_option_default = $my_option[ 'default' ];
var_dump( $my_option_default );It's just pages, tabs, sections and options
Pages, tabs, sections and options definitions for a plugin can reside anywhere inside that plugin, the plugin doesn't force any file structure or naming rules. Yet Opt comes with a nice function that allows you to imprort all your files containing definition at once, the function is opt_dir(), it expects a folder path as the first parameter, example:
<?php
// wp-content/my_plugin/somewhere.php
opt_dir( dirname( __FILE__ ) . '/data' );Calling opt_dir() in the previous example imports (with an include) definitions from the following files:
.../data/pages.php.../data/tabs.php.../data/sections.php.../data/options.php
The framework comes with a few examples demonstrating the different features, you can use them as a starting point, those options are located in the folder /sample-data inside the framework folder. If you want to enable the sample data, set $opt_use_sample_data to TRUE, Like this:
// wp-content/my_plugin/my_plugin.php
$opt_use_sample_data = TRUE;
include dirname( __FILE__ ) . '/opt/opt.php';
Here is an example of defining a page:
<?php
// wp-content/my_plugin/admin/data/pages.php
// Make sure our temporary variable is empty
$pages = array();
$pages[ 'my_page_slug' ] = array(
'title' => __( 'Framework Demo Page' ),
'menu_title' => __( 'Framework Demo' ),
);
// Register pages
opt_pages( $pages );Here is an alternative, passing the array directly to opt_pages() and using brackets ([...]) instead of array(), beware, brackets were introduced in PHP 5.4:
<?php
// wp-content/my_plugin/admin/data/pages.php
// Register pages
opt_pages( [ 'my_page_slug' => [
'title' => __( 'Framework Demo Page' ),
'menu_title' => __( 'Framework Demo' ),
] ] );-
titleThe page title -
menu_titleThe text for the page menu item -
icon_urlThe menu icon, ignored when using parent since subpages don't have icons in WordPress, this parameter accepts the same values you would use in WordPress own add_menu_page(). -
positionThe position in the menu order this menu should appear, as you would use in add_menu_page(). -
parentThe slug name for the parent menu, as you would use in add_submenu_page(). -
submit_button (default='Save Changes')Text for the submit button. -
reset_buttonText for the reset button, if ommitted, there will be no reset button. -
success (default='Settings saved.')Text for the success message.
Registering tabs work in the same way:
<?php
// wp-content/my_plugin/admin/data/tabs.php
$tabs = array();
$tabs[ 'my_tab_slug'] = array(
'title' => __( 'Tab one' ),
'menu_title' => __( 'Tab 1' ),
'page' => __( 'my_page_slug' ),
);
// Register tabs
opt_tabs( $tabs );Most page parameters work for tabs as well but don't forget to specify which page the tabs belong to with the page parameter.
pageThe slug for the page the tab belongs to.
Here is an example of defining a text field:
<?php
// wp-content/my_plugin/admin/data/options.php
$options = array();
$options[ 'my_option_name' ] = array(
'type' => 'text',
'page' => 'page_a',
'title' => __( 'Welcome to my text field' ),
);
// Register options
opt_options( $options );-
pageThe slug of the page the option belongs to. -
tabThe slug of the tab the option belongs to. -
type (default=text)The option typetexttextareacheckboxradioselectmediaproduces an input field with upload functionality
-
titleThe option title -
subtitleA small description under the option title -
descriptionThe text to show under the form field, setting it to~will instruct the framework to output the code that defines the current option. -
placeholderThe placeholder text -
defaultThe default value, use arrays or comma separated values when working withselect,radioorcheckbox. -
valueThe value to show in the form for textual fields -
colorpickerIf set to true for a text input field, it will become a color picker. -
selectedThe value to show in the form for selection based fields, use arrays or comma separated values. -
multipleTellsselectfields to allow multiple choice -
optionsAssociative array of value/text pair that make the available choices forselect,radioorcheckbox.Tip: Accepts also
posts,termsand evensites.Tip: If the text matches an image URL, the image is shown instead of the URL.
-
argsThe parameter to pass to WordPressget_posts()orget_terms()when necessary, i.e, when theoptionsparameter of a selection based field was set topostsorterms. -
taxonomies (defaut=category,post_tag,link_category,post_format)The taxonomies to query when usingtermsas a value foroptionson a selection based form field. -
separator (default=<br />)The separator betweenradioandcheckboxoptions -
width (textarea only)The width for a normal textarea, valid CSS values are expected (px, %, calc...), by default, text areas will span the width of the page. -
height (textarea only)The height for a normal textarea. -
cols (textarea only)The number of columns for a normal textarea. -
rows (textarea only)The number of rows for a normal textarea. -
editorIf set to true for a textarea, it will use a WYSIWYG editor. -
editor_heightAn integer, the height in pixels of the WYSIWYG editor, see this for more information about WYSIWYG height in WordPress. -
textarea_rows (default=20)An integer, the number of rows in the WYSIWYG editor, see this for more information about WYSIWYG height in WordPress. -
teenyIf set to true, the WYSIWYG editor will have less icons. -
media_buttons (default=TRUE)Weither to show the media upload button or not.
You can define and register a shortcode like this:
<?php
// wp-content/my_plugin/admin/data/shortcode.php
$shortcodes = array();
$shortcodes[ 'my_shortcode_a' ] = array(
'text' => __( 'My Shortcode A' ),
'title' => __( 'Fancy description for shortcode A' ),
);
$shortcodes[ 'my_shortcode_b' ] = array(
'image' => 'http://placehold.it/32x32/900/fff/',
'title' => __( 'Fancy description for shortcode B' ),
);
// Register shortcodes
opt_shortcodes( $shortcodes );-
imageAbsolute or relative path to the image used for the button -
textText can be used instead of an image -
wrap (default=false)When set tofalsethe shortcode will replace the text currently selected in the text editor and when set totrue, the selected content will be wrapped in the shortcode. -
funcThis is the function that will handle the shortcode, Opt will use the function called:- The parameter value, for example, if
'func' => 'my_func', the shortcode will be handled by the functionmy_func() - The shortcode tag with
_funcat the end, so if the shortcode tag is[super_tag], the function will besuper_tag_func() - The shortcode tag, so if the shortcode tag is
[super_tag], the function will besuper_tag() - A default function provided by opt that will simply print some information about the shortcode that has been used.
- The parameter value, for example, if
-
parametersan array of fields, these fields definitions ressemble the ones for options. When parameters are found, clicking the shortcode button will open a modal window for building the shortcode with those parameters. -
width (default=0.5)The width percentage of the modal window relative the the page width. -
height (default=0.5)The height percentage of the modal window relative the the page height.