• Resolved nobody123

    (@nobody123)


    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();

    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

    • This topic was modified 2 years, 6 months ago by nobody123.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator bcworkz

    (@bcworkz)

    There’s not enough context on where this code resides to say, but there’s likely a dependency or sequencing issue. I tried your code in my test plugin. I had to revise the salcodes_year() function to return a simple year string since I don’t have the referenced file or class, nor the ‘my_url’ option. The shortcode thus modified works for me. I didn’t test Ajax, but if you get the shortcode working, perhaps Ajax will as well.

    The shortcode callback shouldn’t echo out any content. It will not cause a failure, but it’s still Doing It Wrong?

    Where did you place the Test class declaration? I placed it in my main plugin file and it works from there.

    Thread Starter nobody123

    (@nobody123)

    @bcworkz
    1. I found $this->val can’t work in the function of add_action or add_shortcode in the same class.
    Therefore, $my_crawler = new Test_one($infos); should change to $my_crawler = new Test_one(get_option('my_val'));

    2. I get error Fatal error: Uncaught Error: Class ‘Test_one’ not found when I run my code.
    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();
        }
    }

    3. my file directory:
    The class of Test is in my_plugin_name.php

    my_plugin_name—assets
    | |
    | —js
    | |
    | –app.js
    |
    —src
    | |
    | –my_test1.php
    |
    |
    —my_plugin_name.php

    • This reply was modified 2 years, 6 months ago by nobody123.
    Moderator bcworkz

    (@bcworkz)

    I think you have a race condition. PHP cannot read the file and parse test1.php fast enough before the main processing thread hits the new Test_one() statement. You’ll need to require the file outside of the salcodes_year() callback. Just in time coding is cool, but that’s simply too close to the need.

    Thread Starter nobody123

    (@nobody123)

    So….how do I fix this question?
    Thank you

    Moderator bcworkz

    (@bcworkz)

    You need to require the file well before you need to instantiate the class. Perhaps within a constructor function of Test? Or within js2php_register() since you essentially are using that as a constructor.

    Thread Starter nobody123

    (@nobody123)

    I got this error after moving the code within a constructor function of Test

    Fatal error: Uncaught Error: Class 'Test_one' not found in D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-content\plugins\crawler\crawler.php:131 Stack trace: #0 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-content\plugins\crawler\crawler.php(185): Test->__construct() #1 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-settings.php(409): include_once('D:\\Appserv\\Apac...') #2 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-config.php(96): require_once('D:\\Appserv\\Apac...') #3 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-load.php(50): require_once('D:\\Appserv\\Apac...') #4 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-admin\admin.php(34): require_once('D:\\Appserv\\Apac...') #5 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-admin\index.php(10): require_once('D:\\Appserv\\Apac...') #6 {main}

    The following is my code:

    class Test{
        private $infos;
        
        function __construct()
        {
            add_action( 'wp_ajax_crawler_info', array($this, 'get_info') );
            add_action( 'wp_ajax_nopriv_crawler_info', array($this, 'get_info') );        
            require_once plugin_dir_url(__FILE__) . 'src/ming_crawler.php';
            $this->my_crawler = new Test_one(get_option('my_val'));
        }
      
        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() {
            return get_option('my_url');        
        }
    
        function js2php_register(){
            add_shortcode( 'current_year', array($this, 'salcodes_year') );
        }
        
    }
    $test = new Test;
    $test->js2php_register();
    Thread Starter nobody123

    (@nobody123)

    I got this error after moving the code withinjs2php_register()
    Fatal error: Uncaught Error: Class 'Test_one' not found in D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-content\plugins\crawler\crawler.php:176 Stack trace: #0 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-content\plugins\crawler\crawler.php(183): Test->js2php_register() #1 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-settings.php(409): include_once('D:\\Appserv\\Apac...') #2 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-config.php(96): require_once('D:\\Appserv\\Apac...') #3 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-load.php(50): require_once('D:\\Appserv\\Apac...') #4 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-admin\admin.php(34): require_once('D:\\Appserv\\Apac...') #5 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-admin\index.php(10): require_once('D:\\Appserv\\Apac...') #6 {main}

    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() {
            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') );        
            require_once plugin_dir_url(__FILE__) . 'src/ming_crawler.php';
            $this->my_crawler = new Test_one(get_option('my_val'));
            add_shortcode( 'current_year', array($this, 'salcodes_year') );
        }
        
    }
    $test = new Test;
    $test->js2php_register();
    Moderator bcworkz

    (@bcworkz)

    You’ve misunderstood. I probably wasn’t clear enough. Move only the require file statement to constructor or js2php_register(), NOT the new Test_one() statement! Moving them together doesn’t accomplish anything, the point is to separate them in time of execution. The new Test_one() statement should remain where it was, in the shortcode callback salcodes_year().

    Thread Starter nobody123

    (@nobody123)

    Thank for your advise, but I got error after I move my code.
    The error:
    Fatal error: Uncaught Error: Class 'Test_one' not found in D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-content\plugins\crawler\crawler.php:160 Stack trace: #0 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-includes\shortcodes.php(356): Test->shortcode_value('', '', 'current_year') #1 [internal function]: do_shortcode_tag(Array) #2 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-includes\shortcodes.php(228): preg_replace_callback('/\\[(\\[?)(curren...', 'do_shortcode_ta...', '\n<p>123456789</...') #3 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-includes\class-wp-hook.php(303): do_shortcode('\n<p>123456789</...') #4 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-includes\plugin.php(189): WP_Hook->apply_filters('\n<p>123456789</...', Array) #5 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-includes\post-template.php(253): apply_filters('the_content', '<!-- wp:paragra...') #6 D:\Appserv\Apache24\htdocs\php7.4\wordpress\wp-content\themes\twentytwentyone_test\template-parts\content\content-single.php(23): the_content()

    The code1 (moving tojs2php_register()):

    function js2php_register(){
            require_once plugin_dir_url(__FILE__) . 'src/ming_crawler.php';
            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') );
            
        }

    OR

    The code2 (moving to__construct()):

    function __construct()
        {
           require_once plugin_dir_url(__FILE__) . 'src/ming_crawler.php';
        }

    No matter which I move to code1 or code2, I got same error.
    Could you help me, thanks

    • This reply was modified 2 years, 6 months ago by nobody123.
    Moderator bcworkz

    (@bcworkz)

    Ah! I see another issue, you use plugin_dir_url(), which returns a HTTPS URL path, which is invalid for any sort of require statement. You want to use plugin_dir_path() instead, which returns the full server path to __FILE__ or whatever was passed to it. Sever path as in /home/user1234/wp/wp-content/plugins/my-plugin/, not httр?://example.com/wp-content/plugins/my-plugin/

    Thread Starter nobody123

    (@nobody123)

    Cool!!!
    It work!!!
    Thank you very very very much!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘why does add_action or add_shortcode not create an instance of a class’ is closed to new replies.