| |
| <?php
|
| add_action('add_meta_boxes', function() {
|
| add_meta_box(
|
| 'custom-logo-metabox',
|
| 'Custom Logo',
|
| function($post) {
|
|
|
| wp_nonce_field('custom_logo_meta_box', 'custom_logo_meta_box_nonce');
|
|
|
|
|
| $custom_logo_id = get_post_meta($post->ID, '_custom_logo_id', true);
|
| $custom_logo_url = $custom_logo_id ? wp_get_attachment_image_url($custom_logo_id, 'thumbnail') : '';
|
|
|
|
|
| echo '<div style="margin-bottom: 10px;">';
|
| echo '<img id="custom-logo-preview" src="' . esc_url($custom_logo_url) . '" style="max-width: 100%; display: ' . ($custom_logo_url ? 'block' : 'none') . ';" />';
|
| echo '<input type="hidden" id="custom_logo_id" name="custom_logo_id" value="' . esc_attr($custom_logo_id) . '" />';
|
| echo '<button type="button" class="button" id="upload_custom_logo_button">Upload Logo</button>';
|
| echo '<button type="button" class="button" id="remove_custom_logo_button" style="display: ' . ($custom_logo_url ? 'inline-block' : 'none') . ';">Remove Logo</button>';
|
| echo '</div>';
|
|
|
|
|
| ?>
|
| <script>
|
| jQuery(document).ready(function($){
|
| var file_frame;
|
| $('#upload_custom_logo_button').on('click', function(event){
|
| event.preventDefault();
|
| if (file_frame) {
|
| file_frame.open();
|
| return;
|
| }
|
|
|
| file_frame = wp.media.frames.file_frame = wp.media({
|
| title: 'Select or Upload a Custom Logo',
|
| button: {
|
| text: 'Use this logo',
|
| },
|
| multiple: false
|
| });
|
|
|
| file_frame.on('select', function() {
|
| var attachment = file_frame.state().get('selection').first().toJSON();
|
| $('#custom_logo_id').val(attachment.id);
|
| $('#custom-logo-preview').attr('src', attachment.sizes.thumbnail.url).show();
|
| $('#remove_custom_logo_button').show();
|
| });
|
|
|
| file_frame.open();
|
| });
|
|
|
| $('#remove_custom_logo_button').on('click', function() {
|
| $('#custom_logo_id').val('');
|
| $('#custom-logo-preview').hide();
|
| $(this).hide();
|
| });
|
| });
|
| </script>
|
| <?php
|
| },
|
| 'page',
|
| 'side'
|
| );
|
| });
|
|
|
| add_action('save_post', function($post_id) {
|
|
|
| if (!isset($_POST['custom_logo_meta_box_nonce'])) {
|
| return;
|
| }
|
|
|
|
|
| if (!wp_verify_nonce($_POST['custom_logo_meta_box_nonce'], 'custom_logo_meta_box')) {
|
| return;
|
| }
|
|
|
|
|
| if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
|
| return;
|
| }
|
|
|
|
|
| if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) {
|
| if (!current_user_can('edit_page', $post_id)) {
|
| return;
|
| }
|
| } else {
|
| if (!current_user_can('edit_post', $post_id)) {
|
| return;
|
| }
|
| }
|
|
|
|
|
| if (isset($_POST['custom_logo_id'])) {
|
| $custom_logo_id = absint($_POST['custom_logo_id']);
|
| update_post_meta($post_id, '_custom_logo_id', $custom_logo_id);
|
| }
|
| });
|
|
|
| add_filter('get_custom_logo', function($html) {
|
| if (is_page()) {
|
| $custom_logo_id = get_post_meta(get_the_ID(), '_custom_logo_id', true);
|
| if ($custom_logo_id) {
|
| $custom_logo_url = wp_get_attachment_image_url($custom_logo_id, 'full');
|
| if ($custom_logo_url) {
|
|
|
| $html = '<img src="' . esc_url($custom_logo_url) . '" alt="' . get_bloginfo('name') . '">';
|
| }
|
| }
|
| }
|
| return $html;
|
| });
|
|
|
| add_filter('get_site_icon_url', function($url, $size, $blog_id) {
|
| if (is_page()) {
|
|
|
| $post_id = get_the_ID();
|
|
|
|
|
| $custom_logo_id = get_post_meta($post_id, '_custom_logo_id', true);
|
| if ($custom_logo_id) {
|
| $custom_logo_url = wp_get_attachment_image_url($custom_logo_id, array($size, $size));
|
| if ($custom_logo_url) {
|
| return $custom_logo_url;
|
| }
|
| }
|
| }
|
| return $url;
|
| }, 10, 3);
|
| |
| |
Comments