• Resolved joerns

    (@joerns)


    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)
  • Plugin Author Claude Vedovini

    (@cvedovini)

    isn’t that something you’d rather put in the footer or someplace that is always there? because as I understand it, using the plugin you will only show the background image credits when a single post or page is displayed, whereas the background image is _always_ displayed. Hence you won’t give credit on archive pages for example.

    Thread Starter joerns

    (@joerns)

    No, it is meant to work everywhere, and works for me on every page I tested. However in WordPress it is complicated to retrieve the ID of the currently selected background image, so the code a bit longer.

    Plugin Author Claude Vedovini

    (@cvedovini)

    I am pretty sure that the way it’s done the credits for that background will either be repeated or absent on an archive page (blog, category or tag page)
    Do you have a link to your website you can send me?

    Thread Starter joerns

    (@joerns)

    Well, unfortunatley the site isnt online yet.

    However I have a screenshot: https://dl.dropboxusercontent.com/u/2311504/Unbenannt.PNG

    Really works on all pages.

    Thread Starter joerns

    (@joerns)

    Have a look at: http://verein.eckmannshain.de/ (in the footer)

    However I have to admit that this does not always work if the image is added by some plugins (e.g. pixabay plugin). If the image is uploaded via media uploader, it works. I could investiage that but am busy at the moment.

    Plugin Author Claude Vedovini

    (@cvedovini)

    do you use the template tag to output the credits in the footer?

    Plugin Author Claude Vedovini

    (@cvedovini)

    Hi, I just released version 1.4 which adds support for the custom header and background images. I also added a widget that you can put in the footer or the sidebars.
    Thanks for your contribution 🙂

    Thread Starter joerns

    (@joerns)

    You’re welcome. I always prefer contributing to existing plugins rather than creating an indefinite number of forks 😉

    Output is done by calling if(function_exists('the_image_credits')) the_image_credits(); in my footer.php.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Background image’ is closed to new replies.