Skip to content

Instantly share code, notes, and snippets.

@krystyna93
Last active June 10, 2023 13:45
Show Gist options
  • Save krystyna93/3cdd8ae3fa9beabcf5f5746f3696729b to your computer and use it in GitHub Desktop.
Save krystyna93/3cdd8ae3fa9beabcf5f5746f3696729b to your computer and use it in GitHub Desktop.
Custom Featured Image Metabox: Text Field for Image From URL
<?php
/*
The purpose of this field is to allow you to enter a custom URL for an image, which can be used as the featured image for your
WordPress post or page. This could be useful if you have an image that you want to use as the featured image, but it is not hosted
on your own website and therefore cannot be easily uploaded through the WordPress media library.
By adding this custom field, you can enter the URL of the external image and then use it as the featured image for your post or page,
just as you would with a standard image uploaded through the WordPress media library.
*/
function add_custom_featured_image_field() {
// Check if user has permission to edit posts
if (!current_user_can('edit_posts')) {
return;
}
// Prevent cross-site scripting (XSS) attacks
wp_nonce_field(basename(__FILE__), 'custom_featured_image_nonce');
// Get the saved custom featured image URL for this post
$custom_featured_image_url = get_post_meta(get_the_ID(), 'custom_featured_image_url', true);
// Sanitize and validate the input
$custom_featured_image_url = esc_url_raw($custom_featured_image_url);
if (!filter_var($custom_featured_image_url, FILTER_VALIDATE_URL)) {
$custom_featured_image_url = '';
}
// Output the custom input field
?>
<div class="custom-featured-image-field">
<label for="custom_featured_image">Custom Featured Image:</label>
<input type="text" name="custom_featured_image" id="custom_featured_image" value="<?php echo $custom_featured_image_url; ?>">
</div>
<?php
}
// Save the custom featured image URL when the post is saved
function save_custom_featured_image_url($post_id) {
// Check if user has permission to edit this post
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Verify the nonce field to prevent CSRF attacks
if (!isset($_POST['custom_featured_image_nonce']) || !wp_verify_nonce($_POST['custom_featured_image_nonce'], basename(__FILE__))) {
return;
}
// Sanitize and validate the input
$custom_featured_image_url = isset($_POST['custom_featured_image']) ? esc_url_raw($_POST['custom_featured_image']) : '';
if (!filter_var($custom_featured_image_url, FILTER_VALIDATE_URL)) {
$custom_featured_image_url = '';
}
// Save the custom featured image URL
update_post_meta($post_id, 'custom_featured_image_url', $custom_featured_image_url);
}
add_action('save_post', 'save_custom_featured_image_url');
// Limit access to the custom featured image field to admin users only
function restrict_custom_featured_image_field() {
if (!current_user_can('manage_options')) {
remove_post_type_support('post', 'thumbnail');
}
}
add_action('admin_init', 'restrict_custom_featured_image_field');
/*
To display the custom featured image on the front end, you can use the get_post_meta() function to retrieve the saved custom
featured image URL and then output it as an <img> tag. Here's an example code snippet:
*/
<?php
// Get the saved custom featured image URL for this post
$custom_featured_image_url = get_post_meta(get_the_ID(), 'custom_featured_image_url', true);
// Output the custom featured image
if ($custom_featured_image_url) {
echo '<img src="' . esc_attr($custom_featured_image_url) . '" alt="">';
} else {
// fallback to the default featured image
the_post_thumbnail();
}
/* This code retrieves the saved custom featured image URL using get_post_meta(), checks if the URL exists and outputs
it as an <img> tag using echo. If no custom featured image is set for the post, it falls back to the default featured image
using the_post_thumbnail().
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment