WordPress路径相关函数总结

2016年3月26日建站历程 原创教程35,455

经常维护网站或者编辑php代码的时候,都会使用路径变量。不同的网站有不同的相对和绝对路径。之前都是百度查找,不过每次使用都很麻烦,记不住。这次更新VipSystem会员系统插件,顺便把路径的相关函数粘贴过来。希望有所帮助文章源自狐狸影视城-https://fox-studio.net/31067.html

 文章源自狐狸影视城-https://fox-studio.net/31067.html

前言

与WordPress打交道,经常遇到的一个问题就是获取路径,包括URL路径和服务器路径,在主题或插件中引用js或css文件需要URL地址,而include一些文件时则需要服务器路径。在WordPress中,不能认定wp-content目录一定位于/wp-content下,也不能认为admin的地址一定是/wp-admin,为了避免错误,了解WordPress中与获取路径相关的函数很重要。文章源自狐狸影视城-https://fox-studio.net/31067.html

以下均假设WordPress站点安装在http://www.fox-studio.net下文章源自狐狸影视城-https://fox-studio.net/31067.html

 文章源自狐狸影视城-https://fox-studio.net/31067.html

站点路径相关函数

HOME_URL() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

返回站点路径,相当于后台设置->常规中的"站点地址(URL)"。文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. $url = home_url();
  2. echo $url;
  3. //输出: http://www.fox-studio.net
  1. $url = home_url('/images/');
  2. echo $url;
  3. 输出:http://www.fox-studio.net/images/

 文章源自狐狸影视城-https://fox-studio.net/31067.html

SITE_URL() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

如果WordPress安装在域名根目录下,则该函数与home_url()相同。文章源自狐狸影视城-https://fox-studio.net/31067.html

如果WordPress安装在子目录下,例如http://www.solagirl.net/wordpress,则site_url()返回WordPress实际安装地址,相当于后台->设置->常规中的“WordPress 地址(URL)”。文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. $url = site_url();
  2. echo $url;
  3. //假设WordPress安装在http://www.fox-studio.net/wordpress下
  4. //输出:http://www.fox-studio.net/wordpress

 文章源自狐狸影视城-https://fox-studio.net/31067.html

ADMIN_URL() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

返回后台地址,传递参数后也可返回后台menu的地址。文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. $url = admin_url();
  2. echo $url;
  3. //输出:http://www.fox-studio.net/wp-admin/

 文章源自狐狸影视城-https://fox-studio.net/31067.html

CONTENT_URL() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

返回实际的wp-content目录,如果是默认安装,且装在根目录下,则如下所示文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. $url = content_url();
  2. echo $url;
  3. //输出:http://www.fox-studio.net/wp-content

如果在wp-config.php中改变了wp-content目录的位置,则该函数会返回正确地址,例如wp-config.php中如下定义文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. define('WP_CONTENT_DIR', '/home/user/public_html/cdn');
  2. define('WP_CONTENT_URL', 'http://sola-cdn.me');

则content_url()的返回值为文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. http://sola-cdn.me

 文章源自狐狸影视城-https://fox-studio.net/31067.html

INCLUDES_URL() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

返回当前WordPress站点存放核心文件的目录wp-includes的地址,可以带一个$path作为参数。文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. $url = includes_url( '/js/');
  2. echo $url;
  3. //输出:http://www.fox-studio.net/wp-includes/js/

 文章源自狐狸影视城-https://fox-studio.net/31067.html

WP_UPLOAD_DIR() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

返回WordPress上传目录的地址,是一个数组,包含一系列与上传地址相关的信息。文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. <?php $upload_dir = wp_upload_dir(); ?>

提供如下信息给你文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. * 'path' - 上传目录的服务器绝对路径,通常以反斜杠(/)开头
  2. * 'url' - 上传目录的完整URL
  3. * 'subdir' - 子目录名称,通常是以年/月形式组织的目录地址,例如/2016/03
  4. * 'basedir' - 上传目录的服务器绝对路径,不包含子目录
  5. * 'baseurl' - 上传目录的完整URL,不包含子目录
  6. * 'error' - 报错信息.

例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. $upload_dir = wp_upload_dir();
  2. echo $upload_dir['baseurl'];
  3. //输出:http://www.fox-studio.net/wp-content/uploads

 文章源自狐狸影视城-https://fox-studio.net/31067.html

主题路径相关函数

GET_THEME_ROOT_URI() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

获取存放主题的目录URI文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo get_theme_root_uri();
  2. //输出:http://www.fox-studio.net/wp-content/themes

 文章源自狐狸影视城-https://fox-studio.net/31067.html

GET_THEME_ROOT() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

获取存放主题的目录的服务器绝对路径文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo get_theme_root();
  2. //输出:/home/user/public_html/wp-content/themes

 文章源自狐狸影视城-https://fox-studio.net/31067.html

GET_THEME_ROOTS() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

获取主题目录的目录名称,如果你的主题目录是/wp-content/themes,则文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo get_theme_roots();
  2. //输出:/themes

 文章源自狐狸影视城-https://fox-studio.net/31067.html

GET_STYLESHEET_DIRECTORY() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

获取当前启用的主题目录的服务器绝对路径,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. /home/user/public_html/wp-content/themes/twentyeleven

可以用来include文件,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. <?php include( get_stylesheet_directory() . '/includes/myfile.php'); ?>

 文章源自狐狸影视城-https://fox-studio.net/31067.html

GET_STYLESHEET_DIRECTORY_URI() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

获取当前启用的主题目录的URI,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo get_stylesheet_directory_uri();
  2. //输出:http://www.fox-studio.net/wp-content/themes/twentyeleven

可以使用在需要主题目录URI的场合,例如图片文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. <img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />

 文章源自狐狸影视城-https://fox-studio.net/31067.html

GET_TEMPLATE_DIRECTORY_URI() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录URI,用法与get_stylesheet_directory_uri()类似。文章源自狐狸影视城-https://fox-studio.net/31067.html

 文章源自狐狸影视城-https://fox-studio.net/31067.html

GET_TEMPLATE_DIRECTORY() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录的服务器绝对路径,用法与get_stylesheet_directory()类似。文章源自狐狸影视城-https://fox-studio.net/31067.html

 文章源自狐狸影视城-https://fox-studio.net/31067.html

GET_TEMPLATE() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

获取当前启用主题的主题目录名称,例如现在启用的主题为twentyeleven,则文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo get_stylesheet();
  2. //输出:twentyeleven

 文章源自狐狸影视城-https://fox-studio.net/31067.html

GET_STYLESHEET() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

获取当前启用主题的主题目录名称,与get_template()的区别是,如果用了child theme,则返回child theme的目录名称。文章源自狐狸影视城-https://fox-studio.net/31067.html

 文章源自狐狸影视城-https://fox-studio.net/31067.html

插件路径相关函数

PLUGINS_URL() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

获取当前插件的目录的URI,例如一个插件位于/wp-content/plugins/myplugin下,该目录下放有插件的主文件名为myplugin.php,在myplugin.php中执行下面的代码,结果如下文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo plugins_url();
  2. //输出:http://www.fox-studio.net/wp-content/plugins
  1. echo plugins_url('',__FILE__);
  2. //输出:http://www.fox-studio.net/wp-content/plugins/myplugin
  1. echo plugins_url('js/myscript.js',__FILE__);
  2. //输出:http://www.fox-studio.net/wp-content/plugins/myplugin/js/myscript.js

 文章源自狐狸影视城-https://fox-studio.net/31067.html

PLUGIN_DIR_URL() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

返回当前插件的目录URI,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo plugin_dir_url( __FILE__ );
  2. //输出:http://www.fox-studio.net/wp-content/plugins/myplugin/

注意结尾有反斜杠。文章源自狐狸影视城-https://fox-studio.net/31067.html

 文章源自狐狸影视城-https://fox-studio.net/31067.html

PLUGIN_DIR_PATH() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

返回当前插件目录的服务器绝对路径,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo plugin_dir_path( __FILE__ );
  2. //输出:/home/user/public_html/wp-content/plugins/myplugin/

可以用来引用文件,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. <?php
  2. define( 'MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );
  3. require MYPLUGINNAME_PATH . 'includes/class-metabox.php';
  4. require MYPLUGINNAME_PATH . 'includes/class-widget.php';
  5. ?>

 文章源自狐狸影视城-https://fox-studio.net/31067.html

PLUGIN_BASENAME() >>文章源自狐狸影视城-https://fox-studio.net/31067.html

返回调用该函数的插件文件名称(包含插件路径)文章源自狐狸影视城-https://fox-studio.net/31067.html

例如在插件myplugin下的myplugin.php文件中调用该函数,结果如下文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo plugin_basename(__FILE__);
  2. //输出:myplugin/myplugin.php

如果在myplugin/include/test.php文件中调用(test.php通过include引用到myplugin.php中),结果如下文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. echo plugin_basename(__FILE__);
  2. //输出:myplugin/include/test.php

 文章源自狐狸影视城-https://fox-studio.net/31067.html

路径相关常量

WordPress中还有一组用define定义的常量代表路径。文章源自狐狸影视城-https://fox-studio.net/31067.html

WP_CONTENT_DIR文章源自狐狸影视城-https://fox-studio.net/31067.html

wp-content目录的服务器绝对路径,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. /home/user/public_html/wp-content

 文章源自狐狸影视城-https://fox-studio.net/31067.html

WP_CONTENT_URL文章源自狐狸影视城-https://fox-studio.net/31067.html

wp-content目录的URI地址,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. http://www.fox-studio.net/wp-content

 文章源自狐狸影视城-https://fox-studio.net/31067.html

WP_PLUGIN_DIR文章源自狐狸影视城-https://fox-studio.net/31067.html

插件目录的服务器绝对路径,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. /home/user/public_html/wp-content/plugins

 文章源自狐狸影视城-https://fox-studio.net/31067.html

WP_PLUGIN_URL文章源自狐狸影视城-https://fox-studio.net/31067.html

插件目录的URI地址,例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. http://www.fox-studio.net/wp-content/plugins

 文章源自狐狸影视城-https://fox-studio.net/31067.html

TEMPLATEPATH文章源自狐狸影视城-https://fox-studio.net/31067.html

当前启用主题目录的服务器绝对路径,相当于get_template_directory()例如文章源自狐狸影视城-https://fox-studio.net/31067.html

  1. /home/user/public_html/wp-content/themes/twentyeleven

 文章源自狐狸影视城-https://fox-studio.net/31067.html

STYLESHEETPATH文章源自狐狸影视城-https://fox-studio.net/31067.html

当前启用主题目录的服务器绝对路径,相当于get_stylesheet_directory(),与TEMPLATEPATH的区别在于如果使用child theme,该常量指向child theme目录。文章源自狐狸影视城-https://fox-studio.net/31067.html

weinxin
千年骚狐
  • 本文由 发表于 2016年3月26日
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
评论:3   其中:访客  2   博主  1
    • 912884156
      912884156 1

      狐狸 你建站的空间这个问题 是怎么解决的?
      需要多大的空间合适?
      有没有好的减小成本的办法?

    匿名

    发表评论

    匿名网友 填写信息

    :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

    确定