Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Last active April 17, 2019 02:19
Show Gist options
  • Save hellofromtonya/bcc589760ccf1d2173c23cba1ff1f6d1 to your computer and use it in GitHub Desktop.
Save hellofromtonya/bcc589760ccf1d2173c23cba1ff1f6d1 to your computer and use it in GitHub Desktop.
Portfolio Details Metabox example
<?php
/**
* Portfolio Details Metabox Handler
*
* @package KnowTheCode\Portfolio\Metabox
* @since 1.0.0
* @author hellofromTonya
* @link https://KnowTheCode.io
* @license GPL-2.0+
*/
namespace KnowTheCode\Portfolio\Metabox;
add_action( 'add_meta_boxes', __NAMESPACE__ . '\register_portfolio_metabox' );
/**
* Register the Portfolio Details metabox.
*
* @since 1.0.0
*
* @return void
*/
function register_portfolio_metabox() {
add_meta_box(
'portfolio-details',
__( 'Portfolio Details', 'portfolio' ),
__NAMESPACE__ . '\render_portfolio_details_metabox',
'portfolio'
);
}
/**
* Render out the Portfolio Details metabox.
*
* @since 1.0.0
*
* @param WP_Post $post
* @param array $metabox
*
* @return void
*/
function render_portfolio_details_metabox( $post, array $metabox ) {
$config = get_portfolio_details_config();
wp_nonce_field( $config['nonce']['action'], $config['nonce']['name'] );
$post_id = (int) $post->ID;
$metadata = array();
foreach ( $config['defaults'] as $meta_key => $default_value ) {
$data = get_portfolio_details_metadata( $post_id, $meta_key );
$metadata[ $meta_key ] = $data ?: $default_value;
}
// then include the view file where the HTML is
include( $config['view'] );
}
add_action( 'save_post', __NAMESPACE__ . '\save_portfolio_details_metabox', 1, 2 );
/**
* Save the portfolio details when we save a portfolio.
*
* @since 1.0.0
*
* @param integer $post_id Post ID.
* @param stdClass $post Post object.
*
* @return mixed Returns post id if permissions incorrect, null if doing autosave, ajax or future post, false if update
* or delete failed, and true on success.
*/
function save_portfolio_details_metabox( $post_id, $post ) {
$config = get_portfolio_details_config();
if ( ! isset( $_POST[ $config['setting_name'] ] ) ) {
return;
}
if ( ! is_okay_to_save_metabox( $post_id, $config['nonce']['name'], $config['nonce']['action'] ) ) {
return;
}
// merge the saved fields from the $_POST with the details.
$data = wp_parse_args( $_POST[ $config['setting_name'] ], $config['defaults'] );
foreach ( (array) $data as $meta_key => $value ) {
if ( $value ) {
update_post_meta( $post_id, $meta_key, esc_attr( $value ) );
} else {
delete_post_meta( $post_id, $meta_key );
}
}
}
/**
* Checks if the conditions are set to save this metabox.
*
* @since 1.0.0
*
* @param int $post_id Post ID
* @param string $nonce_name Name of the nonce
* @param string $nonce_action Name of the nonce action.
*
* @return bool
*/
function is_okay_to_save_metabox( $post_id, $nonce_name, $nonce_action ) {
if ( ! isset( $_POST[ $nonce_name ] ) ) {
return false;
}
if ( ! wp_verify_nonce( $_POST[ $nonce_name ], $nonce_action ) ) {
return false;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return false;
}
if ( wp_is_post_autosave( $post_id ) ) {
return false;
}
return ! wp_is_post_revision( $post_id );
}
/**
* Get the portfolio details metadata.
*
* @since 1.0.0
*
* @param int $post_id Post ID
* @param string $meta_key Meta key for the metadata to get
* @param bool $is_single
*
* @return mixed
*/
function get_portfolio_details_metadata( $post_id, $meta_key, $is_single = true ) {
static $metadata = array();
if ( ! isset( $metadata[ $post_id ] ) ) {
$metadata[ $post_id ] = array();
}
if ( ! isset( $metadata[ $post_id ][ $meta_key ] ) ) {
$metadata[ $post_id ][ $meta_key ] = get_post_meta( $post_id, $meta_key, $is_single );
}
return $metadata[ $post_id ][ $meta_key ];
}
/**
* Get the portfolio details configuration parameters.
*
* @since 1.0.0
*
* @return array
*/
function get_portfolio_details_config() {
return array(
'setting_name' => 'portfolio_details',
'view' => __DIR__ . '/views/portfolio-details.php', // change this location to where you put the view file.
'nonce' => array(
'name' => 'portfolio_details_nonce',
'action' => 'portfolio_details_save',
),
'defaults' => array(
'_portfolio_client_name' => '',
'_portfolio_project_name' => '',
'_portfolio_client_url' => '',
),
);
}
<?php
/**
* Put this file into a `views` folder
*/
?>
<p>
<label for="_portfolio_client_name">
<strong><?php _e( 'Client', 'portfolio' ); ?></strong>
</label><br/>
<input class="large-text" type="text" name="portfolio_details[_portfolio_client_name]" id="_portfolio_client_name" value="<?php echo $metadata['_portfolio_client_name'] ? esc_attr( $metadata['_portfolio_client_name'] ) : ''; ?>" />
</p>
<p>
<label for="_portfolio_project_name">
<strong><?php _e( 'Project Name', 'portfolio' ); ?></strong>
</label><br/>
<input class="large-text" type="text" name="portfolio_details[_portfolio_project_name]" id="_portfolio_project_name" value="<?php echo $metadata['_portfolio_project_name'] ? esc_attr( $metadata['_portfolio_project_name'] ) : ''; ?>" />
</p>
<p>
<label for="_portfolio_client_url">
<strong><?php _e( 'Client URL', 'portfolio' ); ?></strong>
</label><br/>
<input class="large-text" type="text" name="portfolio_details[_portfolio_client_url]" id="_portfolio_client_url" value="<?php echo $metadata['_portfolio_client_url'] ? esc_url( $metadata['_portfolio_client_url'] ) : ''; ?>" />
</p>
@hellofromtonya
Copy link
Author

The above code provides you with an example of how to create a custom metabox for a custom post type. The HTML for the metabox is broken out into a view file. The main file handles all of the processing and business logic including:

  • Registering the metabox to the post type
  • Rendering the metabox out to the browser (which is a callback from the registration)
  • Saving the metadata (which is also called custom fields) upon saving a portfolio

You can adapt this code to fit your needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment