As a WordPress developer that creates highly customized products for my clients, I found myself running into the same problem over and over again when attempting to create PHP scripts within the WordPress framework. I love using the built-in functionality of WordPress, but I kept running into the same error messages over and over: “call to undefined method…” or “failed to open stream.”
作为一个为客户创建高度定制化产品的WordPress开发人员,我发现自己一再尝试在WordPress框架中创建PHP脚本时遇到同样的问题。 我喜欢使用WordPress的内置功能,但是我一遍又一遍地遇到相同的错误消息:“调用未定义的方法……”或“无法打开流”。
Then you go on a hunt to find the proper files that you need to include, require, include_once, or require_once, depending upon your needs. Once you find the right file, usually some PHP document in the wp-includes folder, for example, you have to include/require it at the beginning of your script — often only to find another “call to undefined method…”
然后,根据需要,继续查找需要包含,require,include_once或require_once的适当文件。 例如,一旦找到正确的文件(通常是wp-includes文件夹中的一些PHP文档),您就必须在脚本的开头包含/要求它-通常仅是找到另一个“对未定义方法的调用……”
To save time and let you start creating code now, not after hours (or days) of Googling a solution, I created a simple script with which I start all my custom files. It handles all the proper files I need for 99% of my development.
为了节省时间,让你现在开始创建代码,而不是谷歌搜索后的溶液小时(或几天),我创建了一个简单的脚本,我开始我所有的自定义文件。 它处理了我99%的开发工作所需的所有适当文件。
I only recommend using this while you are developing your plugin, themes, or scripts and then later, before going to production, finding which files you actually need. I do not recommend you keep this script running all the time as it makes unnecessary calls for files you likely don’t need.
我仅建议您在开发插件 ,主题或脚本时使用此功能,然后再在生产之前查找实际需要的文件。 我不建议您一直保持该脚本运行,因为它会不必要地调用您可能不需要的文件。
I’ll walk you through this script so that you can see what I’m doing and why. This can also work as a reference tool for you later.
我将引导您完成此脚本,以便您了解我在做什么以及为什么。 以后还可以用作参考工具。
I like to always have the ABSPATH defined, but it’s hit or miss with some scripts and WordPress installs. I used to use the simple if test to check whether or not the ABSPATH is defined, but that doesn’t always work out either:
我总是喜欢定义ABSPATH,但是它会因某些脚本和WordPress安装而受到影响。 我曾经使用简单的if测试来检查是否定义了ABSPATH,但这并不总是可行的:
[sourcecode language=”php”]
[源代码语言=“ php”]
/* NOTE: This is what I do NOT do any more */
/ *注意:这是我不再做的* /
// Define the ABSPATH
//定义ABSPATH
if ( !defined(‘ABSPATH’) ) }
如果(!defined('ABSPATH'))}
define(‘ABSPATH’, dirname(___FILE___) . ‘/’);
define('ABSPATH',dirname(___ FILE___)。'/');
}
}
[/sourcecode]
[/源代码]
Instead, I create my own path to my WordPress install that is far more stable:
相反,我创建了自己的WordPress安装路径,该路径更加稳定:
[sourcecode language=”php”]
[源代码语言=“ php”]
$location = $_SERVER[‘DOCUMENT_ROOT’];
$ location = $ _SERVER ['DOCUMENT_ROOT'];
include ($location . ‘/wp-config.php’); include ($location . ‘/wp-load.php’); include ($location . ‘/wp-includes/pluggable.php’);
包括($ location。'/ wp-config.php'); 包括($ location。'/wp-load.php'); 包括($ location。'/wp-includes/pluggable.php');
[/sourcecode]
[/源代码]
This assumes your WordPress install is in the root of your website i.e. www.yoursite.com
假设您的WordPress安装位于网站的根目录下,即www.yoursite.com
If that’s not the case, simply add the string to your $location variable like this:
如果不是这种情况,只需将字符串添加到$ location变量中,如下所示:
[sourcecode language=”php”]
[源代码语言=“ php”]
$location = $_SERVER[‘DOCUMENT_ROOT’] . ‘/your-wp-install’;
$ location = $ _SERVER ['DOCUMENT_ROOT']。 '/ your-wp-install';
[/sourcecode]
[/源代码]
In my experience, there are three files that cause all my problems when it comes to setting up my scripts. I call these my “Big Three” because once I include or require them, most of my problems go away when it comes to the “call to undefined method” errors. They are:
以我的经验,在设置脚本时,有三个文件会引起我的所有问题。 我称它们为“三大”,因为一旦包含或要求它们,当涉及“调用未定义方法”错误时,我的大部分问题就会消失。 他们是:
wp-config.php wp-config.php pluggable.php Pluggable.php wp-load.php wp-load.phpOnce you have these, you’ll find most of your errors go away.
掌握这些内容后,您会发现大部分错误都会消失。
[sourcecode language=”php”]
[源代码语言=“ php”]
// Get the Big Three
//获得三巨头
include (ABSPATH . ‘wp-config.php’);
包括(ABSPATH。'wp-config.php');
include (ABSPATH . ‘wp-load.php’);
包括(ABSPATH。'wp-load.php');
include (ABSPATH . ‘wp-includes/pluggable.php’);
包括(ABSPATH。'wp-includes / pluggable.php');
[/sourcecode]
[/源代码]
Another helpful thing to do is to make sure that your $wpdb is a global variable. This allows any functions you create to have access to the WordPress database query features. This can be a huge time saver.
要做的另一件事是确保$ wpdb是全局变量。 这使您创建的任何功能都可以访问WordPress数据库查询功能。 这可以节省大量时间。
Because you included the wp-config.php and wp-db.php files, you shouldn’t have any problems getting $wpdb to work properly, but I still like to run an if test to check first:
因为您包括了wp-config.php和wp-db.php文件,所以使$ wpdb正常工作应该没有任何问题,但是我仍然想运行一个if测试来首先检查:
[sourcecode language=”php”]
[源代码语言=“ php”]
// Get the Big Four
//获得四大
global $wpdb;
全局$ wpdb;
if( !isset($wpdb) )
if(!isset($ wpdb))
{
{
include ($location . ‘/wp-config.php’);
包括($ location。'/ wp-config.php');
include ($location . ‘wp-includes/wp-db.php’);
包括($ location。'wp-includes / wp-db.php');
}
}
[/sourcecode]
[/源代码]
If you’re starting with a fresh install of WordPress, you only have one admin user. So, there’s no point in trying to connect to the WordPress database and query users. I built this simple test to see if we are connecting properly. If so, you’ll see your name and email address registered with WordPress in a table:
如果您从全新安装WordPress开始,则只有一个管理员用户。 因此,尝试连接到WordPress数据库并查询用户没有任何意义。 我建立了这个简单的测试,以查看我们是否正确连接。 如果是这样,您会在表格中看到在WordPress中注册的姓名和电子邮件地址:
[sourcecode language=”php”]
[源代码语言=“ php”]
/* Test above include statements are working by listing all the admins and their email addresses in a table. */
/ *通过在表格中列出所有管理员及其电子邮件地址,上面的测试include语句起作用。 * /
// Start Test $args = array( //set up the query to only get admins ‘role’ => ‘Administrator’, ‘fields’ => ‘all_with_meta’ );
//开始测试$ args = array(//将查询设置为仅获取管理员'role'=>'Administrator','fields'=>'all_with_meta');
$query = get_users( $args ); //use the get_users call
$ query = get_users($ args); //使用get_users调用
// Set up the table echo ‘<table cellpadding="0" cellspacing="0" border="1"><tr><thead><th>Nice Name</th><th>Email</th></thead></tr>’; // For each of the admins, make a table row foreach ($query as $query) { echo ‘<td>’ . $query->user_nicename . ‘</td><td><a href="mailto:’ . $query->user_email . ‘">’ . $query->user_email . ‘</a></td></tr>’;
//设置表echo'<table cellpadding =“ 0” cellspacing =“ 0” border =“ 1”> <tr> <thead> <th>好的名称</ th> <th>电子邮件</ th> < / thead> </ tr>'; //对于每个管理员,为每个表创建一行表($ query as $ query){echo'<td>'。 $ query-> user_nicename。 '</ td> <td> <ahref="mailto:'。$query->user_email。'“>'。 $ query-> user_email。 '</a> </ td> </ tr>';
} //End foreach echo ‘</table>’; // close the table tag
} //结束foreach echo'</ table>'; //关闭表格标签
// End Test – comment out or delete from here to the above "Start Test"
//结束测试–注释掉或从此处删除上面的“开始测试”
[/sourcecode]
[/源代码]
If the test works and you see your name and email address, just delete our comment out the test script and start coding away!
如果测试成功,并且您看到自己的姓名和电子邮件地址,只需删除测试脚本中的注释,然后开始编码即可!
You can drop this PHP file anywhere within the WordPress folders and it will work. So, for initial testing and programming, this script can save you tons of time otherwise spent troubleshooting basic technical problems.
您可以将此PHP文件拖放到WordPress文件夹内的任何位置,它将起作用。 因此,对于初始测试和编程,此脚本可以为您节省大量时间,否则将您花费在排除基本技术问题上。
Again, you may want to do some detail work once your functions work properly and only include the absolutely necessary files. If you don’t need to include the wp-config.php file, for example, don’t! Just comment out the code, line by line, and see if it works properly. Keep what you need, lose the rest.
同样,一旦功能正常运行,您可能需要做一些细节工作,只包括绝对必要的文件。 例如,如果您不需要包括wp-config.php文件,那就不要! 只需逐行注释掉代码,然后查看其是否正常运行。 保留您所需要的,剩下的就丢掉。
Hopefully this will save you a bunch of time and let you focus on coding, rather than basic mapping of required files for WordPress development.
希望这可以节省您很多时间,让您专注于编码,而不是WordPress开发所需文件的基本映射。
Here’s the complete script for easy reference. Just copy, save the file wherever needed, and have some fun getting work done:
这是完整的脚本,以方便参考。 只需复制,将文件保存在任何需要的地方,即可完成工作,并从中获得一些乐趣:
[sourcecode language=”php”]
[源代码语言=“ php”]
<?php /* Justyn’s Magic Includer
<?php / *贾斯汀的魔法包含者
Assumes you are using the root folder of your server for your WordPress install. If not, just add the sub-folder where for WordPress you are using.
假设您正在使用服务器的根文件夹进行WordPress安装。 如果没有,只需将子文件夹添加到您正在使用的WordPress的位置。
For example, add the sub-folder to the $location variable: $location = $_SERVER[‘DOCUMENT_ROOT’] . ‘/your-sub-folder’; */
例如,将子文件夹添加到$ location变量:$ location = $ _SERVER ['DOCUMENT_ROOT']。 '/您的子文件夹'; * /
$location = $_SERVER[‘DOCUMENT_ROOT’];
$ location = $ _SERVER ['DOCUMENT_ROOT'];
include ($location . ‘/wp-config.php’); include ($location . ‘/wp-load.php’); include ($location . ‘/wp-includes/pluggable.php’);
包括($ location。'/ wp-config.php'); 包括($ location。'/wp-load.php'); 包括($ location。'/wp-includes/pluggable.php');
global $wpdb;
全局$ wpdb;
if( !isset($wpdb) ) { include ($location . ‘/wp-config.php’); include ($location . ‘/wp-includes/wp-db.php’); }
if(!isset($ wpdb)){include($ location。'/wp-config.php'); 包括($ location。'/wp-includes/wp-db.php'); }
/* Test above include statements are working by listing all the admins and their email addresses in a table. */
/ *通过在表格中列出所有管理员及其电子邮件地址,上面的测试include语句起作用。 * /
// Start Test $args = array( //set up the query to only get admins ‘role’ => ‘Administrator’, ‘fields’ => ‘all_with_meta’ );
//开始测试$ args = array(//将查询设置为仅获取管理员'role'=>'Administrator','fields'=>'all_with_meta');
$query = get_users( $args ); //use the get_users call
$ query = get_users($ args); //使用get_users调用
// Set up the table echo ‘<table cellpadding="0" cellspacing="0" border="1"><tr><thead><th>Nice Name</th><th>Email</th></thead></tr>’; // For each of the admins, make a table row foreach ($query as $query) { echo ‘<td>’ . $query->user_nicename . ‘</td><td><a href="mailto:’ . $query->user_email . ‘">’ . $query->user_email . ‘</a></td></tr>’;
//设置表echo'<table cellpadding =“ 0” cellspacing =“ 0” border =“ 1”> <tr> <thead> <th>好的名称</ th> <th>电子邮件</ th> < / thead> </ tr>'; //对于每个管理员,为每个表创建一行表($ query as $ query){echo'<td>'。 $ query-> user_nicename。 '</ td> <td> <ahref="mailto:'。$query->user_email。'“>'。 $ query-> user_email。 '</a> </ td> </ tr>';
} //End foreach echo ‘</table>’; // close the table tag
} //结束foreach echo'</ table>'; //关闭表格标签
// End Test – comment out or delete from here to the above "Start Test"
//结束测试–注释掉或从此处删除上面的“开始测试”
?>
?>
[/sourcecode]
[/源代码]
Would you like to delve a little deeper into PHP? Check out our partner site, PHPMaster.com.
您想更深入地研究PHP吗? 查看我们的合作伙伴网站PHPMaster.com 。
翻译自: https://www.sitepoint.com/the-ultimate-includerequire-script-for-wordpress/
相关资源:jdk-8u281-windows-x64.exe