为什么我的 Wordpress 插件 functions.php 文件被多次调用

问题描述

对于使用任何不正确的术语,请提前道歉。如有任何更正,我将不胜感激。

长话短说,我在插件的functions.PHP 文件添加一个日志函数,然后立即调用了该函数。当我检查日志文件时,日志消息已经重复了 9 次。我可以通过考虑重复的函数声明并在functions.PHP 之外调用函数解决这个问题。但我只想知道为什么它会被调用这么多次。

更多详情: 我正在尝试从 enginethemes.com 自定义 Freelance Engine 主题。特别是私信插件主题插件是用 Backbone.js 构建的。我从这个问题为每个 JON 创建了一个日志文件

how to create a logfile in php?

我想记录插件中的一些功能,以便我可以看到它们何时运行。

我随意地将日志功能放在插件的functions.PHP 的顶部。它有效,但问题是它似乎运行了 9 次,我不明白为什么。我已经确认正在使用计数变量重新声明该函数。我还确认functions.PHP 只包含在主插件文件中一次。这是我的functions.PHP 日志函数。 :

<?PHP

define( 'FREELANCER_UNREAD','freelancer_unread');
define( 'EMPLOYER_UNREAD','freelancer_unread');
function wh_log($log_msg)
{
    $log_filename = plugin_dir_path( __FILE__ )."/log";
    if (!file_exists($log_filename))
    {
        // create directory/folder uploads.
        mkdir($log_filename,0777,true);
    }
    $log_file_data = $log_filename.'/log_' . date('d-M-Y') . '.log';
    // if you don't add `FILE_APPEND`,the file will be erased each time you add a log
    file_put_contents($log_file_data,$log_msg . "\n",FILE_APPEND);
    
}
// call to function

wh_log("this is my log message - ". $count_log_test);

这里是使用 require_once 包含functions.PHP 文件的主要插件文件

function require_plugin_file()
{
    if(!class_exists('AE_Base') ){
        return 0;
    }
    add_action( 'wp_enqueue_scripts','ae_plugin_enqueue_scripts' );
    require_once dirname(__FILE__) . '/settings.PHP';
    require_once dirname(__FILE__) . '/template.PHP';
    require_once dirname(__FILE__) . '/functions.PHP';
    require_once dirname(__FILE__) . '/class-private-message-posttype.PHP';
    require_once dirname(__FILE__) . '/class-private-message-actions.PHP';
    require_once dirname(__FILE__) . '/class-ae-search.PHP';
    require_once dirname(__FILE__) . '/update.PHP';
    if( !defined ( 'ET_DOMAIN' ) ){
        define( 'ET_DOMAIN','enginetheme' );
    }
    $ae_private_message = AE_Private_Message_Posttype::getInstance();
    $ae_private_message->init();
    $ae_private_message_action = AE_Private_Message_Actions::getInstance();
    $ae_private_message_action->init();
}
add_action('after_setup_theme','require_plugin_file');

此外,插件的functions.PHP 中有一些函数用于解释重复的函数声明:

if ( !function_exists('ae_private_msg_user_profile') ) {
    function ae_private_msg_user_profile( $user_id ) {

最后,这里是整体文件结构

click for pic of plugin directory

这是一个已知的 wordpress 插件过程,它们会重复加载吗?或者也许是在 wordpress 中使用 Backbone.js 或特别是 ajax 的产物?重申一下,我一次没有看到functions.PHP 包含在主文件中。尽管很容易解决,但我只想了解那里发生的事情。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)