Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#31271 closed enhancement (fixed)

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

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

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 12 years ago.
31271-tests.patch​ (1.1 KB ) - added by tyxla 12 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 12 years ago.
docs fixes
31271.3.diff​ (2.4 KB ) - added by DrewAPicture 12 years ago.
Accounts for 'default'
31271.4.diff​ (2.6 KB ) - added by DrewAPicture 12 years ago.
Tests for 'default'
31271.5.diff​ (2.7 KB ) - added by DrewAPicture 12 years ago.

Download all attachments as: .zip

Change History (17)

@tyxla
12 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
12 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
12 years ago

  • Milestone Awaiting Review → 4.2

#3 @helen
12 years ago

  • Focuses template added

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


12 years ago

#5 @DrewAPicture
12 years ago

  • Owner set to DrewAPicture
  • Status new → reviewing

#6 @DrewAPicture
12 years ago

  • Keywords commit added

31271.2.diff​ makes the docs adjustments.

@DrewAPicture
12 years ago

Accounts for 'default'

#7 @helen
12 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.


12 years ago

@DrewAPicture
12 years ago

Tests for 'default'

#9 @DrewAPicture
12 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.


12 years ago

#11 @helen
12 years ago

  • Resolution → fixed
  • Status reviewing → 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.