add_action always resulting in unexpected T_STRING
-
Hi WordPressers,
Since a few days now, i am trying to develop some simple plugins to get better at it. Now i have smacked into this problem i cant find the solution to.
Every time i try to call add_action(), it seems to return the following error:
Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in [folder] on line 48
If i watch and/or read tutorials, the function i wrote should work just fine.
class google_maps_api extends WP_widget { function google_maps_js(){ wp_enqueue_script("jquery"); wp_register_script('google_maps_api_script', plugins_url('/js/google_maps_api.js', __FILE__) ); wp_enqueue_script('google_maps_api_script', plugins_url('/js/google_maps_api.js', __FILE__) ); } add_action('wp_head','google_maps_js'); }
Could anyone clarify what i am doing wrong? Thanks in advance!
-
Try it with this:
class google_maps_api extends WP_widget { function __construct() { add_action( 'wp_head', array( $this, 'google_maps_js' ) ); } function google_maps_js() { wp_enqueue_script( "jquery" ); wp_register_script( 'google_maps_api_script', plugins_url( '/js/google_maps_api.js', __FILE__ ) ); wp_enqueue_script( 'google_maps_api_script', plugins_url( '/js/google_maps_api.js', __FILE__ ) ); } }
See: https://codex.www.ads-software.com/Function_Reference/add_action#Using_add_action_with_a_class
It replaced the error to line 35, which in this case is
function __construct() {
It just does not want to recognize those lines as a function.
Do you get the exact same error (syntax error, unexpected T_STRING)? Is this all the code?
No, but since it is the first line where it stops, i doubt the other code has something to do with it. It is right at the top, just beneath the ususal commented plugin information, but i will post the script as it is right now.
Note: I did left some commenting and experimental code out in my first post because i think that can not be the problem.
<?php /** * @package google_maps_api * @version 0.1.0 */ /* Plugin Name: Google Maps API Description: ******* Version: 0.1.0 Author: ****** Author URI: ******* License: GPLv2 */ /* Copyright 2013 **** (info@****.**) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ class google_maps_api extends WP_widget { funtion __construct() { add_action('wp_head', array( $this, 'google_maps_js' ) ); } function google_maps_js(){ // jQuery core wp_enqueue_script("jquery"); // plugin specific variables $map_location = esc_attr( $location ); $map_zoom = esc_attr( $zoom ); // plugin specific files //wp_enqueue_style ('google_maps_api_style', plugins_url('/css/google_maps_api.css', __FILE__) ); wp_register_script('google_maps_api_script', plugins_url('/js/google_maps_api.js', __FILE__) ); wp_enqueue_script('google_maps_api_script', plugins_url('/js/google_maps_api.js', __FILE__) ); } //add_action('wp_enqueue_scripts','google_maps_css'); function google_maps_css() { wp_register_style( 'prefix-style', plugins_url('/css/google_maps_api.css', __FILE__) ); wp_enqueue_style( 'prefix-style' ); } function google_maps_api() { $widget_options = array( 'classname' => 'google_maps_api', 'description' => 'A google maps plugin that you can use to place a google maps map inside one of your widget areas.' ); parent::WP_Widget('google_maps_api', 'Google Maps API', $widget_options); } function widget($args, $instance) { extract( $args, EXTR_SKIP ); $title = ( $instance['title'] ) ? $instance['title'] : 'Google Map'; ?> <?php echo $before_widget; ?> <?php echo $before_title . $title . $after_title; ?> <div id="map-canvas" /> <?php echo $after_widget; ?> <?php } function update($new_instance, $old_instance) { $instance = $old_instance; $instance ['title'] = strip_tags( $new_instance['title'] ); $instance ['key'] = strip_tags( $new_instance['key'] ); $instance ['location'] = strip_tags( $new_instance['location'] ); $instance ['zoom'] = strip_tags( $new_instance['zoom'] ); return $instance; } function form($instance) { $defaults = array( 'title' => 'Follow Me', 'key' => '', 'location' => '', 'zoom' => '' ); $instacne = wp_parse_args( (array) $instance, $defaults ); $title = $instance['title']; $key = $instance['key']; $location = $instance['location']; $zoom = $instance['zoom']; ?> <p>Title: <input class="google_maps_api" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p> <p>API Key: <input class="google_maps_api" name="<?php echo $this->get_field_name( 'key' ); ?>" type="text" value="<?php echo esc_attr( $key ); ?>" /></p> <p>Initial Location: <input class="google_maps_api" name="<?php echo $this->get_field_name( 'location' ); ?>" type="text" value="<?php echo esc_attr( $location ); ?>" /></p> <p>Initial Zoom: <input class="google_maps_api" name="<?php echo $this->get_field_name( 'zoom' ); ?>" type="text" value="<?php echo esc_attr( $zoom ); ?>" /></p> <?php } } $google_maps_api_array = new google_maps_api(); $google_maps_api_vars = get_class_vars(get_class($google_maps_api_array)); print_r ($google_maps_api_vars['widget_options']); function google_maps_api_init() { register_widget('google_maps_api'); } add_action( 'widgets_init', create_function('', 'return register_widget("google_maps_api");') ); ?>
Try changing this:
funtion __construct()
to this:
function __construct()
*facepalm* sorry for the typo error, that happens to me way too much than it should…
This did it i think! I am getting new errors about the following lines of code, so i can continue debugging those! Many thanks!!!
Wait no.. It seems that this placed almost all of my <head> content in the <body>. Not sure if this is fixed when i solve the errors though.
I think register_widget() will instantiate the widget class. Maybe removing this will help:
$google_maps_api_array = new google_maps_api(); $google_maps_api_vars = get_class_vars(get_class($google_maps_api_array)); print_r ($google_maps_api_vars['widget_options']);
Well while this did fix one of the errors i was having, this did not solve the dumping of the head information inside the body.
I got everything back in the header again. It seems i messed up the script by trying to add the google maps JavaScript as an internal file. The file is secured in a way that it can only be loaded from google’s own API page.
Because of this error, wordpress behaved as if the head element was done, and put everything else in the body tag. I hope this is usefull to people. I still think this reaction is a bit weird from wordpress. A bug maybe?
Nope, still broken. It still gets dumped in the body. I am pretty confused right now…
These are the errors i am getting:
Notice: Undefined property: google_maps_api::$option_name in ***/wordpress/wp-includes/widgets.php on line 291
Warning: array_merge() [function.array-merge]: Argument #2 is not an array in ***/wordpress/wp-includes/widgets.php on line 788
As this is not referring to my own code, i have no idea where this is coming from.
As this is not referring to my own code, i have no idea where this is coming from.
Do these notices go away when you disable your plugin?
You’re probably registering your widget wrong. Try it with this:
https://pastebin.com/T1tfBJ38Also read how to properly enqueue scripts:
https://codex.www.ads-software.com/Function_Reference/wp_enqueue_scriptThat’s it! That got the widget to appear. The last problem i cannot seem to solve is to correctly render the google map. I get two errors in the console that are kind of unknown to me, so i do not know how to solve it.
GET: https://maps.googleapis.com/maps/api/js/ViewportInfoService.GetViewportInfo?1m6&1m2&1dNaN&2dNaN&2m2&1dNaN&2dNaN&2u8&4sen&5e0&6sm%40245000000&7b0&8e0&9b0&callback=_xdc_._oay0qo&token=123778 : 403(forbidden
and
It is either that, or i have to rerender the api after the page is loaded (as this seems to be a issue that is known to google).
Do you see in the source code if the javascript is loaded correctly?
https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API<script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/en_ALL/mapfiles/api-3/15/1/%7Bcommon,map%7D.js"></script> <script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/en_ALL/mapfiles/api-3/15/1/%7Butil,onion%7D.js"></script> <script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/en_ALL/mapfiles/api-3/15/1/%7Bcontrols,stats%7D.js"></script> <script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/en_ALL/mapfiles/api-3/15/1/%7Bmarker%7D.js"></script>
These are the scripts related to google maps that are getting inserted into my header. There is no sign of the original script to be seen. Is that normal?
I can see the map being inserted, however it is entirely grey with a strange “glitch” in the top left corner.
- The topic ‘add_action always resulting in unexpected T_STRING’ is closed to new replies.