0

I'm trying to move to using the WordPress customizer more. I need to find a way to get the ID of the page the user is viewing in the window. I was wondering is this is possible in the customizer?

Another question is, is it possible to use a loop to create a setting for each individual page?

I'm trying to let the user upload an image to the customizer to use as a banner. So I want to be able to get the page id to uniquely create a setting for each page then in my theme grab the image if it exists. I know the customizer has gotten more advanced but I've done a bit of digging and cant find anything.

1

I think you work about the ajax call. Inside the theme, in the right frame works all default WP functions inside the loop. But if you get the post inside the customizer you can use the ajax call and get the id from the URL.

Source Example

JavaScript

add_action( 'wp_ajax_fb_custom', 'fb_customizer_ajax' );
function fb_customizer_ajax() {

    if ( ! wp_verify_nonce( $_POST[ 'ajaxnonce' ], 'fb_customize_ajax_nonce' ) ) {
        die( -1 );
    }

    $current_url = $_POST[ 'current_url' ];
    $current_id  = url_to_postid( $current_url );
    echo $current_id . '<br>' . $current_url;
    die();
}

PHP

The same should be possible in php. Use the help of the function url_to_postid(), this return value is the ID of the post for a url. The url is in the global _GET, like $_GET['url'].

  • The $_GET['url'] only seems to display the home page link and doesn't change when navigating to other pages, I wanted the user to be able to set banners dependent on the page they're on. I also tried using the $_POST['current_url'] and that displayed nothing at all. Was the top part meant to go into functions.php? Bit confused since you labeled it JavaScript but used "echo". Thanks for the help! – Chess Byte Jun 26 '16 at 9:20
  • The echo inside the example is only a source line to demonstrate the the usage. Also you should debug, a var_dump() about the _GET and _POST global to find the right parameter. The _post var is only available after send post data, like after update a settings. – bueltge Jun 26 '16 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.