1

I've created the following plugin which requires a js library, however attempts to load the plugin with the line add_action( 'wp_enqueue_scripts', 'calc_script' ); in place fail with the error unexpected T_STRING any help appreciated.

<?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 Answer 1

3

Because calc_script is inside your class, it's not publicly available like a regular function. So, change your enqueue call to this:

add_action( 'wp_enqueue_scripts', array( $this , 'calc_script' ) );

And make calc_script() public.

Edit: Your add_action() is in the class as well, it needs to be in another function, preferably one hooked to the init action.

Second Edit: Added working example from a plugin on a production site, just changed the namespace and edited for simplicity as we're only hooking one action in this case. Assuming the css file is in your plugin root directory and this is the initial plugin file, i.e. where you have the: /* Plugin Name: My Plugin */ declaration at the top.

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

Last Edit Hopefully!: I saw that your original class is hooked onto widgets_init. That's the likely culprit, that fires too late I believe. Put your wp_enqueue_script in a different class or remove it from the class. Just make sure to add a prefix to your functions/class names.

7
  • 1
    Also, your full PHP error should identify exactly where the problem is.
    – vancoder
    Commented Apr 12, 2013 at 16:32
  • If you used a decent IDE, it wouldn't let you get away with this kind of error in the first place.
    – vancoder
    Commented Apr 12, 2013 at 16:34
  • True, what ide are you using @toomanyairmiles? Commented Apr 12, 2013 at 16:40
  • Ok - but how does one go about making calc_script public? Commented Apr 12, 2013 at 23:31
  • Type public in front of it :) public function calc_script() stackoverflow.com/questions/4361553/… Commented Apr 12, 2013 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.