WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Broken Plugin, unexpected T_function (16 posts)

  1. bouncebackdata1
    Member
    Posted 1 year ago #

    Hello,
    There is only one plugin I can find for an airport or flight school to display the current weather conditions and I would love to get it working. The developer seems to not exist anymore, so maybe someone on here can help. I get the following error whenever I try to activate the plugin: "Parse error: syntax error, unexpected T_FUNCTION in /home/content/63/8624163/html/wp-content/plugins/metar-widget/metar-widget.php on line 62" Line 62 is the last line. It's a very simple plugin that uses a single php page. Here's the entire code for the plugin:

    <?php
    /*
    Plugin Name: METAR plugin
    Plugin URI: http://wordpress.org/extend/plugins/metar-widget/
    Description: Plugin to include the latest METAR information from NOAA database for any Airport
    Version: 0.1
    Author: Luther Blissett
    Author URI: http://lutherblissett.net
    License: GPL3
    */
    
    class MetarWidget extends WP_Widget
    {
        public function __construct() {
            parent::__construct("metar_widget", "METAR Widget",
                array("description" => "Plugin to inlclude the latest METAR information from NOAA database for any Airport"));
        }
    
        public function form($instance) {
            $icao = "";
            // if instance is defined, populate the fields
            if (!empty($instance)) {
                $icao = $instance["icao"];
            }
    
            $tableId = $this->get_field_id("icao");
            $tableName = $this->get_field_name("icao");
            echo '<label for="' . $tableId . '">ICAO</label>';
            echo '<input id="' . $tableId . '" type="text" name="' .
                $tableName . '" value="' . $icao . '"/>';
        }
    
        public function update($newInstance, $oldInstance) {
            $values = array();
            $values["icao"] = htmlentities($newInstance["icao"]);
            return $values;
        }
    
        public function widget($args, $instance) {
            $icao = $instance["icao"];
    
            $fileName = "http://weather.noaa.gov/pub/data/observations/metar/stations/$icao.TXT";
            $metar = '';
            $fileData = @file($fileName) or die('METAR not available');
            if ($fileData != false) {
            	list($i, $date) = each($fileData);
            	$utc = strtotime(trim($date));
            	$time = date("D, F jS Y g:i A",$utc);
    
            	while (list($i, $line) = each($fileData)) {
            		$metar .= ' ' . trim($line);
                	}
            	$metar = trim(str_replace('  ', ' ', $metar));
            }
    
    	echo '<div class="widget widget-wrapper" id="' . $args['widget_id'] . '-container">';
    	echo '<div class="widget-title"><b>Current METAR for ' . $icao . '</b></div>';
    	echo $metar . '</div>';
        }
    }
    
    add_action("widgets_init", function () { register_widget("MetarWidget"); });

    Any help is greatly appreciated,
    Mike

  2. Hi bouncebackdata1,

    >> Please use http://wordpress.pastebin.com if you're going to post blocks of code in the forums.

    I just activated it without issue on a WordPress 3.4.2. I also noticed the plugin page says it requires a minimum of version 3.4.

    What version of WordPress are you running?

  3. bouncebackdata1
    Member
    Posted 1 year ago #

    Hey, I'm running 3.4.2 as well. Is in conflicting with Jquery or something like that?

  4. bouncebackdata1
    Member
    Posted 1 year ago #

  5. bouncebackdata1
    Member
    Posted 1 year ago #

    I gave you [mod redacted password] so you can take a look if you don't mind. I tried changing line 62 a few times, it broke my site once and I had to go into the SQL database and disable all the plugins for the site to work again.

  6. Rev. Voodoo
    Volunteer Moderator
    Posted 1 year ago #

    NOTE: Support given through this site is free and voluntary. As such, we usually do not want the burden/liability of being given access to a site. Even more so, PLEASE do not post unsolicited and public login credentials out in the open! Kthx!

  7. bcworkz
    Member
    Posted 1 year ago #

    How about PHP version? Anonymous functions are only supported with v5.3. If earlier, name the function and reference it in add_action().

  8. bouncebackdata1
    Member
    Posted 1 year ago #

    I was running 5.2, but I upgraded to 5.3 and I get the same error. I'm using the Karma theme, I'm guessing there must be some conflict with something in there. Anyone have any troubleshooting tips?

  9. bouncebackdata1
    Member
    Posted 1 year ago #

    I just tried activating it on another karma theme site and I get the same error. I tried activating it on an avada theme site and it worked. I'm not sure how to hunt down the problem.

  10. bcworkz
    Member
    Posted 1 year ago #

    You should probably still ditch the anonymous function for the sake of broader compatibility. That could get it working, you can worry about why anonymous functions aren't working later.

  11. bouncebackdata1
    Member
    Posted 1 year ago #

    How do I do that?

  12. bouncebackdata1
    Member
    Posted 1 year ago #

    I'm sorry, I don't know a lot about programming, but I can usually figure things out with some direction.

  13. bouncebackdata1
    Member
    Posted 1 year ago #

    Can anyone tell me how to "ditch the anonymous function"?

  14. alieneila
    Member
    Posted 1 year ago #

    The last line of your code...

    add_action("widgets_init", function () { register_widget("MetarWidget"); });

    see the "function () {}" that is an anonymous function, it's not named.

    try using...

    add_action( 'widgets_init', create_function( '', 'register_widget( "MetarWidget" );' ) );

  15. bouncebackdata1
    Member
    Posted 1 year ago #

    Finally! it works! Thank you very much for the help.

  16. alieneila
    Member
    Posted 1 year ago #

    you're quite welcome =)

Topic Closed

This topic has been closed to new replies.

About this Topic