How do I get current page ID in Wordpress customizer file?












0














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!










share|improve this question






















  • 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
















0














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!










share|improve this question






















  • 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














0












0








0







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!










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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


















  • 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
















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












1 Answer
1






active

oldest

votes


















0














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;
}
}





share|improve this answer





















  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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;
}
}





share|improve this answer





















  • 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
















0














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;
}
}





share|improve this answer





















  • 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














0












0








0






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;
}
}





share|improve this answer












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;
}
}






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'