doubi2228 2016-02-05 03:23
浏览 178
已采纳

将简短代码添加到TinyMCE以显示菜单下拉列表

I have a shortcode for show menu:

//menu categories shortcode
function menu_categories_shortcode( $atts, $content = null ){
    extract( shortcode_atts( array(
        'menu' => ''
      ), $atts ) );
    ob_start();
        wp_nav_menu(array(
            'menu' => $menu,
            'container'       => 'ul',
            'container_class' => 'sidebar-categories',
            'items_wrap'      => '%3$s',
            'depth' => 1
        ));
    $content = ob_get_contents();
    ob_end_clean();
return $content;
}
add_shortcode('menu-categories', 'menu_categories_shortcode');

Now, i wanna add shortcode button of menu shortcode to TinyMCE for client can choose menu they want to show.

Ex: On Home page, client want to show menu with menu name is Menu 1, so shortcode will be: [menu-categories menu="Menu 1"] When client click menu shortcode button, a dropdown of all menu will be popup for client can choose menu they want.

Add shortcode button

This is my js to call menu popup:

(function() {
    tinymce.create('tinymce.plugins.menuPlugin', {
        init: function(ed, url) {
            // Register commands
            ed.addCommand('mcebutton', function() {
                ed.windowManager.open({
                    file: url + '/menu_popup.php',
                    width: 220 + parseInt(ed.getLang('button.delta_width', 0)),
                    height: 240 + parseInt(ed.getLang('button.delta_height', 0)),
                    inline: 1
                }, {
                    plugin_url: url
                });
            });
            // Register buttons
            ed.addButton('menu_button', {
                title: 'Choose Menu',
                cmd: 'mcebutton',
                image: url + '/icon.gif'
            });
        },
    });
    tinymce.PluginManager.add('menu_button', tinymce.plugins.buttonPlugin);
})();

The problem I've in menu_popup.php, how to i get all menus? My menu_popup.php file:

<form action="/" method="get" accept-charset="utf-8">
    <div>
        <label for="button-url">Choose Menu</label>
        <?php $menus = get_registered_nav_menus();
                    if($menus) : ?>
        <select id="button-url">
            <?php 
                 foreach ($menus as $key => $menu) : ?>
                <option value="<?php echo $menu; ?>"><?php echo $menu; ?></option>          <?php endforeach; ?>
        </select>
        <?php endif; ?>
    </div>
    <div>
        <a href="javascript:ButtonDialog.insert(ButtonDialog.local_ed)" id="insert" style="display: block; line-height: 24px;">Insert</a>
    </div>
</form>

Error i have:

Error

Thanks so much!!!

  • 写回答

1条回答 默认 最新

  • douguai6716 2016-07-22 17:14
    关注

    In your menu_poup.php you need to include this code:

    define('WP_USE_THEMES', false);
    require('../../../wp-blog-header.php');
    

    https://codex.wordpress.org/Integrating_WordPress_with_Your_Website

    to access WordPress functions. I just tried it using your code and it worked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)