Skip to content

Instantly share code, notes, and snippets.

@xy0
Last active August 17, 2018 21:17
Show Gist options
  • Save xy0/34c1354625d1fdc1c2bb470d138ebcd8 to your computer and use it in GitHub Desktop.
Save xy0/34c1354625d1fdc1c2bb470d138ebcd8 to your computer and use it in GitHub Desktop.
ATMC-Custom-Metabox.php
<?php
/*
/
/ ATMC Custom Metabox /
/ /
/ A-Train Marketing /
/ /
Version 0.1.5 /
*/
class ATMC_Custom_Metabox {
protected $title = '';
protected $slug = '';
protected $post_types = array();
protected $post_id = 0;
protected $context = '';
protected $priority = '';
protected $render_style = 'default';
protected $Fields = array(
'_atmc-cm-text' => array(
'displayName' => 'Text',
'type' => 'text',
'value' => 'Default basic text'
),
'_atmc-cm-checkbox' => array(
'displayName' => 'Checkbox',
'type' => 'checkbox',
'value' => ''
),
'_atmc-cm-dropdown' => array(
'displayName' => 'Dropdown',
'type' => 'dropdown',
'value' => array(
1 => 'Item 1',
2 => 'Item 2',
3 => 'Item 3'
)
)
);
// not currently used
private $post_data = '';
private $settings = array();
function __construct( $args ) {
if( count($args) == 7 ) {
// Error: not currently supporting all 7 args
return;
} else if($args) {
$title = $args[0];
$slug = $args[1];
$post_types = $args[2];
$context = $args[3];
$priority = $args[4];
} else {
return;
}
if( $slug == '' || $context == '' || $priority == '' ) {
return;
}
if( $title == '' ) {
$this->title = ucfirst( $slug );
}
if( empty( $post_types ) ) {
$this->post_types = array('post');
}
if($context == '') {
$this->context = 'advanced';
}
if($priority == '') {
$this->priority == 'high';
}
$this->title = $title;
$this->slug = $slug;
$this->post_types = $post_types;
$this->settings_id = $this->slug;
$this->context = $context;
$this->priority = $priority;
add_action( 'add_meta_boxes', array( $this, 'register' ) );
add_action( 'save_post', array( $this, 'save_meta_settings' ) );
}
public function register( $post_type ) {
if ( in_array( $post_type, $this->post_types ) ) {
add_meta_box( $this->slug, $this->title, array( $this, 'render' ), $post_type, $this->context, $this->priority, null );
}
}
public function render( $post ) {
$this->post_id = $post->ID;
wp_nonce_field( 'metabox_' . $this->slug, 'metabox_' . $this->slug . '_nonce' );
echo '<div class="atmc_metabox_wrapper"';
$this->render_fields( $this->render_style );
echo '</div>';
}
public function save_meta_settings( $post_id ) {
if ( ! isset( $_POST['metabox_' . $this->slug . '_nonce'] ) ) {
return $post_id;
}
$nonce = $_POST['metabox_' . $this->slug . '_nonce'];
if ( ! wp_verify_nonce( $nonce, 'metabox_' . $this->slug ) ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
$this->post_id = $post_id;
$this->post_data = $_POST;
$this->save_settings();
}
public function save_settings() {
foreach( $this->Fields as $key => $field ) {
update_post_meta( $this->post_id, $key, $_POST[$key] );
}
}
public function render_fields($render_style) {
foreach( $this->Fields as $key => $field ) {
$value = get_post_meta( $this->post_id, $key, true);
wp_nonce_field( plugin_basename(__FILE__),'exl_noncename' );
switch($render_style) {
case 'default':
echo '<div>';
echo '<label for="'. $key .'">'. $field['displayName'] .'<br>';
switch( $field['type'] ) {
case 'text':
echo '<input name="'. $key .'" type="text" name="'. $key .'" value="' . esc_attr( $value ) . '"/>';
break;
case 'dropdown':
echo '<select name="'. $key .'">';
foreach( $field['value'] as $dropdown_key => $dropdown_value ) {
$selected = false;
if( $dropdown_key == $value) {
$selected = true;
}
echo '<option '. ($selected ? 'selected' : false) .' value="'. $dropdown_key .'">'. $dropdown_value .'</option>';
}
echo '</select>';
break;
case 'checkbox':
echo '<input name="'. $key .'" type="checkbox" value="true" '. ($value ? 'checked' : false) .'>';
break;
case 'image_upload':
if($value) {
$meta_image_image_src = $value;
} else {
$meta_image_image_src = false;
}
echo '<div id="meta-image-image'.$key.'" style="text-align:center;background-image: linear-gradient(45deg, #bfc1c2 25%, transparent 25%),linear-gradient(-45deg, #bfc1c2 25%, transparent 25%),linear-gradient(45deg, transparent 75%, #bfc1c2 75%),linear-gradient(-45deg, transparent 75%, #bfc1c2 75%);background-size: 20px 20px;background-position: 0 0, 0 10px, 10px -10px, -10px 0px;">';
if($meta_image_image_src){
echo '<img style="margin:auto;max-width:100%;max-height:150px;" id="meta-image-image'. $key .'" src="'.$meta_image_image_src.'">';
}
?>
</div>
<p>
<input type="text" name="<?php echo $key ?>" id="meta-image<?php echo $key ?>" value="<?php echo $value ?>" style="width:100%;"/>
<input type="button" id="meta-image-button<?php echo $key ?>" class="button" value="Choose or Upload an Image" />
</p>
<?php
break;
case 'radio':
// not yet
break;
case 'color_picker':
?>
<p>
<input name="<?php echo $key ?>" type="text" value="<?php echo $value; ?>" class="meta-color" />
</p>
<?php
break;
}
echo '</label></div>';
break;
}
}
}
public function set_fields( $fields ) {
$this->Fields = $fields;
}
}; // End ATMC Custom Metabox Class
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment