简体   繁体   中英

Load plugin class in functions.php

I am using wordpress 4.9.8 and PHP 7.1.8 I want to load a function from my class.

The plugin class is located under:

C:\\Users\\admin\\Desktop\\wordpress\\wp-content\\plugins\\content-creator\\includes\\SinglePostContent.php

My function.php file is with the following folder:

C:\\Users\\admin\\Desktop\\wordpress\\wp-content\\themes\\rehub-blankchild\\functions.php

The function I would like to load looks like the following:

class SinglePostContent
{

    public function __construct()
    {
        //...
    }

    public function main($postID)
    {
      //...
    }

I tried to use the

add_action('wp_ajax_updateContent', 'updateContent');
function updateContent()
{

    $post_id = intval($_POST['post_id']);

    try {
        SinglePostContent::main($post_id); // HERE I get the error!
    } catch (Exception $e) {
        echo $e;
    }
    wp_die();

}

Any suggestion how to load the class SinglePostContent within my function.php

You are accessing the method as static which is not. You need to instantiate the class and then call the method like this:

add_action('wp_ajax_updateContent', 'updateContent');

function updateContent(){

    $post_id = 1;
    $single_post_content = new SinglePostContent;
    try {

        $single_post_content->main( $post_id );  
    } catch ( Exception $e ) {
        echo $e;
    }
    wp_die();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM