插件中的wp_enQueue_脚本有问题

时间:2013-04-12 作者:toomanyairmiles

我创建了下面的插件,它需要一个js库,但是我试图用这行代码加载插件add_action( \'wp_enqueue_scripts\', \'calc_script\' ); 就地故障并出现错误unexpected T_STRING 感谢您的帮助。

<?php
/**
 * Plugin Name: NME Savings Calculator
 * Description: A widget that displays the potential savings from installing solar panels.
 * Version: 0.1
 * Author: NME
 * Author URI: http://nme.com.au
 */


add_action( \'widgets_init\', \'NME_solar_widget\' );



function NME_solar_widget() {
    register_widget( \'NME_solar_widget\' );
}

class NME_solar_widget extends WP_Widget {

    function NME_solar_widget() {
        $widget_ops = array( \'classname\' => \'example\', \'description\' => __(\'A widget that displays the potential savings from installing solar panels.\', \'example\') );

        $control_ops = array( \'width\' => 300, \'height\' => 350, \'id_base\' => \'example-widget\' );

        $this->WP_Widget( \'example-widget\', __(\'Solar Calculator Widget\', \'example\'), $widget_ops, $control_ops );

    }

    function widget( $args, $instance ) {
        extract( $args );

        //Our variables from the widget settings.
        $title = apply_filters(\'widget_title\', $instance[\'title\'] );
        $name = $instance[\'name\'];
        $daily_kwh = $instance[\'daily_kwh\'];
        $contact_text = $instance[\'contact_text\'];
        $cost_label = $instance[\'cost_label\'];
        $provide_label = $instance[\'provide_label\'];

        echo $before_widget;

        // Display the widget title 
        if ( $title )
            echo $before_title . $title . $after_title;


        //Display the name 

        echo \'<div>\';


            printf( \'<p>\' . __(\'%1$s\', \'example\') . \'</p>\', $name );

            printf( \'<p>\' . __(\'%1$s\', \'example\') . \'&nbsp;<input type="text" id="daily-average" size="8"/> <span id="quote-system-size-daily"> 0 kw </p>\', $daily_kwh );

            printf( \'<p>\' . __(\'%1$s\', \'example\') . \'&nbsp;<input type="text" id="quarter-average" size="8"/> <span id="quote-system-size-quarter"> 0 kw </p>\', $cost_label );

            printf( \'<p><input type="text" id="system-size" size="26"/>&nbsp;\' . __(\'%1$s\', \'example\') . \'<br/><span class="amount" id="quote-savings">\'.\'$0 per year!!\'.\'</p>\', $provide_label );


        echo \'</div>\';

        if ( $contact_text )
        printf( \'<p>\' . __(\'%1$s\', \'example\') . \'</p>\', $contact_text );    

        echo $after_widget;
    }

    //Update the widget 

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        //Strip tags from title and name to remove HTML 
        $instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
        $instance[\'name\'] = strip_tags( $new_instance[\'name\'] );
        $instance[\'daily_kwh\'] = strip_tags( $new_instance[\'daily_kwh\'] );
        $instance[\'contact_text\'] = strip_tags( $new_instance[\'contact_text\'] );
        $instance[\'cost_label\'] = strip_tags( $new_instance[\'cost_label\'] );
        $instance[\'provide_label\'] = strip_tags( $new_instance[\'provide_label\'] );

        return $instance;
    }

function calc_script()
{
    wp_register_script( \'calc\', plugins_url( \'/solar/calc.js\',  __FILE__ ), array( \'jquery\' ) );
    wp_enqueue_script( \'calc\' );
}
add_action( \'wp_enqueue_scripts\', \'calc_script\' );

    function form( $instance ) {

        //Set up some default widget settings.
        $defaults = array( \'title\' => __(\'Type text here\', \'example\'), \'name\' => __(\'Type text here\', \'example\'), \'show_info\' => true );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>


        <p>
            <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e(\'Headline:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" value="<?php echo $instance[\'title\']; ?>" style="width:100%;" />
        </p>


        <p>
            <label for="<?php echo $this->get_field_id( \'name\' ); ?>"><?php _e(\'Introduction Text:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'name\' ); ?>" name="<?php echo $this->get_field_name( \'name\' ); ?>" value="<?php echo $instance[\'name\']; ?>" style="width:100%;" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id( \'daily_kwh\' ); ?>"><?php _e(\'Daily KWH Text:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'daily_kwh\' ); ?>" name="<?php echo $this->get_field_name( \'daily_kwh\' ); ?>" value="<?php echo $instance[\'daily_kwh\']; ?>" style="width:100%;" />
        </p>        

        <p>
            <label for="<?php echo $this->get_field_id( \'cost_label\' ); ?>"><?php _e(\'Cost Text:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'cost_label\' ); ?>" name="<?php echo $this->get_field_name( \'cost_label\' ); ?>" value="<?php echo $instance[\'cost_label\']; ?>" style="width:100%;" />
        </p>    

        <p>
            <label for="<?php echo $this->get_field_id( \'contact_text\' ); ?>"><?php _e(\'Contact Text:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'contact_text\' ); ?>" name="<?php echo $this->get_field_name( \'contact_text\' ); ?>" value="<?php echo $instance[\'contact_text\']; ?>" style="width:100%;" />
        </p>    

        <p>
            <label for="<?php echo $this->get_field_id( \'provide_label\' ); ?>"><?php _e(\'Provide Text:\', \'example\'); ?></label>
            <input id="<?php echo $this->get_field_id( \'provide_label\' ); ?>" name="<?php echo $this->get_field_name( \'provide_label\' ); ?>" value="<?php echo $instance[\'provide_label\']; ?>" style="width:100%;" />
        </p>        

    <?php
    }
}

?>

1 个回复
SO网友:Andrew Bartel

因为calc\\u脚本在类中,所以它不像常规函数那样公开可用。因此,将您的排队呼叫更改为:

add_action( \'wp_enqueue_scripts\', array( $this , \'calc_script\' ) );
并将calc\\u script()公开。

编辑:您的add\\u action()也在类中,它需要在另一个函数中,最好是挂接到init操作的函数。

第二次编辑:添加了生产站点上插件的工作示例,只是更改了名称空间并进行了编辑以简化操作,因为在本例中我们只挂接了一个操作。假设css文件位于您的插件根目录中,并且这是初始插件文件,即您拥有:/* Plugin Name: My Plugin */ 顶部的声明。

$go = new my_plugin;
class my_plugin {

    public function __construct() {
        add_action( \'wp_enqueue_scripts\' , array( $this , \'enqueue_styles\' ) );
    }

    public function enqueue_styles() {
        wp_enqueue_style( \'my_stylesheet.css\' , plugins_url().\'/\'.basename(dirname(__FILE__)).\'/my_stylesheet.css\' );
    }
}
希望最后一次编辑!:我看到您原来的类连接到了widgets\\u init。我认为这可能是罪魁祸首,我认为这已经太迟了。将wp\\u enqueue\\u脚本放在其他类中,或将其从类中删除。只需确保在函数/类名中添加前缀即可。

结束

相关推荐

Travel Blog Plugins

今年晚些时候,我将使用Wordpress创建一个关于我旅行的博客。我希望该博客具有以下功能我的帖子将被地理定位一张包含帖子位置的地图,可以单击地图上的各个点到达帖子</我正在寻找最好/最合适的插件。谢谢,艾尔。