Make WordPress Core

Changeset 53741


Ignore:
Timestamp:
07/20/2022 09:11:30 PM (4 years ago)
Author:
audrasjb
Message:

Themes: Add a hook to filter theme header image URL.

This changeset introduces the get_header_image filter, which can be used to modify header image URL returned by get_header_image(), in themes that support the Header Image feature.

Props hztyfoon, audrasjb, mukesh27, SergeyBiryukov, costdev.
Fixes #56180.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/theme.php

    r53704 r53741  
    11801180        }
    11811181
     1182        /**
     1183         * Filters the header image URL.
     1184         *
     1185         * @since 6.1.0
     1186         *
     1187         * @param string $url Header image URL.
     1188         */
     1189        $url = apply_filters( 'get_header_image', $url );
     1190
     1191        if ( ! is_string( $url ) ) {
     1192                return false;
     1193        }
     1194
     1195        $url = trim( $url );
    11821196        return sanitize_url( set_url_scheme( $url ) );
    11831197}
  • trunk/tests/phpunit/tests/theme/customHeader.php

    r52010 r53741  
    8080                $this->assertFalse( has_header_image() );
    8181                $this->assertFalse( $image );
     82        }
     83
     84        /**
     85         * Tests the "get_header_image" filter.
     86         *
     87         * @ticket 56180
     88         *
     89         * @covers get_header_image
     90         *
     91         * @dataProvider data_filter_header_image
     92         *
     93         * @param mixed  $header_image The header image.
     94         * @param string $expected     The expected return value from get_header_image().
     95         */
     96        public function test_filter_header_image( $header_image, $expected ) {
     97                add_filter(
     98                        'get_header_image',
     99                        static function() use ( $header_image ) {
     100                                return $header_image;
     101                        }
     102                );
     103
     104                $this->assertSame( $expected, get_header_image() );
     105        }
     106
     107        /**
     108         * Data provider.
     109         *
     110         * @return array
     111         */
     112        public function data_filter_header_image() {
     113                return array(
     114                        'an image url'         => array(
     115                                'header_image' => 'http://example.org/image.png',
     116                                'expected'     => 'http://example.org/image.png',
     117                        ),
     118                        'an empty string'      => array(
     119                                'header_image' => '',
     120                                'expected'     => '',
     121                        ),
     122                        'a string with spaces' => array(
     123                                'header_image' => ' ',
     124                                'expected'     => '',
     125                        ),
     126                        'null'                 => array(
     127                                'header_image' => null,
     128                                'expected'     => false,
     129                        ),
     130                        'false'                => array(
     131                                'header_image' => false,
     132                                'expected'     => false,
     133                        ),
     134                );
    82135        }
    83136
Note: See TracChangeset for help on using the changeset viewer.