How do I get current page ID in Wordpress customizer file?
I'm trying to hide or show customizer settings based on what page I am viewing similar to active_callback' => 'is_front_page'
, however, I haven't found a way how to access the current page ID from my customizer.php
file. I want to be able to use active_callback' => 'is_specific_page'
through a custom callback based on current page ID like so:
function is_specific_page() {
// LOGIC RETURNS TRUE OR FALSE DEPENDING ON CURRENT PAGE
}
I've tried using all of the following to no avail:
global $post; $post->ID();
global $wp_query; $post_id = $wp_query->post->ID;
get_the_ID();
Thank you in advanced for your help!
php wordpress
|
show 1 more comment
I'm trying to hide or show customizer settings based on what page I am viewing similar to active_callback' => 'is_front_page'
, however, I haven't found a way how to access the current page ID from my customizer.php
file. I want to be able to use active_callback' => 'is_specific_page'
through a custom callback based on current page ID like so:
function is_specific_page() {
// LOGIC RETURNS TRUE OR FALSE DEPENDING ON CURRENT PAGE
}
I've tried using all of the following to no avail:
global $post; $post->ID();
global $wp_query; $post_id = $wp_query->post->ID;
get_the_ID();
Thank you in advanced for your help!
php wordpress
Can you try, global $post; $post->ID; (without the brakets)
– zipkundan
Nov 21 '18 at 20:58
Doesn't seem to work.$post
variable seems to be empty.
– Alex E.
Nov 21 '18 at 21:49
Give it a try using get_queried_object_id() instead.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
Didn't work. Hmm... I'm new to PHP so if there was a way I could log Wordpress' PHP objects to see what's happening inside, similar toconsole.log(obj)
that would help me a lot. Thanks.
– Alex E.
Nov 23 '18 at 3:41
Ahh I figured out how to do that withjson_encode()
:)
– Alex E.
Nov 23 '18 at 3:48
|
show 1 more comment
I'm trying to hide or show customizer settings based on what page I am viewing similar to active_callback' => 'is_front_page'
, however, I haven't found a way how to access the current page ID from my customizer.php
file. I want to be able to use active_callback' => 'is_specific_page'
through a custom callback based on current page ID like so:
function is_specific_page() {
// LOGIC RETURNS TRUE OR FALSE DEPENDING ON CURRENT PAGE
}
I've tried using all of the following to no avail:
global $post; $post->ID();
global $wp_query; $post_id = $wp_query->post->ID;
get_the_ID();
Thank you in advanced for your help!
php wordpress
I'm trying to hide or show customizer settings based on what page I am viewing similar to active_callback' => 'is_front_page'
, however, I haven't found a way how to access the current page ID from my customizer.php
file. I want to be able to use active_callback' => 'is_specific_page'
through a custom callback based on current page ID like so:
function is_specific_page() {
// LOGIC RETURNS TRUE OR FALSE DEPENDING ON CURRENT PAGE
}
I've tried using all of the following to no avail:
global $post; $post->ID();
global $wp_query; $post_id = $wp_query->post->ID;
get_the_ID();
Thank you in advanced for your help!
php wordpress
php wordpress
asked Nov 21 '18 at 17:34
Alex E.
1
1
Can you try, global $post; $post->ID; (without the brakets)
– zipkundan
Nov 21 '18 at 20:58
Doesn't seem to work.$post
variable seems to be empty.
– Alex E.
Nov 21 '18 at 21:49
Give it a try using get_queried_object_id() instead.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
Didn't work. Hmm... I'm new to PHP so if there was a way I could log Wordpress' PHP objects to see what's happening inside, similar toconsole.log(obj)
that would help me a lot. Thanks.
– Alex E.
Nov 23 '18 at 3:41
Ahh I figured out how to do that withjson_encode()
:)
– Alex E.
Nov 23 '18 at 3:48
|
show 1 more comment
Can you try, global $post; $post->ID; (without the brakets)
– zipkundan
Nov 21 '18 at 20:58
Doesn't seem to work.$post
variable seems to be empty.
– Alex E.
Nov 21 '18 at 21:49
Give it a try using get_queried_object_id() instead.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
Didn't work. Hmm... I'm new to PHP so if there was a way I could log Wordpress' PHP objects to see what's happening inside, similar toconsole.log(obj)
that would help me a lot. Thanks.
– Alex E.
Nov 23 '18 at 3:41
Ahh I figured out how to do that withjson_encode()
:)
– Alex E.
Nov 23 '18 at 3:48
Can you try, global $post; $post->ID; (without the brakets)
– zipkundan
Nov 21 '18 at 20:58
Can you try, global $post; $post->ID; (without the brakets)
– zipkundan
Nov 21 '18 at 20:58
Doesn't seem to work.
$post
variable seems to be empty.– Alex E.
Nov 21 '18 at 21:49
Doesn't seem to work.
$post
variable seems to be empty.– Alex E.
Nov 21 '18 at 21:49
Give it a try using get_queried_object_id() instead.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
Give it a try using get_queried_object_id() instead.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
Didn't work. Hmm... I'm new to PHP so if there was a way I could log Wordpress' PHP objects to see what's happening inside, similar to
console.log(obj)
that would help me a lot. Thanks.– Alex E.
Nov 23 '18 at 3:41
Didn't work. Hmm... I'm new to PHP so if there was a way I could log Wordpress' PHP objects to see what's happening inside, similar to
console.log(obj)
that would help me a lot. Thanks.– Alex E.
Nov 23 '18 at 3:41
Ahh I figured out how to do that with
json_encode()
:)– Alex E.
Nov 23 '18 at 3:48
Ahh I figured out how to do that with
json_encode()
:)– Alex E.
Nov 23 '18 at 3:48
|
show 1 more comment
1 Answer
1
active
oldest
votes
Hello Alex welcome to stackoverflow.
Here is how you could compare the current page path to a specific path and find a match
function is_specific_pages(){
$current_path = $_SERVER['REQUEST_URI'];//returns page path with leading slash
//check if page path is one of the specific pages
if( $current_path == '/page-1'){
return true;
}elseif( $current_path == '/page-2'){
return true;
}else{
return false;
}
}
Hey, thank you for this! The reason why I wanted to use IDs is in the case of changed URIs which would happen automatically if the page's title changes.
– Alex E.
Nov 21 '18 at 21:46
No problem. Its possible to change the page title without updating the path (slug). so you can keep the URI he same and adjust the title as needed, if nothing else works this can be a fallback.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
If this problem lingers I'll have to use your solution. Thank you so much!
– Alex E.
Nov 23 '18 at 3:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417700%2fhow-do-i-get-current-page-id-in-wordpress-customizer-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Hello Alex welcome to stackoverflow.
Here is how you could compare the current page path to a specific path and find a match
function is_specific_pages(){
$current_path = $_SERVER['REQUEST_URI'];//returns page path with leading slash
//check if page path is one of the specific pages
if( $current_path == '/page-1'){
return true;
}elseif( $current_path == '/page-2'){
return true;
}else{
return false;
}
}
Hey, thank you for this! The reason why I wanted to use IDs is in the case of changed URIs which would happen automatically if the page's title changes.
– Alex E.
Nov 21 '18 at 21:46
No problem. Its possible to change the page title without updating the path (slug). so you can keep the URI he same and adjust the title as needed, if nothing else works this can be a fallback.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
If this problem lingers I'll have to use your solution. Thank you so much!
– Alex E.
Nov 23 '18 at 3:49
add a comment |
Hello Alex welcome to stackoverflow.
Here is how you could compare the current page path to a specific path and find a match
function is_specific_pages(){
$current_path = $_SERVER['REQUEST_URI'];//returns page path with leading slash
//check if page path is one of the specific pages
if( $current_path == '/page-1'){
return true;
}elseif( $current_path == '/page-2'){
return true;
}else{
return false;
}
}
Hey, thank you for this! The reason why I wanted to use IDs is in the case of changed URIs which would happen automatically if the page's title changes.
– Alex E.
Nov 21 '18 at 21:46
No problem. Its possible to change the page title without updating the path (slug). so you can keep the URI he same and adjust the title as needed, if nothing else works this can be a fallback.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
If this problem lingers I'll have to use your solution. Thank you so much!
– Alex E.
Nov 23 '18 at 3:49
add a comment |
Hello Alex welcome to stackoverflow.
Here is how you could compare the current page path to a specific path and find a match
function is_specific_pages(){
$current_path = $_SERVER['REQUEST_URI'];//returns page path with leading slash
//check if page path is one of the specific pages
if( $current_path == '/page-1'){
return true;
}elseif( $current_path == '/page-2'){
return true;
}else{
return false;
}
}
Hello Alex welcome to stackoverflow.
Here is how you could compare the current page path to a specific path and find a match
function is_specific_pages(){
$current_path = $_SERVER['REQUEST_URI'];//returns page path with leading slash
//check if page path is one of the specific pages
if( $current_path == '/page-1'){
return true;
}elseif( $current_path == '/page-2'){
return true;
}else{
return false;
}
}
answered Nov 21 '18 at 20:59
Hans-Eric Lippke
7917
7917
Hey, thank you for this! The reason why I wanted to use IDs is in the case of changed URIs which would happen automatically if the page's title changes.
– Alex E.
Nov 21 '18 at 21:46
No problem. Its possible to change the page title without updating the path (slug). so you can keep the URI he same and adjust the title as needed, if nothing else works this can be a fallback.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
If this problem lingers I'll have to use your solution. Thank you so much!
– Alex E.
Nov 23 '18 at 3:49
add a comment |
Hey, thank you for this! The reason why I wanted to use IDs is in the case of changed URIs which would happen automatically if the page's title changes.
– Alex E.
Nov 21 '18 at 21:46
No problem. Its possible to change the page title without updating the path (slug). so you can keep the URI he same and adjust the title as needed, if nothing else works this can be a fallback.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
If this problem lingers I'll have to use your solution. Thank you so much!
– Alex E.
Nov 23 '18 at 3:49
Hey, thank you for this! The reason why I wanted to use IDs is in the case of changed URIs which would happen automatically if the page's title changes.
– Alex E.
Nov 21 '18 at 21:46
Hey, thank you for this! The reason why I wanted to use IDs is in the case of changed URIs which would happen automatically if the page's title changes.
– Alex E.
Nov 21 '18 at 21:46
No problem. Its possible to change the page title without updating the path (slug). so you can keep the URI he same and adjust the title as needed, if nothing else works this can be a fallback.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
No problem. Its possible to change the page title without updating the path (slug). so you can keep the URI he same and adjust the title as needed, if nothing else works this can be a fallback.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
If this problem lingers I'll have to use your solution. Thank you so much!
– Alex E.
Nov 23 '18 at 3:49
If this problem lingers I'll have to use your solution. Thank you so much!
– Alex E.
Nov 23 '18 at 3:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417700%2fhow-do-i-get-current-page-id-in-wordpress-customizer-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you try, global $post; $post->ID; (without the brakets)
– zipkundan
Nov 21 '18 at 20:58
Doesn't seem to work.
$post
variable seems to be empty.– Alex E.
Nov 21 '18 at 21:49
Give it a try using get_queried_object_id() instead.
– Hans-Eric Lippke
Nov 22 '18 at 15:05
Didn't work. Hmm... I'm new to PHP so if there was a way I could log Wordpress' PHP objects to see what's happening inside, similar to
console.log(obj)
that would help me a lot. Thanks.– Alex E.
Nov 23 '18 at 3:41
Ahh I figured out how to do that with
json_encode()
:)– Alex E.
Nov 23 '18 at 3:48