WordPress Meta Box Generator

I wrote a little application to generate the PHP code needed to add custom meta boxes to your WordPress theme. You input the title, what post types you want it to show up on and where and it generates the PHP code for you. You also have the option of setting the prefix and adding some descriptive text under the title. It’s located here or you can click the “WP Tools” button in the nav bar.

Screenshot of the WordPress meta box generator

This first section is for the general settings. Only the title is required as it’s used for generating some variable and function names in the code. You can add a prefix in if you’d like, in case you need to make more than one set of boxes for your functions.php file. The description might also be useful if you need to leave instructions for the box.

Screenshot of the WordPress meta box generator

The next section determines where the box shows up on the editing page. Normal, advanced and side for general location. High, core, default and low to determine it’s priority in that location. Generally, “normal” and “advanced” will put it on the left side while “side” puts it on the right side. “High” and “core” in the priority will put it pretty high up in that column and “default” or “low” will put it at the bottom.

Screenshot of the WordPress meta box generator

The last area is for your fields. Currently I’ve only added text fields and textareas to the generator but I plan to add more very soon. The label is required in this section as well as it’s also used to generate some variable and function names. You can add as many as you’d like and remove the ones you don’t need as well using the “X” button.

Update

  • Removed the field for “prefix”. This is now generated from the title, same as with the Option Page Generator.
  • Checkboxes and radio buttons have been added as well. Radio buttons have the same parameters as the select list. If you need more than one you can put one on each line. And if you need custom values you can use the colon as a separator. See below.
  • I’ve added “select” lists to the field input types. Each option gets a separate line. If you need a value different from the option label you can add do so using a colon between the value and option label. For example: option-one:Option One.
function rational_get_meta( $value ) {
	global $post;

	$field = get_post_meta( $post->ID, $value, true );
	if ( ! empty( $field ) ) {
		return is_array( $field ) ? stripslashes_deep( $field ) : stripslashes( wp_kses_decode_entities( $field ) );
	} else {
		return false;
	}
}

function rational_add_meta_box() {
	add_meta_box(
		'rational-sample-title',
		__( 'Sample Title', 'rational' ),
		'rational_sample_title_html',
		'post',
		'side',
		'core'
	);
	add_meta_box(
		'rational-sample-title',
		__( 'Sample Title', 'rational' ),
		'rational_sample_title_html',
		'page',
		'side',
		'core'
	);
}
add_action( 'add_meta_boxes', 'rational_add_meta_box' );

function rational_sample_title_html( $post) {
	wp_nonce_field( '_rational_sample_title_nonce', 'rational_sample_title_nonce' ); ?>

	<p>Here's a brief description of what the box is for.</p>

	<p>
		<label for="rational_sample_title"><?php _e( 'Artist', 'rational' ); ?></label><br>
		<input type="text" name="rational_sample_title_artist" id="rational_sample_title_artist" value="<?php echo rational_get_meta( 'rational_sample_title_artist' ); ?>">
	</p>	<p>
		<label for="rational_sample_title"><?php _e( 'Bio', 'rational' ); ?></label><br>
			<textarea name="rational_sample_title_bio" id="rational_sample_title_bio" ><?php echo rational_get_meta( 'rational_sample_title_bio' ); ?></textarea>
	
	</p><?php
}

function rational_sample_title_save( $post_id ) {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	if ( ! isset( $_POST['rational_sample_title_nonce'] ) || ! wp_verify_nonce( $_POST['rational_sample_title_nonce'], '_rational_sample_title_nonce' ) ) return;
	if ( ! current_user_can( 'edit_post' ) ) return;

	if ( isset( $_POST['rational_sample_title_artist'] ) )
		update_post_meta( $post_id, 'rational_sample_title_artist', esc_attr( $_POST['rational_sample_title_artist'] ) );
	if ( isset( $_POST['rational_sample_title_bio'] ) )
		update_post_meta( $post_id, 'rational_sample_title_bio', esc_attr( $_POST['rational_sample_title_bio'] ) );
}
add_action( 'save_post', 'rational_sample_title_save' );

/*
	Usage: rational_get_meta( 'rational_sample_title_artist' )
	Usage: rational_get_meta( 'rational_sample_title_bio' )
*/

Above you’ll see a sample output of the generator. At the bottom there’s some snippets to help you get started using your newly stored field data.

Share

Comments


Notice: ob_end_flush(): failed to send buffer of zlib output compression (0) in /home/jhixon/public_html/wp-includes/functions.php on line 3722