Make WordPress Core


Ignore:
Timestamp:
04/05/2012 12:20:28 AM (12 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/custom-background.php

    r20212 r20358  
    5656
    5757        add_action( 'admin_menu', array( $this, 'init' ) );
     58        add_action( 'wp_ajax_set-background-image', array( $this, 'wp_set_background_image' ) );
    5859    }
    5960
     
    7273        add_action("load-$page", array(&$this, 'take_action'), 49);
    7374        add_action("load-$page", array(&$this, 'handle_upload'), 49);
     75        add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 10, 2 );
     76        add_filter( 'media_upload_tabs', array( $this, 'filter_upload_tabs' ) );
    7477
    7578        if ( $this->admin_header_callback )
     
    99102        );
    100103
     104        add_thickbox();
     105        wp_enqueue_script('media-upload');
    101106        wp_enqueue_script('custom-background');
    102107        wp_enqueue_style('farbtastic');
     
    127132            set_theme_mod('background_image_thumb', '');
    128133            $this->updated = true;
     134            wp_safe_redirect( $_POST['_wp_http_referer'] );
    129135            return;
    130136        }
     
    249255<?php wp_nonce_field('custom-background-upload', '_wpnonce-custom-background-upload') ?>
    250256<?php submit_button( __( 'Upload' ), 'button', 'submit', false ); ?>
    251 </form>
     257<?php
     258    $image_library_url = get_upload_iframe_src( 'image', null, 'library' );
     259    $image_library_url = remove_query_arg( 'TB_iframe', $image_library_url );
     260    $image_library_url = add_query_arg( array( 'context' => 'custom-background', 'TB_iframe' => 1 ), $image_library_url );
     261?>
     262    </form>
     263    <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>
    252264</td>
    253265</tr>
     
    369381    }
    370382
     383    function attachment_fields_to_edit( $form_fields, $post ) {
     384        if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' ) {
     385            $form_fields = array( 'image-size' => $form_fields['image-size'] );
     386            $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>' );
     387            $form_fields['context'] = array( 'input' => 'hidden', 'value' => 'custom-background' );
     388        }
     389
     390        return $form_fields;
     391    }
     392
     393    function filter_upload_tabs ( $tabs ){
     394        if ( isset( $_REQUEST['context'] ) && $_REQUEST['context'] == 'custom-background' )
     395            return array( 'library' => __('Media Library') );
     396
     397        return $tabs;
     398    }
     399
     400    public function wp_set_background_image() {
     401        if ( ! current_user_can('edit_theme_options') || ! isset( $_POST['attachment_id'] ) ) exit;
     402        $attachment_id = absint($_POST['attachment_id']);
     403        $sizes = array_keys(apply_filters( 'image_size_names_choose', array('thumbnail' => __('Thumbnail'), 'medium' => __('Medium'), 'large' => __('Large'), 'full' => __('Full Size')) ));
     404        $size = 'thumbnail';
     405        if ( in_array( $_POST['size'], $sizes ) )
     406            $size = esc_attr( $_POST['size'] );
     407
     408        update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', get_option('stylesheet' ) );
     409        $url = wp_get_attachment_image_src( $attachment_id, $size );
     410        $thumbnail = wp_get_attachment_image_src( $attachment_id, 'thumbnail' );
     411        set_theme_mod( 'background_image', esc_url( $url[0] ) );
     412        set_theme_mod( 'background_image_thumb', esc_url( $thumbnail[0] ) );
     413        exit;
     414    }
    371415}
Note: See TracChangeset for help on using the changeset viewer.