skip to Main Content

I’m trying to register my custom widget in WordPress. Unfortunately I came across a problem in communication with my another class FacebookServices. I can not figure out why I still getting error Uncaught Error: Call to a member function getFbPage() on null.

Here is a class where I am creating the widget which is pretty standard until I want to call html code into my function widget():

<?php
/**
 * Facebook Page widget
 */

namespace IncApiWidgets;

use WP_Widget;
use IncPluginsFacebookServices;



class FbPage extends WP_Widget
{
    public $fb_services;

    public $widget_ID;
    public $widget_name;
    public $widget_options = array();
    public $control_options = array();



    public function __construct()
    {
        $this->widget_ID = 'fb_page';
        $this->widget_name = 'Facebook Page';
        $this->widget_options = array(
            'classname'                     => $this->widget_ID,
            'description'                   => 'Widget of Facebook Page',
            'customize_selective_refresh'   => true
        );
        $this->control_options = array();
    }



    public function register()
    {
        parent::__construct( $this->widget_ID, $this->widget_name, $this->widget_options, $this->control_options );

        add_action( 'widgets_init', array( $this, 'widgetInit') );

        $fb_services = new FacebookServices();
    }



    public function widgetInit()
    {
        register_widget( $this );
    }



    public function widget( $args, $instance )
    {
        echo $this->fb_services->getFbPage();   // <= line 58 from error
    }
...

And here is the calling class:

<?php
/**
 * Ensure Facebook integration
 */

namespace IncPlugins;

class FacebookServices
{
    public $url;

    public function __construct()
    {
        $this->url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    }



    public function theFbLikes()
    {   
        echo '<div class="fb-plugin-wrapper js-fbPlugin likes rendering">';
        echo '<div class="fb-like" data-href="' . $this->url . '" data-layout="standard" data-action="like" data-size="small" data-show-faces="true" data-share="true"></div>';
        echo '</div>';
    }



    public function getFbPage()
    {
        return 'test';
    }
}

I suppose to get 'test' but every time I call function getFbPage() I get error:

Fatal error: Uncaught Error: Call to a member function getFbPage() on null in C:xampphtdocsdev1wp-contentthemestomvaincApiWidgetsFbPage.php:58 Stack trace: #0 C:xampphtdocsdev1wp-includesclass-wp-widget.php(372): IncApiWidgetsFbPage->widget(Array, Array) #1 C:xampphtdocsdev1wp-includeswidgets.php(743): WP_Widget->display_callback(Array, Array) #2 C:xampphtdocsdev1wp-contentthemestomvasingle.php(56): dynamic_sidebar('static') #3 C:xampphtdocsdev1wp-includestemplate-loader.php(74): include('C:\xampp\htdocs...') #4 C:xampphtdocsdev1wp-blog-header.php(19): require_once('C:\xampp\htdocs...') #5 C:xampphtdocsdev1index.php(17): require('C:\xampp\htdocs...') #6 {main} thrown in C:xampphtdocsdev1wp-contentthemestomvaincApiWidgetsFbPage.php on line 58

I can not figure out what miss me.

3

Answers


  1. You are not setting the class property fb_services, you could instantiate in your class __construct() method:

    class FbPage extends WP_Widget
    {
        public $fb_services;
        public $widget_ID;
        public $widget_name;
        public $widget_options = array();
        public $control_options = array();
    
        public function __construct()
        {
            $this->widget_ID = 'fb_page';
            $this->widget_name = 'Facebook Page';
            $this->widget_options = array(
                'classname'                     => $this->widget_ID,
                'description'                   => 'Widget of Facebook Page',
                'customize_selective_refresh'   => true
            );
            $this->control_options = array();
            $this->fb_services = new FacebookServices();
        }
    
    Login or Signup to reply.
  2. $this->fb_services = new FacebookServices();
    

    Just add this line in your constructor if you want to do it like this.
    Keep in mind though to remove the last line of your register function cause you have already created the instance.

    Or simple follow what you almost did in the register function if you are not going to use it in other functions.

    $fb_services = new FacebookServices();
    $fb_services->getFbPage();
    

    Generally if you want to use the instance of your class in most of your functions then it’s good to instantiate it in your constructor but if it’s a one time thing just do it inside your function to be used once when this function is called.

    Login or Signup to reply.
  3. From the code in your widget class, it seems the $fb_services property is declared but never initialized.

    In the register() method, try changing this:

    $fb_services = new FacebookServices();

    into:

    $this->fb_services = new FacebookServices();

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search