Analysis of PHP functions related to custom headers in WordPress topic creation

Source: Internet
Author: User
This article describes how to parse PHP functions related to custom headers in WordPress theme production, including header_image () and get_custom_header () functions. For more information, see Header_image ()
The header_image () function is a standard interface function used by WordPress to customize the top image. This function can automatically determine the background settings and return the user-defined top image address in string format. This document describes and uses this function.

[Display header image path.]: displays the top image address.
Use

The code is as follows:

"Width =" <? Php echo $ header_image_width;?> "Height =" <? Php echo $ header_image_height;?> "Alt =" "/>

Function declaration source code

function header_textcolor() { echo get_header_textcolor();}function get_header_image() { $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );  if ( 'remove-header' == $url ) return false;  if ( is_random_header_image() ) $url = get_random_header_image();  if ( is_ssl() ) $url = str_replace( 'http://', 'https://', $url ); else $url = str_replace( 'https://', 'http://', $url );  return esc_url_raw( $url );}

Get_custom_header custom top
The get_custom_header function is a new gift from WordPress 3.4. This function is used to better integrate and encapsulate the use of the top layer, this article describes the get_custom_header function and how to integrate the top function in the topic of WordPress 3.4.

Make sure that your WordPress has been upgraded to version 3.4.

Get_custom_header
Currently, most themes on the custom top only use two functions. 1. Custom top image 2. Custom top style
For more information, see the default theme twenty eleven.
This function is a built-in function that appears only after WP 3.4. it is mainly used to return parameters at the top of the user's settings as objects.
You may not understand this. if you want to understand it, please read it down.
Note that this function is essentially different from get_header.

Function instance
The following example is from the header. php file in the default theme twenty eleven.
PHP code:

// Determine whether the function exists to be compatible with the old version if (function_exists ('Get _ custom_header ') {// get_custom_header () -> width call the directed width attribute $ header_image_width = get_custom_header ()-> width; // get_custom_header ()-> height call the directed height attribute $ header_image_height = get_custom_header ()-> height ;} else {// code compatible with earlier versions $ header_image_width = HEADER_IMAGE_WIDTH; $ header_image_height = HEADER_IMAGE_HEIGHT ;}

Comprehensive usage
Refer to the official document below to explain the custom top

// Enable the add_theme_support ('m M-head'); $ headarg = array (// package the settings into an array 'default-image' => '', 'random-default' => false, 'width' => 0, 'height' => 0, 'Flex-height' => false, 'flex-width' => false, 'default-text-color' => '', 'header-text' => true, 'uploads' => true, 'WP-head-callback' => '', 'admin-head-callback' =>'', 'admin-preview-callback' => '',); // add the settings in the array to the custom top add_theme_support ('custom-head', $ headarg );

Custom top image

// Enable the add_theme_support ('m M-head'); $ headarg = array (// package the settings into an array 'default-image' => '', 'random-default' => false, 'width' => 0, 'height' => 0, 'Flex-height' => false, 'flex-width' => false, 'default-text-color' => '', 'header-text' => true, 'uploads' => true, 'WP-head-callback' => '', 'admin-head-callback' =>'', 'admin-preview-callback' => '',); // add the settings in the array to the custom top add_theme_support ('custom-head', $ headarg );

Adaptive top image settings

$ Args = array ('flex-width' => true, // adaptive height 'width' => 980, 'Flex-width' => true, // adaptive width 'height' => 200, 'default-image' => get_template_directory_uri (). '/images/header.jpg',); add_theme_support ('m M-head', $ args );

Call custom top image

"   height="<?php echo get_custom_header()->height; ?>"   width="<?php echo get_custom_header()->width; ?>"   alt="" />

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.