Timeline for Can I change a control's transport in Customizer depending on the previewed page?
Current License: CC BY-SA 3.0
12 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Feb 11, 2018 at 19:58 | comment | added | Weston Ruter |
It would be a bit more elegant to actually register a JS subclass of Control which would define the onActiveChange from the start. Then there wouldn't be a case where it could get initially hidden. But this would be more code and it may not be as feasible if you are using a custom control already.
|
|
Feb 11, 2018 at 19:50 | comment | added | Weston Ruter |
Without is_admin() then it was checking is_archive() which is false since it is in the admin.
|
|
Feb 11, 2018 at 19:46 | comment | added | Joy Reynolds |
Edit: Oh, I see that is_admin would only be true during the run called by customize.php . That still leaves me wondering why the control isn't hidden, since that's what was happening before I added is_admin() (the default behavior was still there).
|
|
Feb 11, 2018 at 19:24 | comment | added | Joy Reynolds |
But if is_admin() will always be true, then how can the active callback ever return false? How could it ever change?
|
|
Feb 11, 2018 at 19:21 | comment | added | Weston Ruter |
The is_admin() check is only there to set the state while waiting for the preview to load for the first time. It is true because customize.php also runs all of the active_callback functions when it initializes.
|
|
Feb 11, 2018 at 19:19 | vote | accept | Joy Reynolds | ||
Feb 11, 2018 at 19:19 | comment | added | Joy Reynolds |
That one change made it work on both back end and front end! I don't understand why is_admin() is true on the front end, though.
|
|
Feb 11, 2018 at 17:54 | comment | added | Weston Ruter |
It's true that this answer depends on the initial state being one where the state is active . So that is why the is_admin check has to be there, so that when it runs on customize.php is is initially true .
|
|
Feb 11, 2018 at 17:52 | comment | added | Weston Ruter |
@user43242 I forgot to include is_admin() in the condition. I just updated the answer. The active_callback gets called each time the preview refreshes or you navigate to another page in the preview.
|
|
Feb 11, 2018 at 17:50 | history | edited | Weston Ruter | CC BY-SA 3.0 |
added 14 characters in body
|
Feb 11, 2018 at 17:35 | comment | added | Joy Reynolds |
This solution implies that 1. The PHP active callback is run once, (on preview page load ?) 2. The JS onChangeActive function is run when the active value changes, and this code replaces the function that is normally run. I tested this (although using return is_archive() || is_search() || is_home() ), and it does not override the default behavior. My control is not shown on any page, even if I start the Customizer from the front end on a post page.
|
|
Feb 10, 2018 at 5:56 | history | answered | Weston Ruter | CC BY-SA 3.0 |