If you’re looking to automate the process of setting a single featured image for all pages on your WordPress site, this custom PHP code snippet can be a lifesaver. Below, we’ll break down the functionality and provide essential precautions to ensure your site remains safe.
Understanding the Code
The following PHP snippet performs these actions:
1. Validates the Image ID
Before applying the image as a featured image, the function ensures that the provided ID corresponds to a valid image attachment.
2. Fetches All Published Pages
Using a custom database query via the $wpdb object, it retrieves all published pages from the WordPress database.
3. Sets the Featured Image for Each Page
For every page retrieved, the set_post_thumbnail function assigns the specified image as its featured image.
4. Triggers via URL Parameter
The script listens for a specific URL parameter (?update_featured_image_for_all_pages=1) to initiate the process.
Here’s the full code:
function update_featured_image_for_all_pages( $attachment_id ) {
global $wpdb;
// Ensure the provided ID is a valid image
if ( ! wp_attachment_is_image( $attachment_id ) ) {
return 'Error: The specified ID is not an image.';
}
// Retrieve all pages from the database
$pages = $wpdb->get_col( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'page' AND post_status = 'publish'" );
if ( empty( $pages ) ) {
return 'No pages found.';
}
foreach ( $pages as $page_id ) {
// Set the featured image for each page
set_post_thumbnail( $page_id, $attachment_id );
}
return 'Featured image successfully updated for all pages.';
}
if ( isset( $_GET['update_featured_image_for_all_pages'] ) && $_GET['update_featured_image_for_all_pages'] === '1' ) {
$attachment_id = 280114; // Replace with your desired image attachment ID
$result = update_featured_image_for_all_pages( $attachment_id );
echo '<pre>';
print_r( $result );
echo '</pre>';
wp_die();
}
How to Use the Code
1. Backup Your Site
Before running any bulk updates on your WordPress site, always create a full backup of your database. This ensures you can restore your site if anything goes wrong.
2. Add the Code
Place the snippet in your theme’s functions.php file. You can find this file in the /wp-content/themes/your-theme/ directory.
3. Replace the Image ID
Change 280114 to the ID of the image you want to set as the featured image for all pages. To find the ID, navigate to the Media Library, click on the image, and look for the numeric ID in the URL.
4. Run the Script
Open your browser and visit https://yourwebsite.com/?update_featured_image_for_all_pages=1. Replace yourwebsite.com with your site’s URL. The script will execute, setting the specified image as the featured image for all published pages.
5. Verify Results
Go to your WordPress dashboard and check some pages to confirm the featured image has been applied.
6. Remove the Code
After running the script and verifying the results, immediately remove the code from your functions.php file. Leaving the script in place may pose a security risk.
Important Notes
• Use with Caution: This script overwrites existing featured images for all pages. Double-check your requirements before running the code.
• Backup First: A full database backup is essential to avoid accidental data loss.
• Security: Since this script can be triggered via URL, ensure it’s removed from your site once the task is completed.
Leave a Reply
You must be logged in to post a comment.