Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#31271 closed enhancement (fixed)

Allow is_page_template() to accept an array of templates to check.

Reported by: morganestes's profile morganestes Owned by: drewapicture's profile DrewAPicture
Milestone: 4.2 Priority: normal
Severity: normal Version: 4.2
Component: Posts, Post Types Keywords: has-patch commit
Focuses: template Cc:

Description

Currently, is_page_template() takes an optional single template name to check. This would also allow for an array of strings to check.

Before:

if ( is_page_template( 'page-one.php' ) || is_page_template( 'page-two.php' ) || is_page_template( 'page-four.php' ) ) { 
    throw_hhg( false );
 }

After:

if ( is_page_template( array( 'page-one.php' , 'page-two.php',  'page-three.php' ) ) ) {
    throw_hhg( true );
}

Related: #15061

Attachments (6)

31271.diff (1.2 KB) - added by morganestes 10 years ago.
31271-tests.patch (1.1 KB) - added by tyxla 10 years ago.
Adding a unit test for the is_page_template() array parameter case. Also, adding a unit test for the is_page_template() string parameter case.
31271.2.diff (2.3 KB) - added by DrewAPicture 10 years ago.
docs fixes
31271.3.diff (2.4 KB) - added by DrewAPicture 10 years ago.
Accounts for 'default'
31271.4.diff (2.6 KB) - added by DrewAPicture 10 years ago.
Tests for 'default'
31271.5.diff (2.7 KB) - added by DrewAPicture 10 years ago.

Download all attachments as: .zip

Change History (17)

@morganestes
10 years ago

@tyxla
10 years ago

Adding a unit test for the is_page_template() array parameter case. Also, adding a unit test for the is_page_template() string parameter case.

#1 @tyxla
10 years ago

  • Keywords has-patch added

Great idea. Personally I find that useful - it would help avoiding repetitive is_page_template() calls, making such checks shorter. I'd definitely use that often in my projects.

I've just added a patch that contains 2 unit tests for is_page_template() with the 2 parameter types.

#2 @SergeyBiryukov
10 years ago

  • Milestone changed from Awaiting Review to 4.2

#3 @helen
10 years ago

  • Focuses template added

This ticket was mentioned in Slack in #core by johnbillion. View the logs.


10 years ago

#5 @DrewAPicture
10 years ago

  • Owner set to DrewAPicture
  • Status changed from new to reviewing

@DrewAPicture
10 years ago

docs fixes

#6 @DrewAPicture
10 years ago

  • Keywords commit added

31271.2.diff makes the docs adjustments.

@DrewAPicture
10 years ago

Accounts for 'default'

#7 @helen
10 years ago

  • Keywords commit removed

Seems like it would be nice to get some tests going for the default case. Would also highly recommend wrapping in_array( 'default', $template, true ) && ! $page_template in parens so it's clear what the grouping is, rather than trickily relying on operator precedence.

This ticket was mentioned in Slack in #core by helen. View the logs.


10 years ago

@DrewAPicture
10 years ago

Tests for 'default'

#9 @DrewAPicture
10 years ago

  • Keywords commit added

31271.4.diff adds tests for 'default'.

This ticket was mentioned in Slack in #core by drew. View the logs.


10 years ago

#11 @helen
10 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 31754:

Allow is_page_template() to accept an array, as many other conditional tags do.

props morganestes, tyxla, DrewAPicture.
fixes #31271.

Note: See TracTickets for help on using tickets.