I replicated wordpress.org’s code reference system for my own project.
WordPress provides a template tag
get_the_title() and it’s documented at https://developer.wordpress.org/reference/functions/get_the_title/.
A plugin I just launched provides a template tag
invp_get_the_price() and it’s documented at https://inventorypresser.com/docs/reference/functions/invp_get_the_price/
I learned that the .org docs page content is generated by scanning the project source code, extracting PHPDoc comments, and parsing them into custom post types. The piece of software that does that is https://github.com/WordPress/phpdoc-parser.
Build the Data
- Add PHPDoc comments to all classes and methods in your plugin.
- Use a server that provides the WordPress Command Line Interface.
- Upload your plugin to the site where you want to generate documentation.
- Activate the phpdoc-parser plugin on the same site
- Connect via SSH and run a wp-cli command similar to
wp parser create ../../sites/inventorypress/wp-content/plugins/inventory-presser/ --user=corey
- Replace the path with a path to the plugin for which you want documentation.
- Change the user name to the name of an administrator on your site.
Build the Theme
WordPress.org uses a handful of themes at once. The developer reference pieces live in the wporg-developer theme.
Take Files from wporg-developer
SIDE NOTE: I would like to include wporg-developer in my theme or child theme instead of copying files, but step 2 below makes it hard to use wporg-developer as a dependency. It must be edited. I would enjoy having easy access to updates, and perhaps I could contribute to the theme to add necessary filters someday in the future.
How to download wordpress.org source code
This SVN command will download the whole theme. Paste the command into a terminal or command prompt window:
svn co https://meta.svn.wordpress.org/sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/
Copy these files from wporg-developer into your child theme
- functions.php (comingle it with your functions.php)
- Change option value
wp_parser_root_import_dirto contain a path to the project. For me, this path points to a plugin in the
- Create single.php by taking a copy from the parent theme. Modify it to serve a different content template for the post types populated by phpdoc-parser. Here is a code sample that shows what to change to create a Twenty Twenty-One child theme.
- Optional: Change “View on Trac” links
I added two filters that wrap the URL and the text in the “View on Trac” link below the source code section of each page. These changes allow me to change the links to “View on Github” or wherever I want.
Twenty Twenty-One Child Theme
Download a Twenty Twenty-One child theme with the files listed and mods 1-3 completed: https://github.com/csalzano/twentytwentyone-wporg-child
Also Run This Plugin
SyntaxHighlighter Evolved styles the source code editors.
Missing Source Code
If no source code is displaying, edit the option
wp_parser_root_import_dir to contain a full path to the directory of the plugin. Example: /corey/sites/inventorypress/wp-content/plugins/inventory-presser
My Observations So Far
- I love it, but I didn’t try anything else. Writing code comments is now also writing web pages and a great motivator to do it right.
- phpdoc-parser reruns do not delete items. I need to write code that deletes functions, methods, hooks, and classes that are removed from the project.
I want to write a Github action to automate wp-parser runs on new versions of the plugin. When I push updates to the public repo, a copy of the plugin is written to the webserver, and wp-parser is run to generate new docs.
Automating Updates to Docs
Four months after I published this post, I designed a Github Action to automate the creation and updating of the docs pages. When I push to the master branch of my plugin’s repo, the plugin is deployed to the site on WP Engine where the docs pages are hosted, and the wp-cli command that generates the docs is run.
Here is the workflow file that defines the Action at Github: https://github.com/fridaysystems/inventory-presser/blob/master/.github/workflows/main.yml
It contains one job with two steps that are marked with these comments: