将执行用户输入的短码的小部件

时间:2014-05-14 作者:MJ135

我有一个我正在使用的小部件,它允许用户输入一个短代码,并将其显示在侧栏中。但它不正常工作。我可以输入短代码,但它没有输出。此外,它还接缝以剥离[],如果包含size=35,则在点击保存按钮后将剥离35。以下是部分代码:

     //Set Shortcode 1 title
    $sc1title = apply_filters( \'Shortcode 1 Title\', $instance[\'sc1title\'] );

    //Display Shortcode 1 title
    if ( ! empty( $sc1title ) )
        echo $args[\'before_sc1title\'] . "<h3>$sc1title</h3>" . $args[\'after_sc1title\'];

    //Do Shortcode 1
    //echo do_shortcode(\'sc1\');
    //echo do_shortcode($this->shortcodes[$instance[\'sc1\']]);
    echo do_shortcode(\'[sc1]\');

echo "<br>";

    //Set Shortcode 2 title
    $sc2title = apply_filters( \'Shortcode 2 Title\', $instance[\'sc2title\'] );

    //Display Shortcode 2 title
    if ( ! empty( $sc2title ) )
        echo $args[\'before_sc1title\'] . "<h3>$sc2title</h3>" . $args[\'after_sc1title\'];

    //Do Shortcode 1
    //echo do_shortcode(\'sc2\');
    echo do_shortcode($this->shortcodes[$instance[\'sc2\']]); 

    echo $args[\'after_widget\'];



    // Display after widget code
    echo $after_widget;
}

// Update the widget settings.
public function update( $new_instance, $old_instance ) {
    $instance[\'title\'] = strip_tags( stripslashes($new_instance[\'title\']) );
    $instance[\'mtitle\'] = strip_tags( stripslashes($new_instance[\'mtitle\']) );
    $instance[\'nav_menu\'] = (int) $new_instance[\'nav_menu\'];
    $instance[\'sc1title\'] = strip_tags( stripslashes($new_instance[\'sc1title\']) );
    $instance[\'sc1\'] = strip_tags( stripslashes($new_instance[\'sc1\']) );
    $instance[\'sc2title\'] = strip_tags( stripslashes($new_instance[\'sc2title\']) );
    $instance[\'sc2\'] = strip_tags( stripslashes($new_instance[\'sc2\']) );
    return $instance;

}

// Displays the form on the widget page
public function form( $instance ) {
    $title = isset( $instance[\'title\'] ) ? $instance[\'title\'] : \'\';
    $nav_menu = isset( $instance[\'nav_menu\'] ) ? $instance[\'nav_menu\'] : \'\';
    $mtitle = isset( $instance[\'mtitle\'] ) ? $instance[\'mtitle\'] : \'\';
    $sc1title = isset( $instance[\'sc1title\'] ) ? $instance[\'sc1title\'] : \'\';
    $sc1 = isset( $instance[\'sc1\'] ) ? $instance[\'sc1\'] : \'\';
    $sc2title = isset( $instance[\'sc2title\'] ) ? $instance[\'sc2title\'] : \'\';
    $sc2 = isset( $instance[\'sc2\'] ) ? $instance[\'sc2\'] : \'\';

    // Get menus
    $menus = wp_get_nav_menus( array( \'orderby\' => \'name\' ) );

    // If no menus exists, direct the user to go and create some.
    if ( !$menus ) {
        echo \'<p>\'. sprintf( __(\'No menus have been created yet. <a href="%s">Create some</a>.\'), admin_url(\'nav-menus.php\') ) .\'</p>\';
        return;
    }
    ?>
    <p>
        <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\', \'wpspprofwid\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" value="<?php echo $title; ?>" />   
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( \'mtitle\' ); ?>"><?php _e( \'Menu Title:\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id(\'mtitle\'); ?>" name="<?php echo $this->get_field_name(\'mtitle\'); ?>" value="<?php echo $mtitle; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( \'nav_menu\' ); ?>"><?php _e( \'Select Menu:\' ); ?></label>
        <select id="<?php echo $this->get_field_id( \'nav_menu\' ); ?>" name="<?php echo $this->get_field_name(\'nav_menu\'); ?>">
    <?php
        foreach ( $menus as $menu ) {
            echo \'<option value="\' . $menu->term_id . \'"\'
                . selected( $nav_menu, $menu->term_id, false )
                . \'>\'. $menu->name . \'</option>\';
        }
    ?>
        </select>
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( \'sc1title\' ); ?>"><?php _e( \'Shortcode 1 Title:\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id(\'sc1title\'); ?>" name="<?php echo $this->get_field_name(\'sc1title\'); ?>" value="<?php echo $sc1title; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( \'sc1\' ); ?>"><?php _e( \'Shortcode 1:\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id(\'sc1\'); ?>" name="<?php echo $this->get_field_name(\'sc1\'); ?>" value="<?php echo $sc1; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( \'sc1title\' ); ?>"><?php _e( \'Shortcode 2 Title:\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id(\'sc2title\'); ?>" name="<?php echo $this->get_field_name(\'sc2title\'); ?>" value="<?php echo $sc2title; ?>" />
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( \'sc2\' ); ?>"><?php _e( \'Shortcode 2:\' ); ?></label>
        <input type="text" class="widefat" id="<?php echo $this->get_field_id(\'sc2\'); ?>" name="<?php echo $this->get_field_name(\'sc2\'); ?>" value="<?php echo $sc2; ?>" />
    </p>

1 个回复
SO网友:Edd Smith

我有一种感觉,默认情况下,Wordpress不允许您在侧栏中使用短代码。

您添加了吗

添加过滤器(\'widget\\u text\',\'do\\u shortcode\');

至功能。php

结束

相关推荐

针对在widgets.php页面上的WP指针可拖动的小工具的提示

我正在更新我编写的名为Feature Me的小部件插件,并为第一次安装插件的用户添加WordPress指针,以帮助他们在安装插件后找到小部件。然而,我遇到了一个问题。指针必须有一个目标,该目标是一个ID# 或者一个班级. (正常情况下可以)。但是在widget页面上,所有widget都以widget-[somenumber]. 如果我知道[某个数字]是什么,我可以毫无问题地瞄准它,但99%的时候,我不知道数字是什么,因为它实际上取决于插件的安装顺序。在其他网站上,这是小部件的ID:希望你能看到我的问题。在