First thing that pops in the eye is the unindented code, most [IDE][1]sIDE s do this automatically for us. That's fundamental for everyone's sanity, yours and whomever reads your code.
And you are missing two things from [add_meta_box()][2]add_meta_box() parameters that will make the code reusable:
$metaboxesis the reusable source, just add another item and a new metabox is created.save_postreceives 2 arguments,$post_idand$post_object.- I prefer to put all hook declarations at the beginning, as it gives a general panorama of all functions below.
- Use of [
printf()][3]printf()to build complex HTML blocks.
Suggestion:
Instead of one Meta Box for each piece of information, I think it's better UX to have a single box for your plugin/feature and add the form fields there.
[1]: https://stackoverflow.com/tags/ide/info
[2]: https://codex.wordpress.org/Function_Reference/add_meta_box
[3]: http://php.net/manual/en/function.printf.php
First thing that pops in the eye is the unindented code, most [IDE][1]s do this automatically for us. That's fundamental for everyone's sanity, yours and whomever reads your code.
And you are missing two things from [add_meta_box()][2] parameters that will make the code reusable:
$metaboxesis the reusable source, just add another item and a new metabox is created.save_postreceives 2 arguments,$post_idand$post_object.- I prefer to put all hook declarations at the beginning, as it gives a general panorama of all functions below.
- Use of [
printf()][3] to build complex HTML blocks.
Suggestion:
Instead of one Meta Box for each piece of information, I think it's better UX to have a single box for your plugin/feature and add the form fields there.
[1]: https://stackoverflow.com/tags/ide/info
[2]: https://codex.wordpress.org/Function_Reference/add_meta_box
[3]: http://php.net/manual/en/function.printf.php
First thing that pops in the eye is the unindented code, most IDE s do this automatically for us. That's fundamental for everyone's sanity, yours and whomever reads your code.
And you are missing two things from add_meta_box() parameters that will make the code reusable:
$metaboxesis the reusable source, just add another item and a new metabox is created.save_postreceives 2 arguments,$post_idand$post_object.- I prefer to put all hook declarations at the beginning, as it gives a general panorama of all functions below.
- Use of
printf()to build complex HTML blocks.
Suggestion:
Instead of one Meta Box for each piece of information, I think it's better UX to have a single box for your plugin/feature and add the form fields there.
Suggestion:
Instead of one Meta Box for each piece of information, I think it's better UX to have a single box for your plugin/feature and add the form fields there.
[1]: http://stackoverflow.com/tags/ide/info https://stackoverflow.com/tags/ide/info
[2]: https://codex.wordpress.org/Function_Reference/add_meta_box
[3]: http://php.net/manual/en/function.printf.php
Suggestion:
Instead of one Meta Box for each piece of information, I think it's better UX to have a single box for your plugin/feature and add the form fields there.
[1]: http://stackoverflow.com/tags/ide/info
[2]: https://codex.wordpress.org/Function_Reference/add_meta_box
[3]: http://php.net/manual/en/function.printf.php
Suggestion:
Instead of one Meta Box for each piece of information, I think it's better UX to have a single box for your plugin/feature and add the form fields there.
[1]: https://stackoverflow.com/tags/ide/info
[2]: https://codex.wordpress.org/Function_Reference/add_meta_box
[3]: http://php.net/manual/en/function.printf.php
First thing that pops in the eye is the unindented code, most IDE s[IDE][1]s do this automatically for us. That's fundamental for everyone's sanity, yours and whomever reads your code.
And you are missing two things from add_meta_box() [add_meta_box()][2] parameters that will make the code reusable:
$metaboxesis the reusable source, just add another item and a new metabox is created.save_postreceives 2 arguments,$post_idand$post_object.- I prefer to put all hook declarations at the beginning, as it gives a general panorama of all functions below.
- Use of
printf()[printf()][3] to build complex HTML blocks.
add_action( 'add_meta_boxes', 'wp_cat_map_add_meta_box' );
add_action( 'save_post', 'wp_cat_map_data', 10, 2 );
function wp_cat_map_add_meta_box() {
/* Our configuration vars */
$screens = array( 'post', 'page' );
$metaboxes = array(
'map_lat' => array(
'label' => 'wp_cat_map_lat',
'title' => __( 'Add Latitude', 'wp_cat_map' ),
'description' => __( 'Latitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_lat',
'post_meta' => '_wp_cat_map_lat'
),
'map_long' => array(
'label' => 'wp_cat_map_long',
'title' => __( 'Add Longitude', 'wp_cat_map' ),
'description' => __( 'Longitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_long' ,
'post_meta' => '_wp_cat_map_long'
)
);
foreach ( $metaboxes as $meta_id => $meta_cont ) {
add_meta_box(
$meta_id,
$meta_cont['title'],
'wp_cat_map_callback',
$screens,
'advanced',
'default',
$meta_cont
);
}
}
function wp_cat_map_callback( $post, $post_object ) {
/* Inspect the object */
// printf( '<pre><code>%s</code></pre>', print_r( $post_object, true ) );
wp_nonce_field( 'wp_cat_map_data', 'wp_cat_map_nonce' );
$value = get_post_meta( $post->ID, $post_object['args']['post_meta'], true );
printf(
'<label for="%s">'for="%s">%s</label>',
$post_object['args']['label'],
);
echo $post_object['args']['description'];
echo '</label> ';);
printf(
'<input type="text" id="%s" name="%s" value="%s"/>',
$post_object['args']['label'],
$post_object['args']['post_meta'],
esc_attr( $value )
);
}
function wp_cat_map_data( $post_id, $post_object ) { /* code */ }
Class Code_Review_111019 {
/* Class properties, available to all methods */
private $screens;
private $metaboxes;
public function __construct( $screens, $metaboxes ) {
/* Set properties when instantiating */
$this->screens = $screens;
$this->metaboxes = $metaboxes;
/* Start everythingour hooks at a safer point */
add_action( 'plugins_loaded', array( $this, 'start_up' ) );
}
public function start_up() {
/* Inside classes we don't need unique prefixes or names for the callbacks, better yet, they can be literal */
add_action( 'add_meta_boxes', array( $this, 'add_meta_box''add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
}
public function add_meta_boxadd_meta_boxes() {
foreach ( $this->metaboxes as $meta_id => $meta_cont ) {
add_meta_box(
$meta_id,
$meta_cont['title'],
array( $this, 'build_metabox' ),
$this->screens,
'advanced',
'default',
$meta_cont
);
}
}
public function build_metabox( $post, $post_object ) {
wp_nonce_field( 'wp_cat_map_data', 'wp_cat_map_nonce' );
$value = get_post_meta( $post->ID, $post_object['args']['post_meta'], true );
printf(
'<label for="%s">'for="%s">%s</label>',
$post_object['args']['label'],
);
echo $post_object['args']['description'];$post_object['args']['description']
echo '</label> ';);
printf(
'<input type="text" id="%s" name="%s" value="%s"/>',
$post_object['args']['label'],
$post_object['args']['post_meta'],
esc_attr( $value )
);
}
/* NO CHECKS OR SECURITY WHATSOEVER, FOR DEBUGGING PURPOSES ONLY */
public function save_post( $post_id, $post_object ) {
printf( '<pre><code>%s</code></pre>', print_rforeach( $post_object,$this->metaboxes trueas )$mbox );{
printf( '<pre><code>%s</code></pre>', print_r if( $this->metaboxes,isset( true$_POST[ $mbox['post_meta'] ] ) ); {
printf( '<pre><code>%s</code></pre>', print_r update_post_meta( $_POST$post_id, true$mbox['post_meta'], )$_POST[ $mbox['post_meta'] ] );
die(); }
}
}
}
$Code_Review_111019 = new Code_Review_111019(
/* Screens */
array( 'post', 'page', 'panorama''portfolio' ),
/* Metaboxes */
array(
'map_lat' => array(
'label' => 'wp_cat_map_lat',
'title' => __( 'Add Latitude', 'wp_cat_map' ),
'description' => __( 'Latitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_lat',
'post_meta' => '_wp_cat_map_lat'
),
'map_long' => array(
'label' => 'wp_cat_map_long',
'title' => __( 'Add Longitude', 'wp_cat_map' ),
'description' => __( 'Longitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_long' ,
'post_meta' => '_wp_cat_map_long'
),
'map_zoom' => array(
'label' => 'wp_cat_map_zoom',
'title' => __( 'Add Zoom', 'wp_cat_map' ),
'description' => __( 'Zoom', 'wp_cat_map' ),
'name' => 'wp_cat_map_zoom' ,
'post_meta' => '_wp_cat_map_zoom'
)
)
);
Suggestion:
Instead of one Meta Box for each piece of information, I think it's better UX to have a single box for your plugin/feature and add the form fields there.
[1]: http://stackoverflow.com/tags/ide/info
[2]: https://codex.wordpress.org/Function_Reference/add_meta_box
[3]: http://php.net/manual/en/function.printf.php
First thing that pops in the eye is the unindented code, most IDE s do this automatically for us. That's fundamental for everyone's sanity, yours and whomever reads your code.
And you are missing two things from add_meta_box() parameters that will make the code reusable:
$metaboxesis the reusable source, just add another item and a new metabox is created.save_postreceives 2 arguments,$post_idand$post_object.- I prefer to put all hook declarations at the beginning, as it gives a general panorama of all functions below.
- Use of
printf()to build complex HTML blocks.
add_action( 'add_meta_boxes', 'wp_cat_map_add_meta_box' );
add_action( 'save_post', 'wp_cat_map_data', 10, 2 );
function wp_cat_map_add_meta_box() {
$screens = array( 'post', 'page' );
$metaboxes = array(
'map_lat' => array(
'label' => 'wp_cat_map_lat',
'title' => __( 'Add Latitude', 'wp_cat_map' ),
'description' => __( 'Latitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_lat',
'post_meta' => '_wp_cat_map_lat'
),
'map_long' => array(
'label' => 'wp_cat_map_long',
'title' => __( 'Add Longitude', 'wp_cat_map' ),
'description' => __( 'Longitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_long' ,
'post_meta' => '_wp_cat_map_long'
)
);
foreach ( $metaboxes as $meta_id => $meta_cont ) {
add_meta_box(
$meta_id,
$meta_cont['title'],
'wp_cat_map_callback',
$screens,
'advanced',
'default',
$meta_cont
);
}
}
function wp_cat_map_callback( $post, $post_object ) {
/* Inspect the object */
// printf( '<pre><code>%s</code></pre>', print_r( $post_object, true ) );
wp_nonce_field( 'wp_cat_map_data', 'wp_cat_map_nonce' );
$value = get_post_meta( $post->ID, $post_object['args']['post_meta'], true );
printf(
'<label for="%s">',
$post_object['args']['label']
);
echo $post_object['args']['description'];
echo '</label> ';
printf(
'<input type="text" id="%s" name="%s" value="%s"/>',
$post_object['args']['label'],
$post_object['args']['post_meta'],
esc_attr( $value )
);
}
function wp_cat_map_data( $post_id, $post_object ) { /* code */ }
Class Code_Review_111019 {
private $screens;
private $metaboxes;
public function __construct( $screens, $metaboxes ) {
$this->screens = $screens;
$this->metaboxes = $metaboxes;
/* Start everything at a safer point */
add_action( 'plugins_loaded', array( $this, 'start_up' ) );
}
public function start_up() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
}
public function add_meta_box() {
foreach ( $this->metaboxes as $meta_id => $meta_cont ) {
add_meta_box(
$meta_id,
$meta_cont['title'],
array( $this, 'build_metabox' ),
$this->screens,
'advanced',
'default',
$meta_cont
);
}
}
public function build_metabox( $post, $post_object ) {
wp_nonce_field( 'wp_cat_map_data', 'wp_cat_map_nonce' );
$value = get_post_meta( $post->ID, $post_object['args']['post_meta'], true );
printf(
'<label for="%s">',
$post_object['args']['label']
);
echo $post_object['args']['description'];
echo '</label> ';
printf(
'<input type="text" id="%s" name="%s" value="%s"/>',
$post_object['args']['label'],
$post_object['args']['post_meta'],
esc_attr( $value )
);
}
/* FOR DEBUGGING PURPOSES ONLY */
public function save_post( $post_id, $post_object ) {
printf( '<pre><code>%s</code></pre>', print_r( $post_object, true ) );
printf( '<pre><code>%s</code></pre>', print_r( $this->metaboxes, true ) );
printf( '<pre><code>%s</code></pre>', print_r( $_POST, true ) );
die();
}
}
$Code_Review_111019 = new Code_Review_111019(
/* Screens */
array( 'post', 'page', 'panorama' ),
/* Metaboxes */
array(
'map_lat' => array(
'label' => 'wp_cat_map_lat',
'title' => __( 'Add Latitude', 'wp_cat_map' ),
'description' => __( 'Latitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_lat',
'post_meta' => '_wp_cat_map_lat'
),
'map_long' => array(
'label' => 'wp_cat_map_long',
'title' => __( 'Add Longitude', 'wp_cat_map' ),
'description' => __( 'Longitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_long' ,
'post_meta' => '_wp_cat_map_long'
),
'map_zoom' => array(
'label' => 'wp_cat_map_zoom',
'title' => __( 'Add Zoom', 'wp_cat_map' ),
'description' => __( 'Zoom', 'wp_cat_map' ),
'name' => 'wp_cat_map_zoom' ,
'post_meta' => '_wp_cat_map_zoom'
)
)
);
First thing that pops in the eye is the unindented code, most [IDE][1]s do this automatically for us. That's fundamental for everyone's sanity, yours and whomever reads your code.
And you are missing two things from [add_meta_box()][2] parameters that will make the code reusable:
$metaboxesis the reusable source, just add another item and a new metabox is created.save_postreceives 2 arguments,$post_idand$post_object.- I prefer to put all hook declarations at the beginning, as it gives a general panorama of all functions below.
- Use of [
printf()][3] to build complex HTML blocks.
add_action( 'add_meta_boxes', 'wp_cat_map_add_meta_box' );
add_action( 'save_post', 'wp_cat_map_data', 10, 2 );
function wp_cat_map_add_meta_box() {
/* Our configuration vars */
$screens = array( 'post', 'page' );
$metaboxes = array(
'map_lat' => array(
'label' => 'wp_cat_map_lat',
'title' => __( 'Add Latitude', 'wp_cat_map' ),
'description' => __( 'Latitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_lat',
'post_meta' => '_wp_cat_map_lat'
),
'map_long' => array(
'label' => 'wp_cat_map_long',
'title' => __( 'Add Longitude', 'wp_cat_map' ),
'description' => __( 'Longitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_long' ,
'post_meta' => '_wp_cat_map_long'
)
);
foreach ( $metaboxes as $meta_id => $meta_cont ) {
add_meta_box(
$meta_id,
$meta_cont['title'],
'wp_cat_map_callback',
$screens,
'advanced',
'default',
$meta_cont
);
}
}
function wp_cat_map_callback( $post, $post_object ) {
/* Inspect the object */
// printf( '<pre><code>%s</code></pre>', print_r( $post_object, true ) );
wp_nonce_field( 'wp_cat_map_data', 'wp_cat_map_nonce' );
$value = get_post_meta( $post->ID, $post_object['args']['post_meta'], true );
printf(
'<label for="%s">%s</label>',
$post_object['args']['label'],
$post_object['args']['description'];
);
printf(
'<input type="text" id="%s" name="%s" value="%s"/>',
$post_object['args']['label'],
$post_object['args']['post_meta'],
esc_attr( $value )
);
}
function wp_cat_map_data( $post_id, $post_object ) { /* code */ }
Class Code_Review_111019 {
/* Class properties, available to all methods */
private $screens;
private $metaboxes;
public function __construct( $screens, $metaboxes ) {
/* Set properties when instantiating */
$this->screens = $screens;
$this->metaboxes = $metaboxes;
/* Start our hooks at a safer point */
add_action( 'plugins_loaded', array( $this, 'start_up' ) );
}
public function start_up() {
/* Inside classes we don't need unique prefixes or names for the callbacks, better yet, they can be literal */
add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
}
public function add_meta_boxes() {
foreach ( $this->metaboxes as $meta_id => $meta_cont ) {
add_meta_box(
$meta_id,
$meta_cont['title'],
array( $this, 'build_metabox' ),
$this->screens,
'advanced',
'default',
$meta_cont
);
}
}
public function build_metabox( $post, $post_object ) {
wp_nonce_field( 'wp_cat_map_data', 'wp_cat_map_nonce' );
$value = get_post_meta( $post->ID, $post_object['args']['post_meta'], true );
printf(
'<label for="%s">%s</label>',
$post_object['args']['label'],
$post_object['args']['description']
);
printf(
'<input type="text" id="%s" name="%s" value="%s"/>',
$post_object['args']['label'],
$post_object['args']['post_meta'],
esc_attr( $value )
);
}
/* NO CHECKS OR SECURITY WHATSOEVER, FOR DEBUGGING PURPOSES ONLY */
public function save_post( $post_id, $post_object ) {
foreach( $this->metaboxes as $mbox ){
if( isset( $_POST[ $mbox['post_meta'] ] ) ) {
update_post_meta( $post_id, $mbox['post_meta'], $_POST[ $mbox['post_meta'] ] );
}
}
}
}
$Code_Review_111019 = new Code_Review_111019(
/* Screens */
array( 'post', 'page', 'portfolio' ),
/* Metaboxes */
array(
'map_lat' => array(
'label' => 'wp_cat_map_lat',
'title' => __( 'Add Latitude', 'wp_cat_map' ),
'description' => __( 'Latitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_lat',
'post_meta' => '_wp_cat_map_lat'
),
'map_long' => array(
'label' => 'wp_cat_map_long',
'title' => __( 'Add Longitude', 'wp_cat_map' ),
'description' => __( 'Longitude', 'wp_cat_map' ),
'name' => 'wp_cat_map_long' ,
'post_meta' => '_wp_cat_map_long'
),
'map_zoom' => array(
'label' => 'wp_cat_map_zoom',
'title' => __( 'Add Zoom', 'wp_cat_map' ),
'description' => __( 'Zoom', 'wp_cat_map' ),
'name' => 'wp_cat_map_zoom' ,
'post_meta' => '_wp_cat_map_zoom'
)
)
);
Suggestion:
Instead of one Meta Box for each piece of information, I think it's better UX to have a single box for your plugin/feature and add the form fields there.
[1]: http://stackoverflow.com/tags/ide/info
[2]: https://codex.wordpress.org/Function_Reference/add_meta_box
[3]: http://php.net/manual/en/function.printf.php