defaults = [ 'id' => 'mx-extra-metabox-1', 'post_types' => 'page', // ['page', 'post'] 'name' => esc_html( 'Extra metabox 1', 'affiliate-links-woocommerce' ), 'metabox_type' => 'input-text', 'options' => [] ]; $this->args = wp_parse_args( $args, $this->defaults ); if (is_array($this->args['post_types'])) { $this->args['metabox_id'] = $this->args['id'] . '_' . implode( '_', $this->args['post_types'] ); } else { $this->args['metabox_id'] = $this->args['id'] . '_' . $this->args['post_types']; } $this->args['post_meta_key'] = '_mx_' . $this->args['metabox_id'] . '_id'; $this->args['nonce_action'] = $this->args['metabox_id'] . '_nonce_action'; $this->args['nonce_name'] = $this->args['metabox_id'] . '_nonce_name'; // add options area if ($this->args['metabox_type'] == 'checkbox') { $i = 0; foreach ($this->args['options'] as $key => $value) { $this->args['options'][$key]['name'] = $this->args['post_meta_key'] . $i; $i++; } } add_action( 'add_meta_boxes', [$this, 'addMetaBox'] ); add_action( 'save_post', [$this, 'saveMetaBox'] ); } // add post meta public function addMetaBox() { add_meta_box( $this->args['metabox_id'], $this->args['name'], [ $this, 'metaBoxContent' ], $this->args['post_types'], 'normal' // 'low' ); } // save post meta public function saveMetaBox( $post_id ) { if (!isset($_POST[ $this->args['nonce_name'] ]) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[$this->args['nonce_name']] ) ), $this->args['nonce_action'])) { return; } if (!current_user_can('edit_post', $post_id)) { return; } $value = ''; if (isset($_POST) && isset($_POST[$this->args['post_meta_key']])) { if ($this->args['metabox_type'] == 'input-email') { // email field $value = sanitize_email( wp_unslash( $_POST[ $this->args['post_meta_key'] ] ) ); } elseif ($this->args['metabox_type'] == 'input-url') { // url field $value = esc_url_raw( wp_unslash( $_POST[ $this->args['post_meta_key'] ] ) ); } elseif ($this->args['metabox_type'] == 'textarea') { // textarea field $value = sanitize_textarea_field( wp_unslash( $_POST[ $this->args['post_meta_key'] ] ) ); } elseif ($this->args['metabox_type'] == 'image') { // image id $value = sanitize_text_field( wp_unslash( $_POST[ $this->args['post_meta_key'] ] ) ); } elseif ($this->args['metabox_type'] == 'radio') { // radio value $value = sanitize_text_field( wp_unslash( $_POST[ $this->args['post_meta_key'] ] ) ); } elseif ($this->args['metabox_type'] == 'checkbox') { $_value = null; // checkbox value foreach ($this->args['options'] as $key => $val) { if (isset($_POST[ $val['name']])) { $_value = sanitize_text_field( wp_unslash( $_POST[ $val['name'] ] ) ); } // save data update_post_meta( $post_id, $val['name'], $_value ); } // checkbox marker $value = sanitize_text_field( wp_unslash( $_POST[ $this->args['post_meta_key'] ] ) ); } else { // input text $value = sanitize_text_field( wp_unslash( $_POST[ $this->args['post_meta_key'] ] ) ); } } // save data update_post_meta( $post_id, $this->args['post_meta_key'], $value ); } // metabox content public function metaBoxContent( $post, $meta ) { $metaValue = get_post_meta( $post->ID, $this->args['post_meta_key'], true ); ?>

args['metabox_type'] == 'input-email') : ?> args['metabox_type'] == 'input-url') : ?> args['metabox_type'] == 'textarea') : ?> args['metabox_type'] == 'image') : ?>

/> >Remove Image
args['metabox_type'] == 'radio') : ?> args['options']) == 0) { echo '

You have to add some options to the "Options" array!

'; } else { if (is_array($this->args['options'])) { $i = 0; foreach ($this->args['options'] as $key => $val) { ?>
>
args['metabox_type'] == 'checkbox') : ?> args['options']) == 0) { echo '

You have to add some options to the "Options" array!

'; } else { if (is_array( $this->args['options'])) { ?>args['options'] as $key => $val) { $checkboxValue = get_post_meta( $post->ID, $val['name'], true ); ?>
>

args['nonce_action'], $this->args['nonce_name'], true, true ); } }