Skip to content

Instantly share code, notes, and snippets.

@smjuber
Forked from devinsays/post-type-metaboxes.php
Created July 24, 2020 19:37
Show Gist options
  • Save smjuber/f9b44c79e95b286b3e29f95f76c4b4f8 to your computer and use it in GitHub Desktop.
Save smjuber/f9b44c79e95b286b3e29f95f76c4b4f8 to your computer and use it in GitHub Desktop.

Revisions

  1. @devinsays devinsays revised this gist Nov 4, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions post-type-metaboxes.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    /**
    * Example code for tutorial post on custom meta boxes:
    * https://wptheming.com/2010/08/custom-metabox-for-post-type/
  2. @devinsays devinsays revised this gist Nov 4, 2017. 1 changed file with 14 additions and 16 deletions.
    30 changes: 14 additions & 16 deletions post-type-metaboxes.php
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    <?php
    /**
    * Example code for tutorial post on custom meta boxes:
    * https://wptheming.com/2010/08/custom-metabox-for-post-type/
    @@ -42,7 +41,7 @@ function wpt_event_post_type() {
    'register_meta_box_cb' => 'wpt_add_event_metaboxes',
    );

    register_post_type( 'testimonial', $args );
    register_post_type( 'events', $args );

    }
    add_action( 'init', 'wpt_event_post_type' );
    @@ -92,9 +91,8 @@ function add_events_metaboxes_v2() {
    function wpt_events_location() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '">';
    // Nonce field to validate form request came from current site
    wp_nonce_field( basename( __FILE__ ), 'event_fields' );

    // Get the location data if it's already been entered
    $location = get_post_meta( $post->ID, 'location', true );
    @@ -109,15 +107,15 @@ function wpt_events_location() {
    */
    function wpt_save_events_meta( $post_id, $post ) {

    // Verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times.
    if ( ! wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) ) ) {
    return $post->ID;
    // Return if the user doesn't have edit permissions.
    if ( ! current_user_can( 'edit_post', $post_id ) ) {
    return $post_id;
    }

    // Return if the user doesn't have edit permissions.
    if ( ! current_user_can( 'edit_post', $post->ID ) ) {
    return $post->ID;
    // Verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times.
    if ( ! isset( $_POST['location'] ) || ! wp_verify_nonce( $_POST['event_fields'], basename(__FILE__) ) ) {
    return $post_id;
    }

    // Now that we're authenticated, time to save the data.
    @@ -133,17 +131,17 @@ function wpt_save_events_meta( $post_id, $post ) {
    return;
    }

    if ( get_post_meta( $post->ID, $key, false ) ) {
    if ( get_post_meta( $post_id, $key, false ) ) {
    // If the custom field already has a value, update it.
    update_post_meta( $post->ID, $key, $value );
    update_post_meta( $post_id, $key, $value );
    } else {
    // If the custom field doesn't have a value, add it.
    add_post_meta($post->ID, $key, $value);
    add_post_meta( $post_id, $key, $value);
    }

    if ( ! $value ) {
    // Delete the meta key if there's no value
    delete_post_meta( $post->ID, $key );
    delete_post_meta( $post_id, $key );
    }

    endforeach;
  3. @devinsays devinsays created this gist Nov 4, 2017.
    152 changes: 152 additions & 0 deletions post-type-metaboxes.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,152 @@
    <?php
    /**
    * Example code for tutorial post on custom meta boxes:
    * https://wptheming.com/2010/08/custom-metabox-for-post-type/
    */

    /**
    * Registers the event post type.
    */
    function wpt_event_post_type() {

    $labels = array(
    'name' => __( 'Events' ),
    'singular_name' => __( 'Event' ),
    'add_new' => __( 'Add New Event' ),
    'add_new_item' => __( 'Add New Event' ),
    'edit_item' => __( 'Edit Event' ),
    'new_item' => __( 'Add New Event' ),
    'view_item' => __( 'View Event' ),
    'search_items' => __( 'Search Event' ),
    'not_found' => __( 'No events found' ),
    'not_found_in_trash' => __( 'No events found in trash' )
    );

    $supports = array(
    'title',
    'editor',
    'thumbnail',
    'comments',
    'revisions',
    );

    $args = array(
    'labels' => $labels,
    'supports' => $supports,
    'public' => true,
    'capability_type' => 'post',
    'rewrite' => array( 'slug' => 'events' ),
    'has_archive' => true,
    'menu_position' => 30,
    'menu_icon' => 'dashicons-calendar-alt',
    'register_meta_box_cb' => 'wpt_add_event_metaboxes',
    );

    register_post_type( 'testimonial', $args );

    }
    add_action( 'init', 'wpt_event_post_type' );

    /**
    * Adds a metabox to the right side of the screen under the “Publish” box
    */
    function wpt_add_event_metaboxes() {
    add_meta_box(
    'wpt_events_location',
    'Event Location',
    'wpt_events_location',
    'events',
    'side',
    'default'
    );
    }

    /**
    * If you wanted to have two sets of metaboxes.
    */
    function add_events_metaboxes_v2() {

    add_meta_box(
    'wpt_events_date',
    'Event Date',
    'wpt_events_date',
    'events',
    'side',
    'default'
    );

    add_meta_box(
    'wpt_events_location',
    'Event Location',
    'wpt_events_location',
    'events',
    'normal',
    'high'
    );

    }

    /**
    * Output the HTML for the metabox.
    */
    function wpt_events_location() {
    global $post;

    // Noncename needed to verify where the data originated
    echo '<input type="hidden" name="eventmeta_noncename" id="eventmeta_noncename" value="' .
    wp_create_nonce( plugin_basename(__FILE__) ) . '">';

    // Get the location data if it's already been entered
    $location = get_post_meta( $post->ID, 'location', true );

    // Output the field
    echo '<input type="text" name="location" value="' . esc_textarea( $location ) . '" class="widefat">';

    }

    /**
    * Save the metabox data
    */
    function wpt_save_events_meta( $post_id, $post ) {

    // Verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times.
    if ( ! wp_verify_nonce( $_POST['eventmeta_noncename'], plugin_basename(__FILE__) ) ) {
    return $post->ID;
    }

    // Return if the user doesn't have edit permissions.
    if ( ! current_user_can( 'edit_post', $post->ID ) ) {
    return $post->ID;
    }

    // Now that we're authenticated, time to save the data.
    // This sanitizes the data from the field and saves it into an array $events_meta.
    $events_meta['location'] = esc_textarea( $_POST['location'] );

    // Cycle through the $events_meta array.
    // Note, in this example we just have one item, but this is helpful if you have multiple.
    foreach ( $events_meta as $key => $value ) :

    // Don't store custom data twice
    if ( 'revision' === $post->post_type ) {
    return;
    }

    if ( get_post_meta( $post->ID, $key, false ) ) {
    // If the custom field already has a value, update it.
    update_post_meta( $post->ID, $key, $value );
    } else {
    // If the custom field doesn't have a value, add it.
    add_post_meta($post->ID, $key, $value);
    }

    if ( ! $value ) {
    // Delete the meta key if there's no value
    delete_post_meta( $post->ID, $key );
    }

    endforeach;

    }
    add_action( 'save_post', 'wpt_save_events_meta', 1, 2 );