简体   繁体   English

Wordpress:在插件中使用do_shortcode不起作用

[英]Wordpress: Use do_shortcode in plugin not working

I have this plugin installed on my wordpress: http://wordpress.org/plugins/put/ 我在我的wordpress上安装了此插件: http : //wordpress.org/plugins/put/

Im trying to make plugin that uses UI Tabs plugin inside my own plugin. 我正在尝试制作在我自己的插件中使用UI Tabs插件的插件。

My plugin code so far: 到目前为止,我的插件代码:

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');

Now if I use [print_tabs] shortcode in new page, it should look like this: http://img835.imageshack.us/img835/4905/workingp.png 现在,如果我在新页面中使用[print_tabs]简码,它应该看起来像这样: http : //img835.imageshack.us/img835/4905/workingp.png

But its not working, it looks like this: http://imageshack.us/a/img62/9772/notworkingm.png 但是它不起作用,看起来像这样: http : //imageshack.us/a/img62/9772/notworkingm.png

What could be the problem here? 这可能是什么问题?

The problem, from what I can see in put.php in the Post UI Tabs plugin is that the shortcodes are only added during the "the_content" filter in a function called "on_the_content". 从我在Post UI Tabs插件的put.php中看到的问题是,仅在“ the_content”过滤器中的“ on_the_content”函数中添加了短代码。

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

(line 96 of put.php) (put.php的第96行)

And that function looks like: 该函数如下所示:

    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;
}

(starting at line 118 of put.php) (从put.php的第118行开始)

So, given how this plugin is written by modifying the content with a filter which in turn adds the shortcodes when that filter is run, what you're seeing is probably happening because when you call "do_shortcode" those shortcodes don't actually exist. 因此,考虑到通过使用过滤器修改内容来编写此插件的方式,该过滤器又在运行该过滤器时添加了短代码,因此您看到的可能正在发生,因为当您调用“ do_shortcode”时,这些短代码实际上并不存在。

What echoing do_shortcode is doing then, is just coughing up the text. 然后,回声do_shortcode所做的只是咳嗽文本。

Unfortunately, because of the way the Post UI Tabs plugin is written, you may not be able to do what you're trying to do. 不幸的是,由于Post UI Tabs插件的编写方式,您可能无法执行您想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM