Instantly share code, notes, and snippets.
Created
September 5, 2017 15:55
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save tsquez/033a0ec87ae28230185c1d2fc2e6cbd8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Generated by the WordPress Meta Box Generator | |
*/ | |
class Rational_Meta_Box { | |
private $screens = array( | |
'post', | |
'page', | |
); | |
private $fields = array( | |
array( | |
'id' => 'radio', | |
'label' => 'Select the sidebar layout you would like for this post/page.', | |
'type' => 'radio', | |
'default' => '_default', | |
'options' => array( | |
'_default' => 'Default', | |
'right_sidebar' => 'Content | Sidebar', | |
'left_sidebar' => 'Sidebar | Content', | |
'sidebars_right' => 'Content | Sidebar | Sidebar', | |
'sidebars_left' => 'Sidebar | Sidebar | Content', | |
'both_sidebars' => 'Sidebar | Content | Sidebar', | |
'no_sidebars' => 'Full Width (No Sidebars)', | |
), | |
), | |
); | |
/** | |
* Class construct method. Adds actions to their respective WordPress hooks. | |
*/ | |
public function __construct() { | |
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); | |
add_action( 'save_post', array( $this, 'save_post' ) ); | |
} | |
/** | |
* Hooks into WordPress' add_meta_boxes function. | |
* Goes through screens (post types) and adds the meta box. | |
*/ | |
public function add_meta_boxes() { | |
foreach ( $this->screens as $screen ) { | |
add_meta_box( | |
'sidebar-layout', | |
__( 'Sidebar Layout', 'themename' ), | |
array( $this, 'add_meta_box_callback' ), | |
$screen, | |
'side', | |
'default' | |
); | |
} | |
} | |
/** | |
* Generates the HTML for the meta box | |
* | |
* @param object $post WordPress post object | |
*/ | |
public function add_meta_box_callback( $post ) { | |
wp_nonce_field( 'sidebar_layout_data', 'sidebar_layout_nonce' ); | |
$this->generate_fields( $post ); | |
} | |
/** | |
* Generates the field's HTML for the meta box. | |
*/ | |
public function generate_fields( $post ) { | |
$output = ''; | |
foreach ( $this->fields as $field ) { | |
$label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>'; | |
$db_value = get_post_meta( $post->ID, 'sidebar_layout_' . $field['id'], true ); | |
if ( empty( $db_value ) ) | |
$db_value = $field['default']; | |
switch ( $field['type'] ) { | |
case 'radio': | |
$input = '<fieldset>'; | |
$input .= '<legend class="screen-reader-text">' . $field['label'] . '</legend>'; | |
$i = 0; | |
foreach ( $field['options'] as $key => $value ) { | |
$field_value = !is_numeric( $key ) ? $key : $value; | |
$input .= sprintf( | |
'<label><input %s id="%s" name="%s" type="radio" value="%s"> %s</label>%s', | |
$db_value === $field_value ? 'checked' : '', | |
$field['id'], | |
$field['id'], | |
$field_value, | |
$value, | |
$i < count( $field['options'] ) - 1 ? '<br>' : '' | |
); | |
$i++; | |
} | |
$input .= '</fieldset>'; | |
break; | |
default: | |
$input = sprintf( | |
'<input id="%s" name="%s" type="%s" value="%s">', | |
$field['id'], | |
$field['id'], | |
$field['type'], | |
$db_value | |
); | |
} | |
$output .= '<p>' . $label . '<br>' . $input . '</p>'; | |
} | |
echo $output; | |
} | |
/** | |
* Hooks into WordPress' save_post function | |
*/ | |
public function save_post( $post_id ) { | |
if ( ! isset( $_POST['sidebar_layout_nonce'] ) ) | |
return $post_id; | |
$nonce = $_POST['sidebar_layout_nonce']; | |
if ( !wp_verify_nonce( $nonce, 'sidebar_layout_data' ) ) | |
return $post_id; | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return $post_id; | |
foreach ( $this->fields as $field ) { | |
if ( isset( $_POST[ $field['id'] ] ) ) { | |
switch ( $field['type'] ) { | |
case 'email': | |
$_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] ); | |
break; | |
case 'text': | |
$_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] ); | |
break; | |
} | |
update_post_meta( $post_id, 'sidebar_layout_' . $field['id'], $_POST[ $field['id'] ] ); | |
} else if ( $field['type'] === 'checkbox' ) { | |
update_post_meta( $post_id, 'sidebar_layout_' . $field['id'], '0' ); | |
} | |
} | |
} | |
} | |
new Rational_Meta_Box; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment