Skip to content

Instantly share code, notes, and snippets.

@richardsweeney
Last active December 16, 2015 18:09
Show Gist options
  • Save richardsweeney/5476173 to your computer and use it in GitHub Desktop.
Save richardsweeney/5476173 to your computer and use it in GitHub Desktop.
The beginning of a class to create + save meta boxes + fields.
<?php
class Olab_Meta_Box {
public $id, $title, $meta_box;
/** An array of elements to be rendered in the meta box */
public $elements = array();
/** An array of repeatable elements to be rendered in the meta box */
public $repeaters = array();
/** The prefix for all meta fields: ie omlb[my-element] */
public $prefix = 'olmb';
/** The size of image to display in the meta box on upload */
public $image_size = 'medium';
/** Set default variables */
public function __construct( $id, $title, $args = array() ) {
$defaults = array(
'callback' => array( $this, 'meta_box_callback' ),
'post_type' => 'post',
'context' => 'normal',
'priority' => 'high',
);
$this->id = $id;
$this->title = $title;
$this->meta_box = wp_parse_args( $args, $defaults );
}
/** Let's get this party started! */
public function render() {
global $pagenow;
// print_r( $pagenow );
add_action( 'add_meta_boxes', array( $this, 'add_the_meta_box' ) );
add_action( 'save_post', array( $this, 'save_post_meta' ), 10, 2 );
if ( 'post-new.php' == $pagenow || 'post.php' == $pagenow )
add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_js' ) );
}
/** Register the metabox */
public function add_the_meta_box() {
add_meta_box(
$this->id,
$this->title,
$this->meta_box['callback'],
$this->meta_box['post_type'],
$this->meta_box['context'],
$this->meta_box['priority']
);
}
/** Add an element to the metabox */
public function add_element( $type, $label, $id, $name = false, $placeholder = '' ) {
$element = new StdClass;
$element->type = $type;
$element->label = $label;
$element->name = ( false === $name ) ? $id : $name;
$element->placeholder = $placeholder;
$this->elements[ $id ] = $element;
return $this;
}
/** Add a wp_editor instance to the metabox */
public function add_wp_editor( $id, $name, $settings = array() ) {
$element = new StdClass;
$element->type = 'wp_editor';
$element->name = ( false === $name ) ? $id : $name;
$element->settings = $settings;
$this->elements[ $id ] = $element;
return $this;
}
/** Add some repeatable fields to the meta box */
public function add_repeater( $name ) {
$this->repeaters[] = $name;
return $this;
}
/** Render the metabox element */
private function _render_element( $id, $attr ) {
$value = $this->olab_get_meta( $attr );
switch ( $attr->type ) {
case 'email' :
case 'text' :
case 'tel' :
case 'password' :
case 'url' :
$element = "<label for='$id'>{$attr->label}</label>\n";
$element .= "<input type='{$attr->type}' value='$value' id='$id' name='" . $this->prefix . "[" . $attr->name . "]' placeholder='$attr->placeholder'>\n";
break;
case 'textarea' :
$element = "<label for='$id'>{$attr->label}</label>\n";
$element .= "<textarea id='$id' name='" . $this->prefix . "[" . $attr->name . "]'>$value</textarea>\n";
break;
case 'wp_editor' :
$attr->settings['textarea_name'] = $this->prefix . '[' . $attr->name . ']';
// Note that the ID that is passed to the wp_editor() function can only be composed of lower-case letters. No underscores, no hyphens. Anything else will cause the WYSIWYG editor to malfunction. See http://codex.wordpress.org/Function_Reference/wp_editor
ob_start();
$element = wp_editor( $value, $id, $attr->settings );
ob_end_flush();
break;
case 'uploader' :
$element = "<a href='#' data-id='$id' class='button olab-uploader-trigger'>" . __( 'Upload', 'mim' ) . '</a>';
$element .= "<input type='hidden' id='$id' name='" . $this->prefix . "[" . $attr->name . "]'>";
if ( '' != $value ) {
$img = wp_get_attachment_image_src( $value, $this->image_size );
$element .= "<img src='{$img[0]}'>";
}
break;
}
return $element;
}
/** Render metabox */
public function meta_box_callback() {
wp_nonce_field( basename( __FILE__ ), 'olab_meta_box_nonce' );
foreach ( $this->elements as $id => $attr ) { ?>
<div class="element-container">
<?php echo $this->_render_element( $id, $attr ); ?>
</div>
<?php }
}
/** Save the meta */
public function save_post_meta( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
if ( is_int( wp_is_post_autosave( $post_id ) ) )
return;
if ( ! current_user_can( 'edit_post', $post_id ) )
return $post_id;
if ( ! isset( $_POST[ $this->prefix ] ) )
return $post_id;
// die( var_dump( $_POST[ $this->prefix ] ) );
foreach ( $_POST[ $this->prefix ] as $key => $val ) {
$meta_key = '_' . $this->prefix . '_' . $key;
update_post_meta( $post_id, $meta_key, sanitize_text_field( $val ) );
}
}
/** Enqueue the media loader & my JS*/
function load_admin_js() {
wp_enqueue_media();
wp_enqueue_script( 'olab-meta-box-js', get_bloginfo( 'template_directory' ) . '/includes/olab-meta-box.js', array( 'jquery' ) );
// wp_localize_script( 'olab-admin-js', 'jsGlobals', $jsglobals );
}
/** Get individual post meta */
public function olab_get_meta( $element, $default = '' ) {
global $post;
$meta_key = '_' . $this->prefix . '_' . $element->name;
$meta = get_post_meta( $post->ID, $meta_key, true );
if ( ! $meta )
$meta = $default;
return $meta;
}
/** Get all post meta */
public function get_all_post_meta() {
$meta_array = array();
foreach( $this->elements as $element ) {
$meta_array[] = olab_get_meta( $element );
}
return $meta_array();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment