admin管理员组文章数量:1406926
What's the difference between these two code snippets, and which one should I use for a WordPress plugin?
require_once(dirname(__FILE__).'/inc/canagan_admin_functions.php');
require_once( '/inc/canagan_admin_functions.php' );
What's the difference between these two code snippets, and which one should I use for a WordPress plugin?
require_once(dirname(__FILE__).'/inc/canagan_admin_functions.php');
require_once( '/inc/canagan_admin_functions.php' );
Share
Improve this question
edited Nov 20, 2019 at 14:20
the
1,5682 gold badges13 silver badges32 bronze badges
asked Nov 30, 2011 at 2:07
ShoeboxShoebox
3342 gold badges6 silver badges15 bronze badges
1
- 1 The first one, because the second one won't work. – Otto Commented Nov 30, 2011 at 2:12
1 Answer
Reset to default 11The first one is like saying...
Include the file found in the
inc
directory, above the directory where this file is located.
The second statement is saying...
in the server root (
/
) look in theinc
folder forcanagan_admin_functions.php
and include it.
The first one will work, the second won't. In the second you're looking in the root of the entire file system, where you file certainly won't be.
As a side note, whenever I start a plugin that's going to span multiple files, and/or enqueues CSS or JS files, I usually set up a few constant to help me along the way. Here's an example.
<?php
define( 'CD_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'CD_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
plugin_dir_path
and plugin_dir_url
are nice because they always return paths/urls with trailing slashes.
Then to include/require files, I use the constants. Keeps from having to call dirname(__FILE__)
on every include.
You can then use the constants to include files...
require_once( CD_PLUGIN_PATH . 'path/to/file.php' );
or to enqueue scripts/styles...
<?php
add_action( 'wp_enqueue_scripts', 'wpse35088_enqueue' );
function wpse35088_enqueue()
{
wp_enqueue_script( 'wpse35088-script', CD_PLUGIN_URL . 'path/to/script.js' );
}
本文标签: Using requireonce in a Plugin
版权声明:本文标题:Using require_once in a Plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744987744a2636193.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论