Wordpress - Image Author Link

Adds extra meta boxes to save image author name and url. Shows image credits in the end of post content


<?php
/**
* Image Author Link
*
* Extra Meta Box to save image author name and url to display credits in the end of post content
*
* @since WW Theme 1.0
*/

/* Define the custom box */
add_action( 'add_meta_boxes', 'ww_image_author_add_custom_box' );

/* Do something with the data entered */
add_action( 'save_post', 'ww_image_author_save_postdata' );

/* Add image author link to post */
add_filter( 'the_content', 'ww_add_image_author_link_to_post_content', 200 );

/* Global variables */
$ww_image_author_name = '';
$ww_image_author_url = '';

/* Adds a box to the main column on the Post and Page edit screens */
function ww_image_author_add_custom_box() {
    add_meta_box(
        'ww_image_author_sectionid',
        'Bildkälla',
        'ww_image_author_inner_custom_box',
        'post',
  'side',
  'low'
    );
    add_meta_box(
        'ww_image_author_sectionid',
        'Bildkälla',
        'ww_image_author_inner_custom_box',
        'page',
  'side',
  'low'
    );
}

/* Prints the box content */
function ww_image_author_inner_custom_box( $post ) {

wp_nonce_field( 'ww_image_author_nonce', 'ww_image_author_noncename' );  // Use nonce for verification

global $ww_image_author_name, $ww_image_author_url;
$ww_image_author_name = get_post_meta($post->ID, 'ww_image_author_name', true);
$ww_image_author_url = get_post_meta($post->ID, 'ww_image_author_url', true);

// The actual fields for data entry
   echo '<p><strong>Namn</strong></p>';
echo '<label for="ww_image_author_name_field" class="screen-reader-text">Namn</label> ';
echo '<input type="text" id="ww_image_author_name" name="ww_image_author_name" value="'.$ww_image_author_name.'" size="40" />';
echo '<p><strong>Adress</strong></p>';
echo '<label for="ww_image_author_name_field" class="screen-reader-text">Adress</label> ';
echo '<input type="text" id="ww_image_author_url" name="ww_image_author_url" value="'.$ww_image_author_url.'" size="40" />';
}

/* When the post is saved, saves our custom data */
function ww_image_author_save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
  return;

// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['ww_image_author_noncename'], 'ww_image_author_nonce' ) )
  return;

// Check permissions
if ( 'page' == $_POST['post_type'] ) {
  if ( !current_user_can( 'edit_page', $post_id ) )
  return;
} else {
  if ( !current_user_can( 'edit_post', $post_id ) )
  return;
}

// OK, we're authenticated: Now save the data
ww_save_post_meta($post_id, 'ww_image_author_name', $_POST['ww_image_author_name']);
ww_save_post_meta($post_id, 'ww_image_author_url', $_POST['ww_image_author_url']);
}

function ww_save_post_meta($post_id, $meta_name, $meta_value) {
if ( get_post_meta($post_id, $meta_name == "") ) {
  add_post_meta($post_id, $meta_name, $meta_value, true);
  return true;
} elseif ( $data != get_post_meta($post_id, $meta_name, true) ) {
  update_post_meta($post_id, $meta_name, $meta_value, true);
} elseif ($post_meta_value == "") {
  delete_post_meta($post_id, $post_meta_name);
}
}




function ww_add_image_author_link_to_post_content( $content ) {

global $post;
$ww_image_author_name = get_post_meta($post->ID, 'ww_image_author_name', true);
$ww_image_author_url = get_post_meta($post->ID, 'ww_image_author_url', true);


if( $ww_image_author_name == '' || $ww_image_author_url == '' )
  return $content;

    return $content . '<p class="ww_image_author"><b>Bild:</b> <a href="'.$ww_image_author_url.'" target="_blank">'.$ww_image_author_name.'</a></p>';
}

Creative Commons-licens
Detta verk av Westin Produktion är licensierat under en Creative Commons Erkännande-DelaLika 3.0 Unported-licens.