admin管理员组文章数量:1130349
I have a customizer RadioControl with 3 choices: menu-left, menu-right, menu-top. I want the chosen setting to be added to the body as a class, while the others are removed.
Example
<body class="class1 menu-left"> will become <body class="class1 menu-right">
In order to do that, I want to read the choices dynamically in preview. Yet, the wp.customize.control does not work in a script enqueued in customize_preview_init-action.
I would love to:
const choices = Object.keys(wp.customize( controlKey ).params.choices).join(" ");
How can I read a controls available choices in preview?
I have a customizer RadioControl with 3 choices: menu-left, menu-right, menu-top. I want the chosen setting to be added to the body as a class, while the others are removed.
Example
<body class="class1 menu-left"> will become <body class="class1 menu-right">
In order to do that, I want to read the choices dynamically in preview. Yet, the wp.customize.control does not work in a script enqueued in customize_preview_init-action.
I would love to:
const choices = Object.keys(wp.customize( controlKey ).params.choices).join(" ");
How can I read a controls available choices in preview?
Share Improve this question asked Nov 8, 2018 at 1:15 user9user9 1644 silver badges17 bronze badges1 Answer
Reset to default 0Because this is for a class, and you need to remove the old value and add the new value, you need to send that information to the preview. I used a bunch of checkboxes, but it would be the same problem for radio inputs or a select. Knowing just the new value isn't enough.
I wrote javascript for my control for the control pane that initialized the control with a field that contained all the possible values (sent as json in PHP). Then each time the user chooses, the value of the setting is updated to contain the off values, a separator, and the on values. Since I was doing this for classes also, I just used a comma.
Then in the preview script, the single value is split on the separator and the off ones are used with removeClass() and the on ones are used with addClass().
You can see the full code here: https://wordpress/themes/twenty8teen
I have a customizer RadioControl with 3 choices: menu-left, menu-right, menu-top. I want the chosen setting to be added to the body as a class, while the others are removed.
Example
<body class="class1 menu-left"> will become <body class="class1 menu-right">
In order to do that, I want to read the choices dynamically in preview. Yet, the wp.customize.control does not work in a script enqueued in customize_preview_init-action.
I would love to:
const choices = Object.keys(wp.customize( controlKey ).params.choices).join(" ");
How can I read a controls available choices in preview?
I have a customizer RadioControl with 3 choices: menu-left, menu-right, menu-top. I want the chosen setting to be added to the body as a class, while the others are removed.
Example
<body class="class1 menu-left"> will become <body class="class1 menu-right">
In order to do that, I want to read the choices dynamically in preview. Yet, the wp.customize.control does not work in a script enqueued in customize_preview_init-action.
I would love to:
const choices = Object.keys(wp.customize( controlKey ).params.choices).join(" ");
How can I read a controls available choices in preview?
Share Improve this question asked Nov 8, 2018 at 1:15 user9user9 1644 silver badges17 bronze badges1 Answer
Reset to default 0Because this is for a class, and you need to remove the old value and add the new value, you need to send that information to the preview. I used a bunch of checkboxes, but it would be the same problem for radio inputs or a select. Knowing just the new value isn't enough.
I wrote javascript for my control for the control pane that initialized the control with a field that contained all the possible values (sent as json in PHP). Then each time the user chooses, the value of the setting is updated to contain the off values, a separator, and the on values. Since I was doing this for classes also, I just used a comma.
Then in the preview script, the single value is split on the separator and the off ones are used with removeClass() and the on ones are used with addClass().
You can see the full code here: https://wordpress/themes/twenty8teen
本文标签: Customizer Get Control Choices on Change
版权声明:本文标题:Customizer Get Control Choices on Change 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749053105a2308891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论