PHP包含()用于WordPress插件 - 错误/警告消息

问题描述:

This is my first post on stack overflow, so I apologize if I have violated etiquette or otherwise did something dumb. I'm also a novice programmer as well, so I apologize in advance if this question seems dumb.

Now on to the main point: I'm trying to create a WordPress plugin for a website while our main programmer is on vacation. I've had some success, and now what I need to do is call simplemodal-login from the plugin after the user does a certain action on the webpage. Currently, I am trying this in order to include the files, which are in a separate directory one level up from the plugin in file(I'm going to block out files names to preserve anonymity):

include ("../simplemodal-login/simplemodal-login.php");

I've also tried prefixing the simplemodal-login directory part with /../, ../../, and /../../ . I've also tried

include ("{$_SERVER['DOCUMENT_ROOT']}/[project name]/wp-content/plugins/simplemodal-login");

and some variations of it. Nothing has worked. I keep on getting variations of this error message:

Warning: include(../simplemodal-login/simplemodal-login.php) [function.include]: failed to open stream: No such file or directory in /home/[user name]/public_html/[website name]/[project name]/wp-content/plugins/[my plugin]/[plugin file] on line 29
Warning: include() [function.include]: Failed opening '../simplemodal-login/simplemodal-login.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/[user name]/public_html/[website name]/[project name]/wp-content/plugins/[plugin name]/[plugin file].php on line 29

What am I doing wrong? I simply want to run the simplemodal-login plugin after the user sees the webpage a certain amount of times. How can I include the file properly? I apologize for my inexperience - I've taken an intro course in Java at college (which I did well in) and I've learned a little (I mean LITTLE) bit of PHP this summer as an intern. If anyone could guide me so I don't get that error message when I try to include the files, it would be very helpful.

Thanks!

这是我关于堆栈溢出的第一篇文章,所以如果我违反了礼仪或者做了一些愚蠢的事情,我会道歉。 我也是一名新手程序员,所以如果这个问题看起来很愚蠢,我会事先道歉。 p>

现在谈到要点:我正在尝试创建一个WordPress插件 网站,而我们的主程序员正在度假。 我已经取得了一些成功,现在我需要做的是在用户在网页上执行某项操作后,从插件中调用simplemodal-login。 目前,我正在尝试这个以便包含文件,这些文件位于与文件中的插件相距一级的单独目录中(我将阻止文件名以保持匿名): p>

  include(“../simplemodal-login/simplemodal-login.php");
nn

我也试过为simplemodal添加前缀 - 登录目录部分包含 /../ code>, ../../ code>和 /../../ code>。 我也试过 p>

  include(“{$ _SERVER ['DOCUMENT_ROOT']} / [项目名称] / wp-content / plugins / simplemodal-login”); \  n  code>  pre> 
 
 

及其中的一些变体。 没有任何效果。 我继续收到此错误消息的变体: p>

 警告:include(../ simplemodal-login / simplemodal-login.php)[function.include]:无法打开 stream:第29行/ home / [用户名] / public_html / [网站名称] / [项目名称] / wp-content / plugins / [我的插件] / [插件文件]中没有此类文件或目录
警告:包含 ()[function.include]:无法打开'../simplemodal-login/simplemodal-login.php'(include_path ='。:/ usr / lib / php:/ usr / local / lib / php')  / home / [用户名] / public_html / [网站名称] / [项目名称] / wp-content / plugins / [插件名称] / [插件文件] .php第29行
  code>  pre>  
 
 

我做错了什么? 我只想在用户看到网页一定次数后运行simplemodal-login插件。 如何正确包含文件? 我为自己缺乏经验而道歉 - 我在大学里学习了Java的入门课程(我做得很好),今年夏天我作为一名实习生学到了一些PHP(我的意思是LITTLE)。 如果有人可以引导我,所以当我尝试包含文件时,我没有收到该错误消息,这将非常有用。 p>

谢谢! p> div >

If the file you are trying to include is one directory level up from the plugin file you are writing the code in, a portable solution would be to use:

include dirname(__FILE__) . '/../file.php';

dirname(__FILE__) returns the path of the file that that statement is executed in (regardless of whether or not it has been included itself). I use this in Wordpress plugins without any issues.

Hope that helps.