• I try to get current queried_object ID under customize_register action.

    
    // call action customize register
    add_action('customize_register','register_customize_options');
    
    //customize register callback function 
    function register_customize_options( $wp_customize ){
    
    //if is page customize preview
    if( is_page() ):  //not work
    
       $ObjID = get_the_ID(); //not work
    
    //if is any taxonomy customize preview
    elseif ( is_tax() ) : //not work
    
       $ObjID =  get_queried_object()->term_id; //not work
    
    endif;
    
    // obj $wp_customize;
    // var_dump( $wp_customize ); //not found any ID 
    
    //rest of code... 
    
    }

    Also I try get page ID by url_to_postid( $_GET[‘url’] ), that is work just if user open customize on the page.

    Any suggestion how to get dynamically Object ID under customize_register action?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not so sure any ID is available during this action. Object queries work differently in the Customizer and such information is typically only available during the active callback. If anything at all is available, you may be able to get it with get_queried_object_id(). Don’t be surprised if even that does not work. If it doesn’t, the ID is simply not available.

    What are you trying to do in customize_register where you need the queried object ID? I’m inclined to think you are going about it in the wrong way. You likely need a different action, if not a different approach altogether.

    Thread Starter Goran Petrovic

    (@goran321)

    I try to create page options in customize by Page template. So if I have 2 pages whit same page template I need different options name in customize. I need to set page_34_background, page_40_background… etc…

    Thread Starter Goran Petrovic

    (@goran321)

    Also I need to save options from customize in post meta.

    I can filter on save_customize get the filed and update post meta. But do you have some other suggestion?

    Moderator bcworkz

    (@bcworkz)

    The current page’s query object should be available within a Customizer object’s active & sanitize callbacks through get_queried_object(). Through that you can get everything you need. This means the Customizer labels and values have already been set though. You can only manage whether the Customizer object is visible or not and if the new value is valid or not. The labels need to be generic enough for any particular page. The value is stored in options in the same place regardless of query.

    The Customizer isn’t set up to manage settings the way you want. What you’re expected to do is have a separate setting for each template and manage which is visible with the active callback. Each template would get it’s specific setting through get_option() or get_theme_mod(), all saved in options, not post meta.

    You could also store any new value anywhere else you want during the sanitize callback and the template could use that value instead of the one returned by get_theme_mod(). The value saved in options would reflect the last value saved, whether it relates to the current template or not. You could work around this by using JavaScript to set the applicable value regardless of what was output from the value in options. Any labels could be customized this way as well.

    This begs the question how does JavaScript know what the page ID is and what values to set? I’m not sure. Your best be would be to register/enqueue the JavaScript that does this substitution. Use the handle thus established to pass values with wp_localize_script() from within the setting’s active callback. I’m not sure if the active callback fires early enough for this to work. Or you could try directly outputting the JS value assignment within script tags from active callback. I’ve no idea if that would work or where the output ends up, if anywhere. Something to try anyway?

    Alternately, utilize selective refresh to correct the display. You’re supposed to be updating part of the preview this way, but you can update any part of the DOM. Getting the query data is still the challenge when relying on active callback to set the needed values. But JavaScript has access to the DOM and the preview should have the page ID available as an attribute somewhere. If what you need is not available in the DOM, you could probably get it through Ajax requests.

    If all else fails, fall back to multiple settings and managing visibility. Good luck!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to get post, page or taxonomy ID under add_action(‘customize_register’, …)’ is closed to new replies.