调用wordpress代码中的外部函数


Call an external function inside wordpress code

wordpress是否忽略外来php代码?我正试图在wordpress的根index.php上这行下面的一个外部类中抛出一个函数调用

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

这是我的功能调用

require_once($_SERVER['DOCUMENT_ROOT']."/modules/Site-Traffic/Site-Traffic.php");
//Create traffic object for tracking who is hitting this page
$site_traffic = new Site_Traffic();
$site_traffic->SetTrafficLog();

以下是此函数中发生的情况。

  • 设置正确的数据库、表、用户名和密码
  • 检查querystring参数中是否存在要在函数中跟踪的特殊情况
  • 如果函数看到一个特殊的参数情况,它会创建一条sql语句,然后调用我的mysqliconnection库
  • 然后,库获取sql语句、数据库、表、用户名和密码,并将站点流量调用插入相应的表格

我在wordpress之外的所有页面上都使用它,它工作得很好,但在index.php页面上它根本不工作。

我能够从wordpress index.php页面中回显sql语句、数据库、表、用户名和密码,所以我知道它已经到了那个地步。

编辑:使问题更加明显和具体

将其放入自定义插件或主题的函数.pp.

以下是我的做法:

创建以下文件(包括目录):/your-wp-root/wp-content/plugins/wpse-site-traffic.php

/**
 * Plugin Name: WPSE Site Traffic
 * Description: Woohoo - Site Traffic moved to a plugin.
 * Version: 1.0
 * Author: Dom
 */
// Load Site_Traffic Class.
require_once( ABSPATH . 'modules/Site-Traffic/Site-Traffic.php' );
/**
 * Create traffic object for tracking who is hitting this page. Hook
 * it into 'plugins_loaded' to run early.
 */
function wpse_site_traffic_load() {
    $site_traffic = new Site_Traffic();
    $site_traffic->SetTrafficLog();
}
add_action( 'plugins_loaded', 'wpse_site_traffic_load' );