#18750 closed enhancement (fixed)
specify post ID for is_page_template
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 3.4 |
| Component: | General | Version: | |
| Severity: | normal | Keywords: | has-patch |
| Cc: | bill.erickson@…, chappellind@…, dromsey@… |
Description
is_page_template() checks the $wp_query object for the page ID. It would be great if we could override this with our own ID as a separate variable:
is_page_template( $template, $id );
Two instances where I've used this today:
- In a page template to see if the current page's parent is also using the template
- Limiting metaboxes to display only if a certain page template is used
Attachments (4)
Change History (28)
billerickson — 20 months ago
comment:1
billerickson — 20 months ago
- Cc bill.erickson@… added
- Keywords has-patch dev-feedback added
comment:3
follow-up:
↓ 4
billerickson — 20 months ago
Ah, I didn't know about get_page_template(). So you recommend creating a patch for that to accept an ID?
Replying to billerickson:
Ah, I didn't know about get_page_template(). So you recommend creating a patch for that to accept an ID?
Well I didn't think of it on purpose. I was actually giving an example name for a new function that would just call get_post_meta( $id, '_wp_page_template', true ); and didn't check if it already existed.
billerickson — 20 months ago
comment:5
billerickson — 20 months ago
New patch creates new function, get_the_page_template( $id ), which is simply a wrapper for get_post_meta.
get_page_template() returns the full path of the template file using get_query_template(), so it wasn't a good fit for this enhancement.
You re-patched and replied whilst I was writing this, but I'm going to put it in anyway since I wrote it. It looks like you got what I meant, but note that my example is much simpler as it isn't written to work in the loop without an ID.
---
Sorry, didn't really answer you :( Pretty much starting again...
From your two use case bullet points it sounds like you just want to do something like:
if ( get_post_meta( $current_id, '_wp_page_template', true ) == get_post_meta( $parent_id, '_wp_page_template', true ) )
// do some stuff as the templates are the same
and
if ( 'some_template' == get_post_meta( $id, '_wp_page_template', true ) )
// add metaboxes as the correct template is set
I then suggested the possibility of a new function which would just be a wrapper for the get_post_meta call so that you don't have to be specifying a core meta key. (Foolishly I managed to suggest a new function that already exists!) So you would end up taking the above examples and replacing get_post_meta( $id, '_wp_page_template', true ) with possible_new_function( $id ). Where:
function possible_new_function( $id ) {
return get_post_meta( $id, '_wp_page_template', true );
}
or something similar.
Why didn't I think it should be a mod to is_page_template()? Because that is specifically for determining "whether currently in a page template" with the ability to specify a certain template. It's not a more abstract function for querying a page's template.
Hope that makes sense :) P.S. All code is untested.
comment:7
follow-up:
↓ 10
billerickson — 20 months ago
Thanks for the detailed response and clarification. Initially I thought you were saying get_page_template() already is a wrapper function, but once I looked at it I realized what you meant.
I think the function is slightly more useful with it not requiring an ID, but let me know if you think it's better without and I'll update. Either way it will work for my use case.
My only worry is that it could have a better name since the two function names don't indicate clearly what the difference is.
The naming problem comes from the fact that we don't have a clear name for templates set via _wp_page_template.
The new function should be used in get_page_template() and is_page_template(), which currently uses some clunky code.
comment:9
Master Jake — 17 months ago
- Cc chappellind@… added
I second (or third or fourth) the need for this capability.
comment:10
in reply to:
↑ 7
mikeschinkel — 16 months ago
Replying to billerickson:
My only worry is that it could have a better name since the two function names don't indicate clearly what the difference is.
get_page_template_name()?
comment:11
scribu — 16 months ago
Here's another suggestion: get_custom_page_template()
To summarize, we went from the initial proposal:
is_page_template( $template, $id )
to
get_custom_page_template( $id ) == $template
comment:12
billerickson — 16 months ago
New patch:
- Renamed function to get_custom_page_template()
- Made a few minor modifications to it (duplicated how is_page_template() used the global $wp_query to get the ID)
- Used the function in is_page_template() and get_page_template()
billerickson — 16 months ago
comment:13
scribu — 16 months ago
This part seems superfluous, since get_post_meta() already returns false if there's no template to be found:
1280 if( !empty( $page_template ) ) 1281 return $page_template; 1282 else 1283 return false;
Also, coding standards: if ( instead of if(.
comment:14
scribu — 16 months ago
One more thing: get_queried_object_id() instead of $wp_query->get_queried_object_id().
billerickson — 16 months ago
comment:15
billerickson — 16 months ago
- Reduced the code to simply return get_post_meta
- Removed the $wp_query parts
- Updated if statement to use WP coding standards
comment:16
scribu — 16 months ago
- Keywords dev-feedback removed
- Milestone changed from Awaiting Review to 3.4
Looks good to me.
comment:17
goto10 — 16 months ago
- Cc dromsey@… added
comment:18
nacin — 15 months ago
- Owner set to nacin
- Status changed from new to accepted
Looks good to me as well. I'll get it in next week.
Let's keep thinking about the function name, though. Here's what's been suggested here:
- get_the_page_template()
- wp_get_page_template()
- get_custom_page_template()
- get_custom_page_template_name()
- get_page_template_for_post() (even though it is for pages)
- get_page_template_name()
- possible_new_function()
I like get_page_template_name(), myself. "Custom" doesn't have much of a place here.
comment:19
nacin — 15 months ago
Though in fairness, it's not the name we're getting back, it's the slug. The name is different.
comment:20
billerickson — 15 months ago
How about get_page_template_slug() ?
comment:21
nacin — 15 months ago
This function should not use get_queried_object_id() by default, as that is not typical of a post function. Rather, it should default to get_the_ID().
comment:22
nacin — 15 months ago
comment:23
nacin — 15 months ago
- Resolution set to fixed
- Status changed from accepted to closed
In [20075]:
comment:24
nacin — 15 months ago
Since is_page_template() returns false for the default page template, I wanted this function to return a falsey value. But I also understand there could be a reason to differentiate between not-a-page and default page template, hence false and '' for that edge case use.

I don't think that this would be the best way of doing this. It seems that all you really require is a wrapper around:
A wrapper function, e.g. get_page_template( $id ), so that you don't have to rely on specifying a private meta key (_wp_page_template).