In the WordPress customizer script, I am trying to pass the value from a select control into a custom control that displays taxonomies associated with the selected value (1st control).
$wp_customize->add_control( new Tax_Dropdown_Control( $wp_customize, 'select_tax', array(
'section' => 'section_1',
'label' => __( 'Select Post Taxonomy', 'textdomain' ),
'description' => __( 'Select a taxonomy based on post type selected.', 'textdomain' ),
'dropdown_args' => array(
'post_type' => $wp_customize->get_setting("select_post_type"), // here I need to pass the first setting's value
),
) ) );
// custom controls related snippet
class Tax_Dropdown_Control extends WP_Customize_Control {
.....
$dropdown_args = wp_parse_args( $this->dropdown_args, array(
'post_type' => 'post',
) );
$dropdown_args['echo'] = false;
$taxonomies = get_object_taxonomies($dropdown_args);
if ($taxonomies) {
echo '<select>';
foreach ($taxonomies as $taxonomy ) {
echo '<option>'. $taxonomy. '</option>';
}
echo '</select>';
}
....
}
It would need to update the choices live when the select post type is changed. I'm not sure if maybe an active_callback
can be used to recall the function with the updated variable?
In the WordPress customizer script, I am trying to pass the value from a select control into a custom control that displays taxonomies associated with the selected value (1st control).
$wp_customize->add_control( new Tax_Dropdown_Control( $wp_customize, 'select_tax', array(
'section' => 'section_1',
'label' => __( 'Select Post Taxonomy', 'textdomain' ),
'description' => __( 'Select a taxonomy based on post type selected.', 'textdomain' ),
'dropdown_args' => array(
'post_type' => $wp_customize->get_setting("select_post_type"), // here I need to pass the first setting's value
),
) ) );
// custom controls related snippet
class Tax_Dropdown_Control extends WP_Customize_Control {
.....
$dropdown_args = wp_parse_args( $this->dropdown_args, array(
'post_type' => 'post',
) );
$dropdown_args['echo'] = false;
$taxonomies = get_object_taxonomies($dropdown_args);
if ($taxonomies) {
echo '<select>';
foreach ($taxonomies as $taxonomy ) {
echo '<option>'. $taxonomy. '</option>';
}
echo '</select>';
}
....
}
It would need to update the choices live when the select post type is changed. I'm not sure if maybe an active_callback
can be used to recall the function with the updated variable?
2 Answers
Reset to default 0You could probably use some JavaScript to capture the selected option and then make an Ajax call to the REST API to fetch the corresponding posts and update the second field.
The customizer does everything client side, so retrieving a new taxonomy list would involve an ajax call to the rest api. That's doable, but pretty complicated. An easier, but uglier solution would be to simply include all dropdowns and toggle their display. So you would have this inside a loop through all thinkable dropdowns:
foreach [ thinkable dropdown ] {
....
if ($taxonomies) {
echo '<select id="' . $taxonomies . '">';
foreach ($taxonomies as $taxonomy ) {
echo '<option>'. $taxonomy. '</option>';
}
echo '</select>';
}
}
This will lead to html
like this:
<select id="tax1">
<option>...</option>
<option>...</option>
</select>
...
<select id="tax5">
<option>...</option>
<option>...</option>
</select>
Next, you can use the id
on select
to apply css to toggle the display. Obviously you don't want this ugly html
on your live site, so you'll have to use is_customize_preview
to make sure this is only generated when the customizer is active.
select_post_type
control? What's the code? And thoseecho
($taxonomies
), what control/section are they in/for? – Sally CJ Commented Aug 25, 2020 at 2:20select_post_type
is a select control. The $taxonomies is the code for theselect_tax
custom control class in the render content function – 730wavy Commented Aug 25, 2020 at 4:53