Background image
-
Hi,
I added support for background image credits to your plugin. I also added display of the image title. However, I did not add any admin ui.
Feel free to add the code or parts of it to your plugin.
function get_image_credits() { global $post; $attachment_ids = array(); $credits = array(); $header_id = NULL; $background_id = NULL; // First first check background image $query_images = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'meta_key' => '_wp_attachment_is_custom_background', 'meta_value' => get_option('stylesheet') ) ); //$query_images->get_posts(); foreach ( $query_images->posts as $image ) { if ( get_background_image() != $image->guid ) continue; $background_id = $image->ID; $attachment_ids[] = $background_id; break; } // First check for post thumbnail and save its ID in an array if (function_exists('has_post_thumbnail') && has_post_thumbnail($post->ID)) { $header_id = get_post_thumbnail_id($post->ID); $attachment_ids[] = $header_id; } // Next look in post content and check for instances of wp-image-[digits] if (preg_match_all('/wp-image-(\d+)/i', $post->post_content, $matches)) { foreach ($matches[1] as $id) { if (!in_array($id, $attachment_ids)) { $attachment_ids[] = $id; } } } // Go through all our attachments IDs and generate credits foreach ($attachment_ids as $id) { // Add names $credit_source = esc_attr(get_post_meta($id, '_wp_attachment_source_name', true)); if (empty($credit_source)) continue; // Nobody to credit $credit_text = ''; if(get_the_title($id) != '') $credit_text .= esc_attr(get_the_title($id)) .': '; $credit_text .= $credit_source; // Maybe add if its header or background image $usage = array(); if($id == $background_id) $usage[] = __( 'Background Image' ); if($id == $header_id) $usage[] = __( 'Header Image' ); if(!empty($usage)) $credit_text .= ' (' . implode($usage, ', ') . ')'; $credit_link = esc_url(get_post_meta($id, '_wp_attachment_source_url', true)); if (empty($credit_link)) { $credits[] = $credit_text; } else { $credits[] = '<a href="' . $credit_link . '">' . $credit_text . '</a>'; } } return array_unique($credits); }
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Background image’ is closed to new replies.