Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#33366 closed defect (bug) (invalid)

if (get_theme_mod()) returns false if 0

Reported by: karlikdesign's profile karlikdesign Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.2.4
Component: Customize Keywords:
Focuses: Cc:

Description

Seems like the number type isn't returning 0 in the customizer

Here's my control code in functions.php

$wp_customize->add_setting('apw_header_cta_padding_bottom', array( 'default' => '20' ) );
	$wp_customize->add_control(
		new Customize_Number_Control(
			$wp_customize,
			'header_cta_padding_bottom',
			array(
				'label'      => 'Bottom Padding',
				'section'    => 'apw_header_cta',
				'settings'   => 'apw_header_cta_padding_bottom',
				'type'		 => 'number',
				'priority'	 => 30
			)
		)
	);	

Then, in my header.php I have

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

Then, when I preview the site in the customizer and set that value to zero, this if statement returns false when the value should be zero.

Change History (4)

#1 follow-up: @westonruter
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

@karlikdesign: Thanks for the report. Actually this is not a bug because in PHP the value of 0 is “false-y”. You can see the values in PHP that are considered false in a boolean context via the PHP Manual.

So all you have to do is change your conditional to check for the false value explicitly with a not-identical comparison:

<?php if ( false !== get_theme_mod( 'apw_header_cta_padding_bottom' ) ) : ?>

You probably want to report such future issues to the WordPress Support forums first before opening a Core Trac ticket. Thanks!

#2 in reply to: ↑ 1 @karlikdesign
10 years ago

oh whoops! You're right. That did work :)

Really appreciate your help!!!

Replying to westonruter:

@karlikdesign: Thanks for the report. Actually this is not a bug because in PHP the value of 0 is “false-y”. You can see the values in PHP that are considered false in a boolean context via the PHP Manual.

So all you have to do is change your conditional to check for the false value explicitly with a not-identical comparison:

<?php if ( false !== get_theme_mod( 'apw_header_cta_padding_bottom' ) ) : ?>

You probably want to report such future issues to the WordPress Support forums first before opening a Core Trac ticket. Thanks!

#3 follow-up: @celloexpressions
10 years ago

I would also suggest setting the default return value in the 2nd argument of the get_theme_mod() call, which would also result in more predictable behavior:

get_theme_mod( 'apw_header_cta_padding_bottom', 0 );

#4 in reply to: ↑ 3 @karlikdesign
10 years ago

Good call!

Note: See TracTickets for help on using tickets.