Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
add_action( 'after_setup_theme', 'thumbnail_meta' );
function thumbnail_meta(){
Thumbnail_Meta::create( [
'html_thumbnail_meta_id' => [
'label' => __( '<strong>Featured Settings:</strong>' ),
'input' => 'html'
],
'checkbox_thumbnail_meta_id' => [
'label' => __( 'Checkbox?' ),
'input' => 'checkbox'
],
'url_thumbnail_meta_id' => [
'label' => __( 'Link:' ),
'type' => 'url',
],
'select_thumbnail_meta_id' => [
'label' => __( 'Display' ),
'input' => 'select',
'options' => [
'none' => '-- None --',
'top' => 'Content Top',
'right' => 'Content Right',
'left' => 'Content Left',
'bottom' => 'Content bottom',
'lhs' => 'LHS'
]
]
] );
}
<?php
/**
* Thumbnail Meta
*/
class Thumbnail_Meta
{
//Holds Media Fields
private $media_fields = [];
//Default arguments.
private static $default_item_args = [
'application' => 'image',
'exclusions' => ['audio', 'video'],
'input' => 'text'
];
/**
* Factory method for syntactical suger.
* @param $args
*/
public static function factory( $args ){
new Thumbnail_Meta( $args );
}
/**
* Thumbnail_Meta constructor.
* @param $args
*/
function __construct( $args )
{
$this->media_fields = $args;
add_filter( 'attachment_fields_to_edit', [ $this, 'render' ], 9, 2 );
add_filter( 'attachment_fields_to_save', [ $this, 'save' ], 9, 2 );
}
function
/**
* Render
*
* @description Method responsible for generating fields in the Media Editor.
*
* @param $form_fields
* @param $post
* @return mixed
*
* @since 2.32.0
*/
public function render($form_fields, $post = null)
{
// We browse our set of inputs
foreach ( $this->media_fields as $field => $values )
{
$values = wp_parse_args( $values, self::$default_item_args );
// If the field matches the current attachment mime type
// and is not one of the exclusions
if(
false !== strpos( $post->post_mime_type, 'image' ) &&
! in_array( $post->post_mime_type, $values['exclusions'] ) )
{
// We get the already saved field meta value
$meta = get_post_meta( $post->ID, "_" . $field, true );
// Define the input type to "text" by default
switch ($values['input'])
{
default:
case 'text':
$values['input'] = "text";
break;
case 'url':
$values['input'] = "text";
break;
case 'textarea':
$values['input'] = "textarea";
break;
case 'select':
// Select type doesn't exist, so we will create the html manually
// For this, we have to set the input type to "html"
$values['input'] = "html";
// Create the select element with the right name (matches the one that wordpress creates for custom fields)
$html = '<select name="attachments[' . $post->ID . '][' . $field . ']">';
// If options array is passed
if(isset($values['options']))
{
// Browse and add the options
foreach ($values['options'] as $k => $v)
{
// Set the option selected or not
if($meta == $k)
$selected = " selected='selected'";
else
$selected = "";
$html .= '<option' . $selected . ' value="'.$k.'">' . $v . '</option>';
}
}
$html .= "</select>";
// Set the html content
$values['html'] = $html;
break;
case 'checkbox':
// Checkbox type doesn't exist either
$values['input'] = "html";
// Set the checkbox checked or not
if( $meta )
$checked = ' checked="checked" value="1"';
else
$checked = ' ';
$html = '<input' . $checked . ' type="checkbox" name="attachments[' . $post->ID . '][' . $field . ']" id="attachments-' . $post->ID . '-' . $field . '" />';
$values['html'] = $html;
break;
case 'html':
$values['input'] = "html";
$html = "";
if( isset( $values['html'] ) )
{
$html .= $values['html'];
}else{
$html .= '<span></span>';
}
$values['html'] = $html;
break;
}
// And set it to the field before building it
$values['value'] = $meta;
// We add our field into the $form_fields array
$form_fields[$field] = $values;
}
}
// We return the completed $form_fields array
return $form_fields;
}
/**
* Save Meta
*
* @description Handle data changes.
*
* @param $post
* @param $attachment
* @return mixed
*/
function save($post, $attachment )
{
foreach ( $this->media_fields as $field => $values) {
$values = wp_parse_args( $values, self::$default_item_args );
if( $values['input'] === 'checkbox' ){
if (isset($attachment[$field])) {
update_post_meta( $post['ID'], "_" . $field, 1);
} else {
update_post_meta( $post['ID'], "_" . $field, 0);
}
} else {
if (isset($attachment[$field])) {
update_post_meta( $post['ID'], "_" . $field, $attachment[ $field ] );
} else {
delete_post_meta( $post['ID'], $field);
}
}
}
return $post;
}
}
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.