why does add_action or add_shortcode not create an instance of a class

54 Views Asked by At

I wanted to create an instance of a class when my program start add_shortcode(), but I find it not worked. Does anyone can help me solve this problem? The following is my code:

class Test{
    private $infos;
      
    function get_info() {
        if ( isset($_REQUEST['my_info']) ) {
    
            $infos = $_REQUEST['my_info'];
            if (get_option('my_url')){
                update_option('my_url', $infos);
            }else{
                add_option('my_url', $infos);
            }
        }
        die();
    }
    
    function salcodes_year() {
        echo plugin_dir_url(__FILE__) . 'src/my_test1.php';
        require_once plugin_dir_url(__FILE__) . 'src/my_test1.php';
        $my_crawler = new Test_one($infos);
        return get_option('my_url');        
    }

    function js2php_register(){
        add_action( 'wp_ajax_crawler_info', array($this, 'get_info') );
        add_action( 'wp_ajax_nopriv_crawler_info', array($this, 'get_info') );
        add_shortcode( 'current_year', array($this, 'salcodes_year') );
    }
    
}
$test = new Test;
$test->js2php_register();

I get error Fatal error: Uncaught Error: Class 'Test_one' not found when I run my code. enter image description here The following is my_test1.php:

<?php
class Test_one{
    function a(){
        $this->a1='aaa';
    }
    function b(){
        echo 'a1:'.$this->a1;
    }
    function c(){
        $this->a();
        $this->b();
    }
}

Through my testing I found no matter how do I put these code, require_once plugin_dir_url(__FILE__) . 'src/my_test1.php'; & $my_crawler = new Test_one($infos);, in the function of add_shortcode() or add_action('wp_ajax_') both are not work...

Does anyone help me, Please Thanks

0

There are 0 best solutions below