爱情花园∷与爱同行
男主人
男主人: 歪歪
女主人
女主人: 慧凡
I Love Jia » 爱情花园 » 正文

WordPress路径相关函数总结

歪歪 爱情花园 1 Comment

转载:http://www.solagirl.net/wordpress-path-related-functions.html

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

以下均假设WordPress站点安装在http://www.solagirl.net下

站点路径相关函数

home_url() >>

返回站点路径,相当于后台设置->常规中的”站点地址(URL)“。

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

site_url() >>

如果WordPress安装在域名根目录下,则该函数与home_url()相同。

如果WordPress安装在子目录下,例如http://www.solagirl.net/wordpress,则site_url()返回WordPress实际安装地址,相当于后台->设置->常规中的“WordPress 地址(URL)”。

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

admin_url() >>

返回后台地址,传递参数后也可返回后台menu的地址

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

content_url() >>

返回实际的wp-content目录,如果是默认安装,且装在根目录下,则如下所示

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

如果在wp-config.php中改变了wp-content目录的位置,则该函数会返回正确地址,例如wp-config.php中如下定义

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

则content_url()的返回值为

1
http://sola-cdn.me

includes_url() >>

返回当前WordPress站点存放核心文件的目录wp-includes的地址,可以带一个$path作为参数。

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

wp_upload_dir() >>

返回WordPress上传目录的地址,是一个数组,包含一系列与上传地址相关的信息。

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

提供如下信息给你

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

例如

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

主题路径相关函数

get_theme_root_uri() >>

获取存放主题的目录URI

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

get_theme_root() >>

获取存放主题的目录的服务器绝对路径

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

get_theme_roots() >>

获取主题目录的目录名称,如果你的主题目录是/wp-content/themes,则

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

get_stylesheet_directory() >>

获取当前启用的主题目录的服务器绝对路径,例如

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

可以用来include文件,例如

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

get_stylesheet_directory_uri() >>

获取当前启用的主题目录的URI,例如

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

可以使用在需要主题目录URI的场合,例如图片

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

get_template_directory_uri() >>

如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录URI,用法与get_stylesheet_directory_uri()类似。

get_template_directory() >>

如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录的服务器绝对路径,用法与get_stylesheet_directory()类似。

get_template() >>

获取当前启用主题的主题目录名称,例如现在启用的主题为twentyeleven,则

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

get_stylesheet() >>

获取当前启用主题的主题目录名称,与get_template()的区别是,如果用了child theme,则返回child theme的目录名称。

插件路径相关函数

plugins_url() >>

获取当前插件的目录的URI,例如一个插件位于/wp-content/plugins/myplugin下,该目录下放有插件的主文件名为myplugin.php,在myplugin.php中执行下面的代码,结果如下

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

plugin_dir_url() >>

返回当前插件的目录URI,例如

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

注意结尾有反斜杠。

plugin_dir_path() >>

返回当前插件目录的服务器绝对路径,例如

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

可以用来引用文件,例如

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

plugin_basename() >>

返回调用该函数的插件文件名称(包含插件路径)

例如在插件myplugin下的myplugin.php文件中调用该函数,结果如下

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

如果在myplugin/include/test.php文件中调用(test.php通过include引用到myplugin.php中),结果如下

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

路径相关常量

WordPress中还有一组用define定义的常量代表路径。

WP_CONTENT_DIR

wp-content目录的服务器绝对路径,例如

1
/home/user/public_html/wp-content

WP_CONTENT_URL

wp-content目录的URI地址,例如

1
http://www.solagirl.net/wp-content

WP_PLUGIN_DIR

插件目录的服务器绝对路径,例如

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

WP_PLUGIN_URL

插件目录的URI地址,例如

1
http://www.solagirl.net/wp-content/plugins

TEMPLATEPATH

当前启用主题目录的服务器绝对路径,相当于get_template_directory()例如

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

STYLESHEETPATH

当前启用主题目录的服务器绝对路径,相当于get_stylesheet_directory(),与TEMPLATEPATH的区别在于如果使用child theme,该常量指向child theme目录。

标签:程序源码
上一篇
下一篇

About Author

歪歪

Related Posts

  • 只允许文章作者编辑文章(WordPress情侣博客原理)

    只允许文章作者编辑文章(WordPress情侣博客原理)

    2016-08-18
  • 能答出这20个问题,你就可以创业了!要做好SWOT分析

    能答出这20个问题,你就可以创业了!要做好SWOT分析

    2015-01-27
  • 成功是一种习惯,失败也是一种习惯

    成功是一种习惯,失败也是一种习惯

    2014-05-28

1 Comment

  1. mr林 Reply
    2014-05-10 at 15:42:05

    请问一下,你们的后台账号和密码是什么:我下载了爱情花园的博客程序安装了,但没说账号和密码?
    还有wordpress主题也用不了。

回复 mr林 取消回复

资源共享

  • 爱情花园情侣博客源码下载
  • WORDPRESS情侣博客设计开发原理
  • 爱情花园情侣博客留言薄

站内搜索

爱情花园微信公众号

I Love Jia微信公众号

爱情花园宣言

两人相恋是上天注定的缘,爱情之树的成长少不了爱心的浇灌,和心爱的Ta共同来建设属于你们俩的爱情花园,让你们的爱情之树茁壮成长。

交流话题分为:1、公开话题(任何网友都可浏览);2、私房话(男/女主人交流)3、日记(男/女主人特权)。

Copyright © 2025 I Love Jia 版权所有. 湘ICP备06006525号