Metaboxes

Metaboxes

/ Extensions  /Metaboxes

If you are used the ReduxFramework declaration arrays, you know Redux Metaboxes. They’re one and the same! The only difference is a parent array. We’ll go through a few examples to get you up and running. Please know, you MUST have an options page for metaboxes to work. They’re inter-connected. You can, however, make a fake options panel, and hide it from the admin menu. But the metaboxes extensions works best when united with a panel.

Getting Started

To understand how to use extensions, you should read this article on Loading Extensions. To shortcut the process, you should use the Redux Generator.

Arguments

NameTypeDescription
idstringYou must provide an ID of some kind. This can be shared with your keys from your panel, and if so the values will be overridden for that given page in your global variable.
titlestringThis is the title that appears on the box
post_typesarrayProvide any number of post_types for a given metabox to appear.
page_templatearrayBind visibility of a metabox to any number of page templates. The value will be equal to the filename of the custom page template.
post_formatarrayBind the visibility of a metabox to a given post format. List of post formats.
positionnormal / advanced / sideWhere you want to place your metabox. Normal and advanced are the to left and side is, well, to the side.
priorityhigh / core / default / lowWhere you want to place your metabox. Normal and advanced are the to left and side is, well, to the side.
sectionsarrayYour sections array, the same style as ReduxFramework.

Example Declaration

Since we’ve kept the structure exactly the same, start by constructing a section array like you normally would. In the included config file for metaboxes, we save these to an array. We’ve named that array $boxSections for the below examples.

Now once we have that array, we need only add it to a parent array, named below as $metaboxes. Here’s an example of how we would add a sidebar metabox to a page.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// BE SURE TO RENAME THE FUNCTION NAMES TO YOUR OWN NAME OR PREFIX
if ( !function_exists( "redux_add_metaboxes" ) ):
     function redux_add_metaboxes( $metaboxes ) {
         // Declare your sections
         $boxSections = array ();
         $boxSections [] = array (
             //'title'         => __('General Settings', 'redux-framework-demo'),
             //'icon'          => 'el-icon-home', // Only used with metabox position normal or advanced
             'fields'        => array (
                 array (
                     'id' => 'sidebar' ,
                     //'title' => __( 'Sidebar', 'redux-framework-demo' ),
                     'desc' => 'Please select the sidebar you would like to display on this page. Note: You must first create the sidebar under Appearance > Widgets.' ,
                     'type' => 'select' ,
                     'data' => 'sidebars' ,
                 ),
             ),
         );
 
         // Declare your metaboxes
         $metaboxes = array ();
         $metaboxes [] = array (
             'id'            => 'sidebar' ,
             'title'         => __( 'Sidebar' , 'fusion-framework' ),
             'post_types'    => array ( 'page' , 'post' , 'acme_product' ),
             //'page_template' => array('page-test.php'), // Visibility of box based on page template selector
             //'post_format' => array('image'), // Visibility of box based on post format
             'position'      => 'side' , // normal, advanced, side
             'priority'      => 'high' , // high, core, default, low - Priorities of placement
             'sections'      => $boxSections ,
         );
 
         return $metaboxes ;
     }
     // Change {$redux_opt_name} to your opt_name
     add_action( "redux/metaboxes/{$redux_opt_name}/boxes" , "redux_add_metaboxes" );
endif ;

Example Usage

This is one of the things that makes the metaboxes ultra slick. ReduxFramework creates a global variable for you to access all your panel options with. Say your opt_name is redux_demo, then you can use the global variable $redux_demo to access your values. Now what’s cool is the metaboxes code overrides those values! That means you only have to code your functions ONCE. So say you have a layout field in your panel and on your pages. If you change a single post, and have the layout be say 3, while the panel says 2, the code exposed to your data will be 3. Redux metaboxes does it all for you.

Accessing Data Within Loops

Within loops, Redux will automatically update your Redux global variable with the appropriate values. If you’re trying to do it another way, you will need to use the following function:

1
<br /> $value = redux_post_meta( "OPT_NAME" , "THE_POST" , "META_KEY" )<br />

You can use this anywhere to grab a specific value or all values. The OPT_NAME and THE_POST are the required attributes. OPT_NAME is your Redux opt_name. THE_POST can be a post object or just a post ID. If no META_KEY is specified, an array of all meta will be returned. If you specify the META_KEY only that value will be returned to you.

Help! The defaults are not in the array!

You are correct. Redux Metaboxes will not ever store a “default” value. Know that when you query for values. As of Metaboxes 1.4.4 you can set the global argument of metaboxes_save_defaults to enable default values being saved to the database.

Missing Sidebar?!

For boxes in the standard position and with only one section, the sidebar is omitted when the HTML is output. If you want a side bar, use at least two sections within any given standard position metabox.

Binding Metabox Visibility to Page Template & Post Formats

Yes, metaboxes does that for you too. As denoted above, you need only declare page_template or post_format on the box array for a given metabox. The visibility will be affected accordingly. You can provide an array for each. Obviously, page_templates won’t affect a box that only appears on the post type. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值