Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active March 15, 2023 01:33
Show Gist options
  • Select an option

  • Save cferdinandi/b851efcd82534311310d545b0c1b870c to your computer and use it in GitHub Desktop.

Select an option

Save cferdinandi/b851efcd82534311310d545b0c1b870c to your computer and use it in GitHub Desktop.

Revisions

  1. cferdinandi revised this gist Feb 11, 2017. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions metabox-single.php
    Original file line number Diff line number Diff line change
    @@ -14,6 +14,7 @@
    * @link https://developer.wordpress.org/reference/functions/add_meta_box/
    */
    function _namespace_create_metabox() {

    // Can only be used on a single post type (ie. page or post or a custom post type).
    // Must be repeated for each post type you want the metabox to appear on.
    add_meta_box(
    @@ -24,6 +25,10 @@ function _namespace_create_metabox() {
    'normal', // Where to put it (normal = main colum, side = sidebar, etc.)
    'default' // Priority relative to other metaboxes
    );

    // To add the metabox to a page, too, you'd repeat it, changing the location
    add_meta_box( '_namespace_metabox', 'Some Metabox', '_namespace_render_metabox', 'page', 'normal', 'default' // Priority relative to other metaboxes );

    }
    add_action( 'add_meta_boxes', '_namespace_create_metabox' );

  2. cferdinandi revised this gist Feb 11, 2017. 1 changed file with 33 additions and 33 deletions.
    66 changes: 33 additions & 33 deletions metabox-single.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    /**
    * Create a metabox with a single field.
    * Replace `sfeeney` with some namespace for your project to avoid conflicts with other items
    * Replace `_namespace` with some namespace for your project to avoid conflicts with other items
    */

    //
    @@ -13,40 +13,40 @@
    * Create the metabox
    * @link https://developer.wordpress.org/reference/functions/add_meta_box/
    */
    function sfeeney_create_metabox() {
    function _namespace_create_metabox() {
    // Can only be used on a single post type (ie. page or post or a custom post type).
    // Must be repeated for each post type you want the metabox to appear on.
    add_meta_box(
    'sfeeney_metabox', // Metabox ID
    '_namespace_metabox', // Metabox ID
    'Some Metabox', // Title to display
    'sfeeney_render_metabox', // Function to call that contains the metabox content
    '_namespace_render_metabox', // Function to call that contains the metabox content
    'post', // Post type to display metabox on
    'normal', // Where to put it (normal = main colum, side = sidebar, etc.)
    'default' // Priority relative to other metaboxes
    );
    }
    add_action( 'add_meta_boxes', 'sfeeney_create_metabox' );
    add_action( 'add_meta_boxes', '_namespace_create_metabox' );



    /**
    * Render the metabox markup
    * This is the function called in `sfeeney_create_metabox()`
    * This is the function called in `_namespace_create_metabox()`
    */
    function sfeeney_render_metabox() {
    function _namespace_render_metabox() {

    // Variables
    global $post; // Get the current post data
    $details = get_post_meta( $post->ID, 'sfeeney', true ); // Get the saved values
    $details = get_post_meta( $post->ID, '_namespace', true ); // Get the saved values

    ?>

    <fieldset>
    <div>
    <label for="sfeeney_custom_metabox">
    <label for="_namespace_custom_metabox">
    <?php
    // This runs the text through a translation and echoes it (for internationalization)
    _e( 'Item Name', 'sfeeney' );
    _e( 'Item Name', '_namespace' );
    ?>
    </label>
    <?php
    @@ -55,8 +55,8 @@ function sfeeney_render_metabox() {
    ?>
    <input
    type="text"
    name="sfeeney_custom_metabox"
    id="sfeeney_custom_metabox"
    name="_namespace_custom_metabox"
    id="_namespace_custom_metabox"
    value="<?php echo esc_attr( $details ); ?>"
    >
    </div>
    @@ -68,7 +68,7 @@ function sfeeney_render_metabox() {
    // This validates that submission came from the
    // actual dashboard and not the front end or
    // a remote server.
    wp_nonce_field( 'sfeeney_form_metabox_nonce', 'sfeeney_form_metabox_process' );
    wp_nonce_field( '_namespace_form_metabox_nonce', '_namespace_form_metabox_process' );

    }

    @@ -83,13 +83,13 @@ function sfeeney_render_metabox() {
    * @param Number $post_id The post ID
    * @param Array $post The post data
    */
    function sfeeney_save_metabox( $post_id, $post ) {
    function _namespace_save_metabox( $post_id, $post ) {

    // Verify that our security field exists. If not, bail.
    if ( !isset( $_POST['sfeeney_form_metabox_process'] ) ) return;
    if ( !isset( $_POST['_namespace_form_metabox_process'] ) ) return;

    // Verify data came from edit/dashboard screen
    if ( !wp_verify_nonce( $_POST['sfeeney_form_metabox_process'], 'sfeeney_form_metabox_nonce' ) ) {
    if ( !wp_verify_nonce( $_POST['_namespace_form_metabox_process'], '_namespace_form_metabox_nonce' ) ) {
    return $post->ID;
    }

    @@ -101,7 +101,7 @@ function sfeeney_save_metabox( $post_id, $post ) {
    // Check that our custom fields are being passed along
    // This is the `name` value array. We can grab all
    // of the fields and their values at once.
    if ( !isset( $_POST['sfeeney_custom_metabox'] ) ) {
    if ( !isset( $_POST['_namespace_custom_metabox'] ) ) {
    return $post->ID;
    }

    @@ -111,13 +111,13 @@ function sfeeney_save_metabox( $post_id, $post ) {
    * `wp_filter_post_kses` strips our dangerous server values
    * and allows through anything you can include a post.
    */
    $sanitized = wp_filter_post_kses( $_POST['sfeeney_custom_metabox'] );
    $sanitized = wp_filter_post_kses( $_POST['_namespace_custom_metabox'] );

    // Save our submissions to the database
    update_post_meta( $post->ID, 'sfeeney', $sanitized );
    update_post_meta( $post->ID, '_namespace', $sanitized );

    }
    add_action( 'save_post', 'sfeeney_save_metabox', 1, 2 );
    add_action( 'save_post', '_namespace_save_metabox', 1, 2 );



    @@ -131,7 +131,7 @@ function sfeeney_save_metabox( $post_id, $post ) {
    * Save events data to revisions
    * @param Number $post_id The post ID
    */
    function sfeeney_save_revisions( $post_id ) {
    function _namespace_save_revisions( $post_id ) {

    // Check if it's a revision
    $parent_id = wp_is_post_revision( $post_id );
    @@ -141,17 +141,17 @@ function sfeeney_save_revisions( $post_id ) {

    // Get the saved data
    $parent = get_post( $parent_id );
    $details = get_post_meta( $parent->ID, 'sfeeney', true );
    $details = get_post_meta( $parent->ID, '_namespace', true );

    // If data exists and is an array, add to revision
    if ( !empty( $details ) ) {
    add_metadata( 'post', $post_id, 'sfeeney', $details );
    add_metadata( 'post', $post_id, '_namespace', $details );
    }

    }

    }
    add_action( 'save_post', 'sfeeney_save_revisions' );
    add_action( 'save_post', '_namespace_save_revisions' );



    @@ -160,18 +160,18 @@ function sfeeney_save_revisions( $post_id ) {
    * @param Number $post_id The post ID
    * @param Number $revision_id The revision ID
    */
    function sfeeney_restore_revisions( $post_id, $revision_id ) {
    function _namespace_restore_revisions( $post_id, $revision_id ) {

    // Variables
    $post = get_post( $post_id ); // The post
    $revision = get_post( $revision_id ); // The revision
    $details = get_metadata( 'post', $revision->ID, 'sfeeney', true ); // The historic version
    $details = get_metadata( 'post', $revision->ID, '_namespace', true ); // The historic version

    // Replace our saved data with the old version
    update_post_meta( $post_id, 'sfeeney', $details );
    update_post_meta( $post_id, '_namespace', $details );

    }
    add_action( 'wp_restore_post_revision', 'sfeeney_restore_revisions', 10, 2 );
    add_action( 'wp_restore_post_revision', '_namespace_restore_revisions', 10, 2 );



    @@ -180,12 +180,12 @@ function sfeeney_restore_revisions( $post_id, $revision_id ) {
    * @param Array $fields The fields
    * @return Array The fields
    */
    function sfeeney_get_revisions_fields( $fields ) {
    function _namespace_get_revisions_fields( $fields ) {
    // Set a title
    $fields['sfeeney'] = 'Some Item';
    $fields['_namespace'] = 'Some Item';
    return $fields;
    }
    add_filter( '_wp_post_revision_fields', 'sfeeney_get_revisions_fields' );
    add_filter( '_wp_post_revision_fields', '_namespace_get_revisions_fields' );



    @@ -194,8 +194,8 @@ function sfeeney_get_revisions_fields( $fields ) {
    * @param String|Array $value The field value
    * @param Array $field The field
    */
    function sfeeney_display_revisions_fields( $value, $field ) {
    function _namespace_display_revisions_fields( $value, $field ) {
    global $revision;
    return get_metadata( 'post', $revision->ID, $field, true );
    }
    add_filter( '_wp_post_revision_field_my_meta', 'sfeeney_display_revisions_fields', 10, 2 );
    add_filter( '_wp_post_revision_field_my_meta', '_namespace_display_revisions_fields', 10, 2 );
  3. cferdinandi created this gist Feb 11, 2017.
    201 changes: 201 additions & 0 deletions metabox-single.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,201 @@
    <?php

    /**
    * Create a metabox with a single field.
    * Replace `sfeeney` with some namespace for your project to avoid conflicts with other items
    */

    //
    // Create Metabox
    //

    /**
    * Create the metabox
    * @link https://developer.wordpress.org/reference/functions/add_meta_box/
    */
    function sfeeney_create_metabox() {
    // Can only be used on a single post type (ie. page or post or a custom post type).
    // Must be repeated for each post type you want the metabox to appear on.
    add_meta_box(
    'sfeeney_metabox', // Metabox ID
    'Some Metabox', // Title to display
    'sfeeney_render_metabox', // Function to call that contains the metabox content
    'post', // Post type to display metabox on
    'normal', // Where to put it (normal = main colum, side = sidebar, etc.)
    'default' // Priority relative to other metaboxes
    );
    }
    add_action( 'add_meta_boxes', 'sfeeney_create_metabox' );



    /**
    * Render the metabox markup
    * This is the function called in `sfeeney_create_metabox()`
    */
    function sfeeney_render_metabox() {

    // Variables
    global $post; // Get the current post data
    $details = get_post_meta( $post->ID, 'sfeeney', true ); // Get the saved values

    ?>

    <fieldset>
    <div>
    <label for="sfeeney_custom_metabox">
    <?php
    // This runs the text through a translation and echoes it (for internationalization)
    _e( 'Item Name', 'sfeeney' );
    ?>
    </label>
    <?php
    // The `esc_attr()` function here escapes the data for
    // HTML attribute use to avoid unexpected issues
    ?>
    <input
    type="text"
    name="sfeeney_custom_metabox"
    id="sfeeney_custom_metabox"
    value="<?php echo esc_attr( $details ); ?>"
    >
    </div>
    </fieldset>

    <?php

    // Security field
    // This validates that submission came from the
    // actual dashboard and not the front end or
    // a remote server.
    wp_nonce_field( 'sfeeney_form_metabox_nonce', 'sfeeney_form_metabox_process' );

    }



    //
    // Save our data
    //

    /**
    * Save the metabox
    * @param Number $post_id The post ID
    * @param Array $post The post data
    */
    function sfeeney_save_metabox( $post_id, $post ) {

    // Verify that our security field exists. If not, bail.
    if ( !isset( $_POST['sfeeney_form_metabox_process'] ) ) return;

    // Verify data came from edit/dashboard screen
    if ( !wp_verify_nonce( $_POST['sfeeney_form_metabox_process'], 'sfeeney_form_metabox_nonce' ) ) {
    return $post->ID;
    }

    // Verify user has permission to edit post
    if ( !current_user_can( 'edit_post', $post->ID )) {
    return $post->ID;
    }

    // Check that our custom fields are being passed along
    // This is the `name` value array. We can grab all
    // of the fields and their values at once.
    if ( !isset( $_POST['sfeeney_custom_metabox'] ) ) {
    return $post->ID;
    }

    /**
    * Sanitize the submitted data
    * This keeps malicious code out of our database.
    * `wp_filter_post_kses` strips our dangerous server values
    * and allows through anything you can include a post.
    */
    $sanitized = wp_filter_post_kses( $_POST['sfeeney_custom_metabox'] );

    // Save our submissions to the database
    update_post_meta( $post->ID, 'sfeeney', $sanitized );

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



    //
    // Save a copy to our revision history
    // This is optional, and potentially undesireable for certain data types.
    // Restoring a a post to an old version will also update the metabox.
    //

    /**
    * Save events data to revisions
    * @param Number $post_id The post ID
    */
    function sfeeney_save_revisions( $post_id ) {

    // Check if it's a revision
    $parent_id = wp_is_post_revision( $post_id );

    // If is revision
    if ( $parent_id ) {

    // Get the saved data
    $parent = get_post( $parent_id );
    $details = get_post_meta( $parent->ID, 'sfeeney', true );

    // If data exists and is an array, add to revision
    if ( !empty( $details ) ) {
    add_metadata( 'post', $post_id, 'sfeeney', $details );
    }

    }

    }
    add_action( 'save_post', 'sfeeney_save_revisions' );



    /**
    * Restore events data with post revisions
    * @param Number $post_id The post ID
    * @param Number $revision_id The revision ID
    */
    function sfeeney_restore_revisions( $post_id, $revision_id ) {

    // Variables
    $post = get_post( $post_id ); // The post
    $revision = get_post( $revision_id ); // The revision
    $details = get_metadata( 'post', $revision->ID, 'sfeeney', true ); // The historic version

    // Replace our saved data with the old version
    update_post_meta( $post_id, 'sfeeney', $details );

    }
    add_action( 'wp_restore_post_revision', 'sfeeney_restore_revisions', 10, 2 );



    /**
    * Get the data to display on the revisions page
    * @param Array $fields The fields
    * @return Array The fields
    */
    function sfeeney_get_revisions_fields( $fields ) {
    // Set a title
    $fields['sfeeney'] = 'Some Item';
    return $fields;
    }
    add_filter( '_wp_post_revision_fields', 'sfeeney_get_revisions_fields' );



    /**
    * Display the data on the revisions page
    * @param String|Array $value The field value
    * @param Array $field The field
    */
    function sfeeney_display_revisions_fields( $value, $field ) {
    global $revision;
    return get_metadata( 'post', $revision->ID, $field, true );
    }
    add_filter( '_wp_post_revision_field_my_meta', 'sfeeney_display_revisions_fields', 10, 2 );