【问题标题】:Load plugin class in functions.php在functions.php中加载插件类
【发布时间】:2019-02-20 17:57:19
【问题描述】:

我正在使用wordpress 4.9.8PHP 7.1.8 我想从我的类中加载一个函数。

插件类位于:

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

我的function.php 文件位于以下文件夹中:

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

我要加载的函数如下所示:

class SinglePostContent
{

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

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

我尝试使用

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

}

关于如何在我的 function.php 中加载类 SinglePostContent 的任何建议

【问题讨论】:

  • SinglePostContent 是否存在于您未引用的特定命名空间中?

标签: php wordpress


【解决方案1】:

您正在以静态方式访问该方法,但不是。您需要实例化该类,然后像这样调用该方法:

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

【讨论】:

    猜你喜欢
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多