Skip to content

Instantly share code, notes, and snippets.

@stingray82
Last active November 18, 2023 13:15
Show Gist options
  • Select an option

  • Save stingray82/adc4ea11ac37e6dc143116b7a6d1f938 to your computer and use it in GitHub Desktop.

Select an option

Save stingray82/adc4ea11ac37e6dc143116b7a6d1f938 to your computer and use it in GitHub Desktop.
Additional Items to Media Library for image licencing
<?php
// Add custom fields for image attribution and license information in media library
function add_image_attribution_field($form_fields, $post) {
$form_fields['image_attribution'] = array(
'label' => 'Image Attribution',
'input' => 'text',
'value' => get_post_meta($post->ID, 'image_attribution', true),
);
$form_fields['image_license_for'] = array(
'label' => 'License For',
'input' => 'text',
'value' => get_post_meta($post->ID, 'image_license_for', true),
);
$form_fields['image_license_url'] = array(
'label' => 'License URL',
'input' => 'text',
'value' => get_post_meta($post->ID, 'image_license_url', true),
);
return $form_fields;
}
function save_image_custom_fields($attachment_id) {
if (isset($_POST['image_attribution'])) {
update_post_meta($attachment_id, 'image_attribution', sanitize_text_field($_POST['image_attribution']));
}
if (isset($_POST['image_license_for'])) {
update_post_meta($attachment_id, 'image_license_for', sanitize_text_field($_POST['image_license_for']));
}
if (isset($_POST['image_license_url'])) {
update_post_meta($attachment_id, 'image_license_url', sanitize_text_field($_POST['image_license_url']));
}
}
add_filter('attachment_fields_to_edit', 'add_image_attribution_field', 10, 2);
add_filter('attachment_fields_to_save', 'save_image_custom_fields', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment