Allow selecting custom header and background images from the media library. Props aaroncampbell, sabreuse, greuben. fixes #19840

git-svn-id: https://develop.svn.wordpress.org/trunk@20358 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren
2012-04-05 00:20:28 +00:00
parent f027063f41
commit 485e7bc261
5 changed files with 148 additions and 39 deletions

View File

@@ -55,6 +55,7 @@ class Custom_Background {
$this->admin_image_div_callback = $admin_image_div_callback;
add_action( 'admin_menu', array( $this, 'init' ) );
add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
}
/**
@@ -71,6 +72,8 @@ class Custom_Background {
add_action("load-$page", array(&$this, 'admin_load'));
add_action("load-$page", array(&$this, 'take_action'), 49);
add_action("load-$page", array(&$this, 'handle_upload'), 49);
add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
if ( $this->admin_header_callback )
add_action("admin_head-$page", $this->admin_header_callback, 51);
@@ -98,6 +101,8 @@ class Custom_Background {
'<p>' . __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' ) . '</p>'
);
add_thickbox();
wp_enqueue_script('media-upload');
wp_enqueue_script('custom-background');
wp_enqueue_style('farbtastic');
}
@@ -126,6 +131,7 @@ class Custom_Background {
set_theme_mod('background_image', '');
set_theme_mod('background_image_thumb', '');
$this->updated = true;
wp_safe_redirect( $_POST['_wp_http_referer'] );
return;
}
@@ -248,7 +254,13 @@ if ( get_background_image() ) {
<input type="hidden" name="action" value="save" />
<?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
<?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
</form>
<?php
$image_library_url = get_upload_iframe_src( 'image', null, 'library' );
$image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
$image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
?>
</form>
<span class="howto"><?php _ex( 'or', 'Custom Background: Choose an image from your computer - or - Choose from image library' ); ?></span> <a class="thickbox" href="<?php echo $image_library_url; ?>"><?php _e( 'Choose from image library' ); ?></a>
</td>
</tr>
</tbody>
@@ -368,4 +380,36 @@ if ( get_background_image() ) {
$this->updated = true;
}
function attachment_fields_to_edit( $form_fields, $post ) {
if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
$form_fields = array( 'image-size' => $form_fields['image-size'] );
$form_fields['buttons'] = array( 'tr' => '<tr class="submit"><td></td><td><a data-attachment-id="' . $post->ID . '" class="wp-set-background">' . _( 'Set as background' ) . '</a></td></tr>' );
$form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
}
return $form_fields;
}
function filter_upload_tabs ( $tabs ){
if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' )
return array( 'library' => __('Media Library') );
return $tabs;
}
public function wp_set_background_image() {
if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
$attachment_id = absint($_POST['attachment_id']);
$sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
$size = 'thumbnail';
if ( in_array( $_POST['size'], $sizes ) )
$size = esc_attr( $_POST['size'] );
update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
$url = wp_get_attachment_image_src( $attachment_id, $size );
$thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
set_theme_mod( 'background_image', esc_url( $url[0] ) );
set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
exit;
}
}