Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Staff List Add Custom Field Example - does not add to [simple-staff-list] output, just creates a metabox and saves the data.
<?php
/**
* Plugin Name: Simple Staff List - Post Nominal Field
* Description: Adds a "Post Nominal" field for Simple Staff List plugin
* Version: 1.0
* Author: Brett Shumaker
* Author URI: https://brettshumaker.com
* License: GPL2
**/
/**
* Notes:
* This will only add a custom metabox for the staff-member post type
* and save that data. It _WILL NOT_ add a way to get your new field
* into the [simple-staff-list] shortcode output.
*/
class SSLP_Post_Nominal_Field {
public function __construct() {
add_action( 'do_meta_boxes', array( $this, 'add_meta_boxes' ) );
add_action( 'save_post', array( $this, 'save_post_nominal_field' ), 10, 2 );
}
public function add_meta_boxes() {
add_meta_box(
'sslp-post-nominal-field',
'Post Nominal',
array( $this, 'meta_box_callback' ),
'staff-member',
'side'
);
}
public function meta_box_callback( $post ) {
wp_nonce_field( 'sslp_post_nominal_field', 'sslp_post_nominal_field' );
$value = get_post_meta( $post->ID, '_staff_member_postnominal', true );
echo '<label for="_staff_member_postnominal">Post Nominal</label><br />';
echo '<input name="_staff_member_postnominal" id="_staff_member_postnominal" value="' . esc_attr( $value ) . '" />';
}
public function save_post_nominal_field( $post_id ) {
// Verify the nonce field.
if ( ! isset( $_POST['sslp_post_nominal_field'] ) || ! wp_verify_nonce( $_POST['sslp_post_nominal_field'], 'sslp_post_nominal_field' ) ) {
return;
}
// Don't autosave this metadata
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Make sure the user can edit the post
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( isset( $_REQUEST['_staff_member_postnominal'] ) ) {
update_post_meta( $post_id, '_staff_member_postnominal', sanitize_text_field( $_POST['_staff_member_postnominal'] ) );
}
}
}
new SSLP_Post_Nominal_Field();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.