WordPress dynamic menu with TinyMce

I came across a problem with TinyMce in WordPress. I’ve got it set up in my theme, and it works just fine when it’s hardcoded. I bump into a wall when trying to create a dynamic one.

In short, I need the content of a TinyMce instance (button > dropdown or array of checkbox instances) to be fetched from Custom Post Type pages or categories, whichever. Basically, I want the user to be able to add content from CPT or category in another page, choosing which CPT or which category they want from list.

Read More

I changed the javascript file to .js.php and added some php, which doesn’t work when I try to execute a query in there.

I went through endless google search, and couldn’t find anything useful. Any clue how to do this properly?

<!-- generate the menu in tinymce dynamically by pulling the case studies or case study categories -->
<!-- insert selected values as arguments to the shortcode -->
<!-- use the plugins > casestudy_pages to generate the content -->

<?php


$someValue = 'some value '; // this works when placed in the javascript below
//list terms in a given taxonomy (useful as a widget for twentyten)
$customPostTaxonomies = get_object_taxonomies('case_studies'); // creashes TinyMce

if(count($customPostTaxonomies) > 0)  
{
     foreach($customPostTaxonomies as $tax)
 {
     $args = array(
          'orderby' => 'name',
          'show_count' => 0,
          'pad_counts' => 0,
          'hierarchical' => 1,
          'taxonomy' => $tax,
          'title_li' => ''
        );
     $someValue = wp_list_categories( $args ); // creashes TinyMce
 }
}
?>


(function() {
tinymce.PluginManager.add('my_mce_button', function( editor, url ) {
    editor.addButton( 'my_mce_button', {
        text: 'Intelligent Office Shortcodes',
        icon: false,
        type: 'menubutton',
        menu: [
            {
                text: 'Add Children Pages Menu',
                        onclick: function() {

                            editor.insertContent( '[ChildrenMenu]');

                        }
            },
            {
                text: 'Add Children Pages Menu',
                        onclick: function() {

                            editor.insertContent( '[<?php echo $someValue; ?>]');

                        }
            },
        ]
    });
});
})();

Related posts

Leave a Reply