Php 从wordpress函数内部的小部件访问函数

Php 从wordpress函数内部的小部件访问函数,php,wordpress,Php,Wordpress,我有一些像这样的小部件 <?php /* Plugin Name: MyWidget Plugin URI: https:/// Description: License: GPL2 */ // The widget class class MyWidget extends WP_Widget { // Main constructor public function __construct() { parent::__construct(

我有一些像这样的小部件

<?php
/*
Plugin Name: MyWidget
Plugin URI: https:///
Description: 
License: GPL2
*/

// The widget class
class MyWidget extends WP_Widget
{

    // Main constructor
    public function __construct()
    {
        parent::__construct(
            'mywidgert',
            __('MyWidget', 'password_domain'),
            array(
                'customize_selective_refresh' => true,
            )
        );
    }

    public function form($instance)
    {
    }

    // Update widget settings
    public function update()
    {
    }


    public function widget()
    {

    }

    public function get_user_token()
    {
        return $login_token;
    }
}

function register_my_widget()
{
    register_widget('MyWidget');
}

add_action('widgets_init', 'register_my_widget');
function export_database(){
 $this->get_user_token();
}
$widget = new MyWidget();
$widget->get_user_token();
我犯了500个错误,我也试过类似的方法

<?php
/*
Plugin Name: MyWidget
Plugin URI: https:///
Description: 
License: GPL2
*/

// The widget class
class MyWidget extends WP_Widget
{

    // Main constructor
    public function __construct()
    {
        parent::__construct(
            'mywidgert',
            __('MyWidget', 'password_domain'),
            array(
                'customize_selective_refresh' => true,
            )
        );
    }

    public function form($instance)
    {
    }

    // Update widget settings
    public function update()
    {
    }


    public function widget()
    {

    }

    public function get_user_token()
    {
        return $login_token;
    }
}

function register_my_widget()
{
    register_widget('MyWidget');
}

add_action('widgets_init', 'register_my_widget');
function export_database(){
 $this->get_user_token();
}
$widget = new MyWidget();
$widget->get_user_token();

我再次遇到错误500,如何从MyWidget类中访问我的函数导出数据库,请记住这是wordpress,谢谢你可以使用add_action来实现它,首先在加载主题文件之前注册操作:

<?php
/*
Plugin Name: MyWidget
Plugin URI: https:///
Description: 
License: GPL2
*/

add_action( 'init', array ( 'MyWidget', 'init' ) );

// The widget class
class MyWidget extends WP_Widget
{
    ...
}
最后可以调用
do\u操作('get\u user\u token',50)在你的主题或其他插件中


请参阅此相关帖子:

您可以通过使用“添加”操作来实现它,首先在加载主题文件之前注册操作:

<?php
/*
Plugin Name: MyWidget
Plugin URI: https:///
Description: 
License: GPL2
*/

add_action( 'init', array ( 'MyWidget', 'init' ) );

// The widget class
class MyWidget extends WP_Widget
{
    ...
}
最后可以调用
do\u操作('get\u user\u token',50)在你的主题或其他插件中

请参阅此相关帖子: