0

Is it possible to create a TinyMCE dropdown menu with dynamically generated shortcodes?

This is my code for shortcodes:

function filter_button_group($atts){
$attributes = shortcode_atts(
    array(
        'id' => '',
        'parent' => '',
        'level' => null
    ),
    $atts
);

$terms = get_terms( array(
    'taxonomy' => 'filter_options',
    'hide_empty' => false,
    'parent' => $atts['id']
) );

$output = '';
foreach($terms as $term){
    if ($term->parent != 0) {
        if ($term->parent == $atts['id']) {
            $output .='<button class="btn btn-filter" data-first-click="false" data-level="'. $atts['level'] .'" data-parent-id="' . $term->parent . '" data-id="' . $term->term_id . '">' . $term->name . '</button>';
        }
    }
}

return $output;
}
add_shortcode('filter-btn', 'filter_button_group');

And with this code, I add shortcode code into a code field in taxonomy edit page.

function action_edit_taxonomy( $tag, $taxonomy ) {

if ($tag -> parent == '0') :

?>
<tr class="form-field term-shortcode-wrap">
    <th scope="row">
        <label><?php echo esc_html__( 'Shortcode', 'moam' ); ?></label>
    </th>
    <td>
        <code><?php echo '[filter-btn id="'. $tag->term_id .'"]' ?></code>
    </td>
</tr>
<?php endif; }

add_action( "filter_options_edit_form_fields", 'action_edit_taxonomy', 10, 2 );

But I would like to add these shortcodes into TinyMCE as a dropdown menu dynamically. I mean; each time I create a new taxonomy term in 'filter_options', I would like its shortcode to be added into a dropdown menu in TinyMCE.

Any help would be appreciated. Thank you!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.