Skip to content

Instantly share code, notes, and snippets.

@ashleyfae
Created April 3, 2016 14:43
Show Gist options
  • Save ashleyfae/0536a269bdf90463ecd6f025f2ec2546 to your computer and use it in GitHub Desktop.
Save ashleyfae/0536a269bdf90463ecd6f025f2ec2546 to your computer and use it in GitHub Desktop.
Adds a new field in the Novelist plugin for Cover Artist.
<?php
/*
* Plugin Name: Novelist Cover Artist
* Plugin URI: https://novelistplugin.com
* Description: Adds a new field in the Novelist plugin for Cover Artist.
* Version: 1.0
* Author: Nose Graze
* Author URI: https://www.nosegraze.com
* License: GPL2
*
* @package novelist-cover-artist
* @copyright Copyright (c) 2016, Nose Graze Ltd.
* @license GPL2+
*/
/**
* Add the "Cover Artist" field to the available book layout options.
*
* @param array $fields
*
* @return array
*/
function novelist_cover_artist_field( $fields ) {
$fields['cover_artist'] = array(
'name' => __( 'Cover Artist', 'novelist-cover-artist' ),
'placeholder' => '[cover_artist]',
'label' => sprintf( __( '<strong>Cover Artist:</strong> %s', 'novelist-cover-artist' ), '[cover_artist]' ),
'linebreak' => 'on'
);
return $fields;
}
add_filter( 'novelist/book/available-fields', 'novelist_cover_artist_field' );
/**
* Render the admin area text box
*
* @param Novelist_Book $book Book object.
* @param WP_Post $post Current post object.
* @param array $settings Array of settings for this field. Includes the 'label' and 'linebreak'.
*
* @return void
*/
function novelist_cover_artist_text_box( $book, $post, $settings ) {
?>
<div class="novelist-box-row">
<label for="novelist_cover_artist"><?php _e( 'Cover Artist', 'novelist' ); ?></label>
<div class="novelist-input-wrapper">
<input type="text" id="novelist_cover_artist" name="novelist_cover_artist" value="<?php echo esc_attr( $book->get_value( 'novelist_cover_artist' ) ); ?>">
</div>
</div>
<?php
}
add_action( 'novelist/meta-box/display-field-cover_artist', 'novelist_cover_artist_text_box', 10, 3 );
/**
* Add 'novelist_cover_artist' as a field to be saved.
*
* @param array $fields Array of name attributes to be located in $_POST and saved.
*
* @return array
*/
function novelist_add_cover_image_to_save( $fields ) {
$fields[] = 'novelist_cover_artist';
return $fields;
}
add_filter( 'novelist/book/meta-box/saved-fields', 'novelist_add_cover_image_to_save' );
/**
* Sanitize our field with 'sanitize_text_field'.
*/
add_filter( 'novelist/book/meta-box/sanitize/novelist_cover_artist', 'sanitize_text_field' );
/**
* Render Cover Artist
*
* @param string $value Current value for this field
* @param string $key The key that is being filtered
* @param array $all_fields All available book fields
* @param array $enabled_fields Array of the enabled book fields
* @param Novelist_Book $book Object for the current book
*
* return string
*/
function novelist_render_cover_artist( $value, $key, $all_fields, $enabled_fields, $book ) {
$value = $book->get_value( 'novelist_cover_artist' );
return $value;
}
add_filter( 'novelist/book/pre-render/cover_artist', 'novelist_render_cover_artist', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment