# WordPress 官方的 Code Reference应该如何使用？

- Link: https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/
- Published: 2022-02-25T06:00:00+08:00
- Author: 白宦成

文章目录

 [Toggle](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#)

 * [搜索你需要的函数、Hooks、Filter、Class](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#%e6%90%9c%e7%b4%a2%e4%bd%a0%e9%9c%80%e8%a6%81%e7%9a%84%e5%87%bd%e6%95%b0%e3%80%81hooks%e3%80%81filter%e3%80%81class)
 * [理解 Code Reference 的内容](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#%e7%90%86%e8%a7%a3_code_reference_%e7%9a%84%e5%86%85%e5%ae%b9)
    - [Description](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#description)
    - [Parameters](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#parameters)
    - [Return](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#return)
    - [Source](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#source)
    - [Related](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#related)
    - [Changelog](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#changelog)
    - [User Contributed Notes](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#user_contributed_notes)
 * [总结](https://www.ixiqin.com/2022/02/25/the-official-code-reference-should-be-how-to-use-the-wordpress/#%e6%80%bb%e7%bb%93)

如果你要开发 WordPress ，必然会用到 WordPress 官方的文档。而在官方文档当中，除了
各种 Handbook ，最重要便是 [**Code Reference**](https://developer.wordpress.org/reference/)。

在使用 Code Reference 的时候，也是存在一些技巧的。

## 搜索你需要的函数、Hooks、Filter、Class

访问 [Code Reference 主页](https://developer.wordpress.org/reference/)，可以看到
有一个搜索框，在这里你可以输入你要搜索的关键词，并在下拉框中筛选出你要用的具体的
函数/Hooks/Filter/Class

![](https://files.ixiqin.com/2022/02/23195527/d2b5ca33bd970f64a6301fa75ae2eb22-20-
1024x486.png)

当然，如果你在开发过程中，明确了你要查询的某个特定的函数，也有一个简单的方式来查询，
只需要将下方的链接中的 FUNCTIONNAME 替换为你要查询的函数名，就可以直接跳转到对应
的函数的详细界面，查看对应的函数的功能。

    ```javascript
    https://developer.wordpress.org/reference/functions/FUNCTIONNAME/
    Code language: JavaScript (javascript)
    ```

如果你查询的是 Hooks、Filter 也有类似的方法，WordPress 的单个函数/Hooks/Filter 的
URL非常的明确，因此，你可以在写代码的时候，随时把你需要查询的函数直接复制出来，替换
浏览器地址栏中的方法名即可。

## 理解 Code Reference 的内容

以 `[download_url](https://developer.wordpress.org/reference/functions/download_url/)`
为例，在 Code Reference 当中，大体上会分为以下几个区域

    ```php
    Description
    Parameters
    Return
    Source
    Related
     - Uses
     - Used By
    Changelog
    User Contributed Notes
     - Feedback
    Code language: PHP (php)
    ```

### Description

![](https://files.ixiqin.com/2022/02/23195527/d2b5ca33bd970f64a6301fa75ae2eb22-21.
png)

这里介绍了这个函数的基本介绍，一些时候，也会在这个区域加入一些使用的 Tips。

不过这个描述并非每一个函数都会有。

### Parameters

![](https://files.ixiqin.com/2022/02/23195527/d2b5ca33bd970f64a6301fa75ae2eb22-22.
png)

参数区域说明了这个函数的入参有哪些，你可以对着入参的介绍调用函数，从而避免传入错误
的参数

### Return

![](https://files.ixiqin.com/2022/02/23195527/d2b5ca33bd970f64a6301fa75ae2eb22-23.
png)

返回值定义了具体的返回值，你可以根据返回值来判断具体的结果。不仅如此，点击里面的
链接，可以跳转到对应的类，查看对应返回结果的类型。

### Source

![](https://files.ixiqin.com/2022/02/23195527/d2b5ca33bd970f64a6301fa75ae2eb22-24.
png)

Source 部分则给你了函数的具体的代码，对于一些你在开发过程中百思不得其解的函数，读
一下这个函数的源码可能会让你茅塞顿开。

对于比较长的函数，点击 Expand 就可以展开看。如果你希望在本地看，左上角也告诉你了
这个函数在具体哪个文件可以查看。你可以直接按图索骥，找到本地的对应函数来查看。

### Related

![](https://files.ixiqin.com/2022/02/23195527/d2b5ca33bd970f64a6301fa75ae2eb22-25.
png)

Related 当中分为两个区块，分别是 Uses 和 Used By ，分别介绍当前这个函数是基于哪些
函数实现的（Uses）和其他哪些函数使用了当前这个函数（Used By）

有了 Uses 和 Used By，你就可以自己封装一个类似的函数，也可以看看其他的函数是如何
调用当前这个函数，心中更有数。

### Changelog

![](https://files.ixiqin.com/2022/02/23195527/d2b5ca33bd970f64a6301fa75ae2eb22-26.
png)

Changelog 当中记录了这个函数的具体的变更历史，你就可以非常方便的去了解到一个函数
的发展历史，可能某个功能你过去用的好好的，最近不能用了，就是因为函数发生了更新。

### User Contributed Notes

![](https://files.ixiqin.com/2022/02/23195527/d2b5ca33bd970f64a6301fa75ae2eb22-27.
png)

在 User Contributed Notes 当中，记录了别人对于这个函数使用的反馈，你可以看到其他
开发者是如何使用这个函数的。当你在使用这个函数/Hook出现问题时，就可以参考一下其他
人贡献的笔记，可能会更好的帮助你来理解这些 Notes。

## 总结

用好 WordPress 自带的 Code Reference 可以帮助大家更好的开发 WordPress 的各种功能，
花点时间了解一下 Code Reference ，相信可以帮到你～
