【问题标题】:Use "do_shortcode" in a WordPress plugin is not working在 WordPress 插件中使用“do_shortcode”不起作用
【发布时间】:2013-05-25 18:51:55
【问题描述】:

我的 WordPress 上安装了这个插件: http://wordpress.org/plugins/put/

我正在尝试制作一个在我自己的插件中使用 UI Tabs 插件的插件。

到目前为止我的插件代码:

function load_jquery(){
    echo '<link rel=\'stylesheet\' id=\'jquery-ui-tabs-css\'  href=\'http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/smoothness/jquery-ui.css?ver=1.9.2\' type=\'text/css\' media=\'all\' />';
}

add_action('wp_head','load_jquery');

function print_tabs(){
    echo do_shortcode('[tab name="Tab"]-[/tab]');
    echo do_shortcode('[end_tabset]');
}

add_shortcode('print_tabs', 'print_tabs');

现在,如果我在新页面中使用 [print_tabs] 短代码,它应该如下所示: http://img835.imageshack.us/img835/4905/workingp.png

但它不起作用,它看起来像这样: http://imageshack.us/a/img62/9772/notworkingm.png

这可能是什么问题?

【问题讨论】:

  • 短代码应该return他们的数据,而不是echo它。
  • $val = do_shortcode('[tab name="Tab"]-[/tab]')。 do_shortcode('[end_tabset]');返回 $val;没有任何区别:/
  • 我刚刚通过谷歌找到了这个。我可以确认 PUT 插件没有正确运行 do_shortcode()。

标签: php wordpress shortcode


【解决方案1】:

从我在 Post UI Tabs 插件的 put.php 中看到的问题是,短代码仅在名为“on_the_content”的函数中的“the_content”过滤器中添加。

add_filter( 'the_content',        array( $this, 'on_the_content' ), 7 ); // Priority 7 - before wpautop

(put.php 第 96 行)

那个函数看起来像:

    public function on_the_content( $content ) {

    $this->has_tabs = (bool) apply_filters( 'put_decide_has_tabs', $this->has_tabs );

    if( !$this->has_tabs )
        return $content;

    global $shortcode_tags;

    // Backup current registered shortcodes and clear them all out
    $orig_shortcode_tags = $shortcode_tags;
    $shortcode_tags = array();

    add_shortcode( 'tab',        array( $this, 'on_tab_shortcode' ) );
    add_shortcode( 'end_tabset', array( $this, 'on_tab_end_shortcode' ) );

    // Do the shortcode(only the tab shortcode is registered at this point)
    $content = do_shortcode( $content );

    // Put the original shortcodes back
    $shortcode_tags = $orig_shortcode_tags;

    return $content;
}

(从 put.php 的第 118 行开始)

因此,鉴于此插件是如何通过使用过滤器修改内容来编写的,该过滤器又会在运行该过滤器时添加简码,您所看到的可能正在发生,因为当您调用“do_shortcode”时,这些简码不会实际存在。

那么,回显 do_shortcode 所做的只是把文本咳出来。

很遗憾,由于 Post UI Tabs 插件的编写方式,您可能无法执行您想要执行的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多