• I used to use Piklist to create custom meta boxes because I’m not a super developer, but now the plugin is no longer available, so I have to find another solution. Currently, I only need to radio meta box with different options that will plug into the previous plugin custom field values to keep using the feature.
    I found online meta box generator, so I recreated the radio function I wanted and it seems work, but I was wondering if anyone can take a look at the code to see if it’s not missing anything:

    class FormatsMetaBox{
    
    	private $screen = array(
    		'post',
            'my_article_format',        
    	);
    
    	private $meta_fields = array(
                    array(
                        'label' => 'Post type picker',
                        'id' => 'my_article_format',
                        'default' => 'format-1',
                        'type' => 'radio',
                        'options' => array(
                            'format-1' => 'Standard',
                            'format-2' => 'Background',
                            'format-3' => 'Video One',
                            'format-4' => 'Video Two',
                            'format-5' => 'Review',
                            'format-6' => 'Longform',
                            'format-7' => 'Gallery',
                            'format-8' => 'Minimal',
                            'format-9' => 'Inline',
                            'format-10' => 'Affiliate',
                            'format-11' => 'Linear',
                            'format-99' => 'Legacy'
                        )
                    )
    
    	);
    
    	public function __construct() {
    		add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
    		add_action( 'save_post', array( $this, 'save_fields' ) );
    	}
    
    	public function add_meta_boxes() {
    		foreach ( $this->screen as $single_screen ) {
    			add_meta_box(
    				'Formats',
    				__( 'Formats', '' ),
    				array( $this, 'meta_box_callback' ),
    				$single_screen,
    				'side',
    				'low'
    			);
    		}
    	}
    
    	public function meta_box_callback( $post ) {
    		wp_nonce_field( 'Formats_data', 'Formats_nonce' );
    		$this->field_generator( $post );
    	}
    	public function field_generator( $post ) {
    		$output = '';
    		foreach ( $this->meta_fields as $meta_field ) {
    			$label = '<label for="' . $meta_field['id'] . '">' . $meta_field['label'] . '</label>';
    			$meta_value = get_post_meta( $post->ID, $meta_field['id'], true );
    			if ( empty( $meta_value ) ) {
    				if ( isset( $meta_field['default'] ) ) {
    					$meta_value = $meta_field['default'];
    				}
    			}
    			switch ( $meta_field['type'] ) {
                            case 'radio':
                                $input = '<fieldset>';
                                $input .= '<legend class="screen-reader-text">' . $meta_field['label'] . '</legend>';
                                $i = 0;
                                foreach ( $meta_field['options'] as $key => $value ) {
                                    $meta_field_value = !is_numeric( $key ) ? $key : $value;
                                    $input .= sprintf(
                                        '<label><input %s id=" %s" name="%s" type="radio" value="%s"> %s</label>%s',
                                        $meta_value === $meta_field_value ? 'checked' : '',
                                        $meta_field['id'],
                                        $meta_field['id'],
                                        $meta_field_value,
                                        $value,
                                        $i < count( $meta_field['options'] ) - 1 ? '<br>' : ''
                                    );
                                    $i++;
                                }
                                $input .= '</fieldset>';
                                break;
    
    				default:
                                        $input = sprintf(
                                            '<input %s id="%s" name="%s" type="%s" value="%s">',
                                            $meta_field['type'] !== 'color' ? 'style="width: 100%"' : '',
                                            $meta_field['id'],
                                            $meta_field['id'],
                                            $meta_field['type'],
                                            $meta_value
                                        );
    			}
    			$output .= $this->format_rows( $label, $input );
    		}
    		echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
    	}
    
    	public function format_rows( $label, $input ) {
    		return '<tr><th style="display: inline-block">'.$label.'</th><td style="display: inline">'.$input.'</td></tr>';
    	}
    
    	public function save_fields( $post_id ) {
    		if ( ! isset( $_POST['Formats_nonce'] ) )
    			return $post_id;
    		$nonce = $_POST['Formats_nonce'];
    		if ( !wp_verify_nonce( $nonce, 'Formats_data' ) )
    			return $post_id;
    		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    			return $post_id;
    		foreach ( $this->meta_fields as $meta_field ) {
    			if ( isset( $_POST[ $meta_field['id'] ] ) ) {
    				switch ( $meta_field['type'] ) {
    					case 'email':
    						$_POST[ $meta_field['id'] ] = sanitize_email( $_POST[ $meta_field['id'] ] );
    						break;
    					case 'text':
    						$_POST[ $meta_field['id'] ] = sanitize_text_field( $_POST[ $meta_field['id'] ] );
    						break;
    				}
    				update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );
    			} else if ( $meta_field['type'] === 'checkbox' ) {
    				update_post_meta( $post_id, $meta_field['id'], '0' );
    			}
    		}
    	}
    }
    
    if (class_exists('FormatsMetabox')) {
    	new FormatsMetabox;
    };
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You should be escaping any values from the DB before echoing out. For example, the current email and text field values in the meta box fields.

    On a quick read through, every thing else looks good. Nicely done! I see you verified a nonce and sanitized input, which were my other main concerns to check for. It’s customary to also verify user capability before placing anything in the DB, but I’m pretty sure one cannot get to the save_post action unless the user has the right capability. It wouldn’t hurt to check again, but I don’t think it’s really necessary in this situation.

    Thread Starter wpmhweb

    (@wppit)

    @bcworkz thank you for the second look at the code, much appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating radio meta box, need second look to make sure code is OK’ is closed to new replies.