-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathinit.php
More file actions
177 lines (157 loc) · 7.3 KB
/
init.php
File metadata and controls
177 lines (157 loc) · 7.3 KB
1
2
3
4
5
6
7
8
9
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/*
Plugin Name: NOUVEAU Meta Boxes Example
Plugin URI: http://nouveauframework.com/downloads/plugins/
Description: A simple, functional WordPress plugin that serves as an example for creating new meta boxes for use on admin editor screens.
Author: Matt van Andel
Version: 1.1
Author URI: http://mattstoolbox.com/
License: GPLv2 or later
*/
namespace NV\Plugins;
NV_Example_Meta_Boxes::init();
/**
* Example class for creating custom meta boxes in WordPress. Uses a little magic, but can also be overridden for
* folks who want a little more control.
*
* You can add custom meta boxes in just two steps…
*
* 1. Register your new meta boxes using add_meta_box() within the register_metaboxes() method below.
* 2. Register any settings/form elements you want using MetaBox::register_setting() within the register_settings() method below.
* 3. Customize your CSS and Javascript, if desired, in the assets folder.
*/
class NV_Example_Meta_Boxes {
/**
* STEP 1: REGISTER YOUR METABOXES HERE…
*
* Register any new meta boxes here. Use the magic callback array( '\NV\Plugins\MetaBox', 'build_metabox' ) to
* automatically create a new
*
* Note: These examples fetch all post types and add meta boxes to all of them. Customize to your own needs.
*
* http://codex.wordpress.org/Function_Reference/add_meta_box
*/
public static function register_metaboxes() {
//Get all registered post types...
$screens = get_post_types();
// Loop through the post types and add meta box to each
foreach ( $screens as $screen ) {
// EXAMPLE 1: Add a meta box using the magic form builder function
add_meta_box(
'magic-meta-box', // HTML slug for box id
__( 'Magic Custom Meta Box', 'nvLangScope' ), // Visible title
array( '\NV\Plugins\MetaBox', 'build_metabox' ), // Magic callback. Used to render the meta box HTML
$screen, // The slug of the post type you want to add this meta box to.
'side', // Context. Where on the screen should this show up? Options: 'normal', 'advanced', or 'side'
'high' // Priority. Options: 'high', 'core', 'default' or 'low'
);
// EXAMPLE 2: Add a meta box using a handcrafted metabox
add_meta_box(
'custom-meta-box', // HTML slug for box id
__( 'Custom Meta Box', 'nvLangScope' ), // Visible title
function ( $post, $args ) { // Anonymous function to include metabox template
include 'templates/custom_metabox.php';
},
$screen, // The slug of the post type you want to add this meta box to.
'side', // Context. Where on the screen should this show up? Options: 'normal', 'advanced', or 'side'
'high' // Priority. Options: 'high', 'core', 'default' or 'low'
);
}
}
/**
* STEP 2: REGISTER YOUR SETTINGS…
*
* Register your settings here with MetaBox::register_setting(). Once registered, saving is handled for you and you
* have access to functions that can automatically generate metaboxes and/or fields.
*
* Examples are provided below. The register_setting() method takes 3 arguments…
*
* $meta_key Required. String. The database key the data will be saved under & the id and name attributes for form elements.
* $box_id Required. String. The id/slug of the meta box that this setting should be associated with.
* $args Optional. Array. Additional customization of your setting as an associative array. Options include…
*
* 'label' The visible text to use for the form elements <label>
* 'type' The type of form field to use with this setting. Valid values include: text, textarea, select, radio, checkbox, hidden
* 'placeholder' Text to use as the form elements HTML5 placeholder attribute.
* 'value' The default value or selection for the form element.
* 'list' An associative array of items to include in radio, checkbox, and select elements, in 'value' => 'Display Text' format
* 'howto' Help text to display beneath the form element.
* 'save' Whether or not the setting should be automagically saved by the plugin.
* 'serialize' Serialize the setting under this key instead of storing separately.
*
* Note: The order of registration controls the order of display.
*/
public static function register_settings() {
// * * * * * * * * * * * * * * * * * * * * * * *
// AUTOMAGIC META BOX
// * * * * * * * * * * * * * * * * * * * * * * *
MetaBox::register_setting( '_nv_example_meta_field', 'magic-meta-box', array(
'label' => __( 'Example Field', 'nvLangScope' ),
'placeholder' => __( 'Enter a value…', 'nvLangScope' ),
) );
MetaBox::register_setting( '_nv_example_meta_checkboxes', 'magic-meta-box', array(
'type' => 'checkbox',
'label' => __( 'Example Checklist', 'nvLangScope' ),
'list' => array(
'cb1' => __( 'Item 1', 'nvLangScope' ),
'cb2' => __( 'Item 2', 'nvLangScope' ),
'cb3' => __( 'Item 3', 'nvLangScope' ),
),
) );
MetaBox::register_setting( '_nv_example_meta_radio', 'magic-meta-box', array(
'type' => 'radio',
'label' => __( 'Example Radio List', 'nvLangScope' ),
'list' => array(
'radio1' => __( 'Item 1', 'nvLangScope' ),
'radio2' => __( 'Item 2', 'nvLangScope' ),
'radio3' => __( 'Item 3', 'nvLangScope' ),
),
'value' => 'radio1', // which list item(s) to select by default
) );
MetaBox::register_setting( 'example_serialized_checkbox', 'magic-meta-box', array(
'type' => 'checkbox',
'label' => __( 'Example single checkbox', 'nvLangScope' ),
'serialize' => '_nv_example_serialized',
) );
MetaBox::register_setting( 'example_serialized_textarea', 'magic-meta-box', array(
'type' => 'textarea',
'label' => __( 'Example Textarea', 'nvLangScope' ),
'placeholder' => __('Your text goes here!','nvLangScope'),
'howto' => __( 'You can add a paragraph of help text below any setting.', 'nvLangScope' ),
'serialize' => '_nv_example_serialized',
) );
// * * * * * * * * * * * * * * * * * * * * * * *
// CUSTOM (HAND-CRAFTED) META BOX
// * * * * * * * * * * * * * * * * * * * * * * *
MetaBox::register_setting( '_nv_example_meta_field2', 'custom-meta-box', array(
'label' => __( 'Example Field', 'nvLangScope' ),
'placeholder' => __( 'Enter a value…', 'nvLangScope' ),
) );
MetaBox::register_setting( '_nv_example_meta_dropdown', 'custom-meta-box', array(
'type' => 'select',
'label' => __( 'Example Dropdown', 'nvLangScope' ),
'list' => array(
'' => __( '-- Select One --', 'nvLangScope' ),
'1' => __( 'Option 1', 'nvLangScope' ),
'2' => __( 'Option 2', 'nvLangScope' ),
),
) );
}
/**
* Constructor. Generally, you don't need to mess with this.
*/
public static function init() {
require 'includes/class.NV.Plugins.MetaBox.php';
require 'includes/class.NV.Plugins.MetaBox.Html.php';
require 'includes/class.NV.Plugins.MetaBox.HtmlTags.php';
// Register metabox settings / fields
self::register_settings();
// Add the meta boxes
add_action( 'add_meta_boxes', array( __CLASS__, 'register_metaboxes' ) );
// Enqueue optionals styles & js
add_action( 'admin_enqueue_scripts', function ( $hook ) {
wp_enqueue_script( 'nv_meta_scripts', plugin_dir_url( __FILE__ ) . 'assets/js/scripts.js' );
wp_enqueue_style( 'nv_meta_styles', plugin_dir_url( __FILE__ ) . 'assets/css/styles.css' );
} );
}
}