Today I Learned - Rocky Kev

TIL about how to quickly add a WP Theme customizer Option

POSTED ON:

TAGS:

In WordPress, when you go to

Appearances > Customize:

You can do a lot of theme customization that auto-loads the page with every setting change.
It's a really nice feature.

As a developer, maybe you want to include a custom setting in the WordPress Theme Customizer, using the WordPress Theme Customizer API.

AN EXAMPLE:

/**
* Typically in your functions.php
* Create Logo Setting and Upload Control.
*/

function tracy_new_customizer_settings($wp_customize) {
// add a setting for the site logo
$wp_customize->add_setting('add_theme_logo');
// Add a control to upload the logo
$wp_customize->add_control(new WP_Customize_Image_Control(
$wp_customize,
'add_theme_logo',
array(
'label' => 'Add Banner Logo (1200px)',
'section' => 'title_tagline',
'settings' => 'add_theme_logo',
)
));
}

add_action('customize_register', 'tracy_new_customizer_settings');
/**
* Typically in your page-template.php
* Call your new theme option element.
*/

<?php if (get_theme_mod('add_theme_logo')): ?>

<img src="<?php echo get_theme_mod('add_theme_logo'); ?>" alt="<?php echo esc_attr(get_bloginfo('name', 'display')); ?>">

<?php endif; ?>

REF:
https://codex.wordpress.org/Theme_Customization_API


Related TILs

Tagged:

TIL Static Blocks vs Dynamic Blocks

A static block is a piece of content whose markup is known when the page is saved. The block saves its content and markup directly in the post content. A dynamic block is a piece of content whose markup and exact content are not known when the page is saved.

TIL how to convert a shortcode to a WP block

Traditionally, shortcodes were a way that plugin developers could provide users the ability to add specific plugin functionality anwhere on their site. But shortcodes are not very user friendly, nor was hunting down the relevant data you needed to render the correct data for the shortcode. Converting existing shortcodes to blocks provides a much greater user experience in all aspects.

TIL how WordPress does serverside

This isn't fully accurate, but for the means of describing server-side rendering, it's a good start!