0

I'm trying to build out a plugin from a tutorial that will access my google calendar. So far I'm stuck on just trying to register the widget. Here is a link to the tutorial:

https://code.tutsplus.com/tutorials/how-to-create-a-google-calendar-plugin-for-wordpress--cms-32710

I keep running into the problem of the 'add_action' function at the bottom of the script. I keep getting an error of 'syntax error, unexpected 'add_action'. I'm not even sure I'm building this correctly.

<?php

/** 
 * Plugin Name: My gCal Plugin
 * Description: Displays upcoming events from Google calender
 * version: 1.0.0
 * 
 * 
 */


class My_gCal_Widget extends WP_Widget
{
    public function __construct()
    {
        parent::__construct(
            'my_gcal_widget',
            'My_gCal_Widget',
            array(
                'classname' => 'my-gcal-widget',
                'description' => 'Shows events from a calendar'
            )
        );
    }

    public function widget($args, $instance)
    {
    }
}

add_action('widgets_init', function () {
    register_widget('My_gCal_Widget');
});
5
  • your code has no indentation, which conceals lots of possible problems, please indent your code correctly, it will make the issue more obvious and help people trying to answer your question. Use the Edit link under the tags to update your question
    – Tom J Nowell
    Nov 17, 2021 at 16:33
  • which version of PHP are you using?
    – Tom J Nowell
    Nov 17, 2021 at 23:26
  • @TomJNowell 7.3.5
    – RRhodes
    Nov 18, 2021 at 0:17
  • No syntax issues were found via some checker tools, can you delete the contents of your file then replace them with the code in your question? I do not believe the cause of the problem has survived the transition from your file to your question. Also please update the question with the full error message shown in the PHP error log or in the WP Admin area on activation
    – Tom J Nowell
    Nov 18, 2021 at 1:03
  • otherwise the only way I can see this PHP syntax error being a thing is if you try to load the file directly in the browser. It's possible VS Code has not been configured and isn't aware of WordPress PHP functions and you should ignore it. Unless your code generates a syntax error when it runs there is no syntax error. As for why your widget doesn't show, did you activate the plugin? And did you follow the rest of the tutorial? Your widget does not have any form or update functions
    – Tom J Nowell
    Nov 18, 2021 at 1:14

1 Answer 1

0

It's not entirely clear in the tutorial, but the add_action() call should be outside the class.

class My_gCal_Widget extends WP_Widget {
    public function __construct() {
        parent:: __construct(
            'my_gcal_widget',
            'My_gCal_Widget',
            array(
                'classname' => 'my-gcal-widget',
                'description' => 'Shows events from a calendar'
            )
        );
    }

    public function widget($args, $instance) { }

}

add_action('widgets_init', function() {
    register_widget('My_gCal_Widget');
});
8
  • That's what I thought too, but same error occurs.
    – RRhodes
    Nov 17, 2021 at 16:03
  • @RRhodes where is this file and how is it being loaded? Are you trying to load it directly in the browser bypassing WordPress?
    – Tom J Nowell
    Nov 17, 2021 at 16:32
  • @TomJNowell The file is my plugins folder. I'm fairly new to this, so I'm just following the tutorial. The author didn't mention how to load the file.
    – RRhodes
    Nov 17, 2021 at 17:15
  • You activate it like any other WordPress plugin: go to your WordPress admin pages (usually at example.com/wp-admin) and select Plugins. Find the My GCal Plugin plugin and click Activate.
    – Pat J
    Nov 17, 2021 at 17:31
  • @PatJ Yep, I did that part already
    – RRhodes
    Nov 17, 2021 at 18:13

Your Answer

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

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