Mimic the WordPress.org developer reference for your project

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

  1. Add PHPDoc comments to all classes and methods in your plugin.
  2. Use a server that provides the WordPress Command Line Interface.
  3. Upload your plugin to the site where you want to generate documentation.
  4. Activate the phpdoc-parser plugin on the same site
  5. Connect via SSH and run a wp-cli command similar to wp parser create ../../sites/inventorypress/wp-content/plugins/inventory-presser/ --user=corey
    1. Replace the path with a path to the plugin for which you want documentation.
    2. 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

  • /inc
  • /js
  • /reference
  • /stylesheets
  • content-reference.php
  • content-reference-archive.php
  • functions.php (comingle it with your functions.php)

Theme Mods

  1. Change option value wp_parser_root_import_dir to contain a path to the project. For me, this path points to a plugin in the wp-content/plugins folder.
  2. Change get_template_directory_uri() calls to get_stylesheet_directory_uri() calls
  3. 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.
  4. 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.
    1. wp-parser_source_url_base line 1173 of template-tags.php
    2. wp-parser_view_on_trac_link_text in a few places in template-source.php

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.

Troubleshooting

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

  1. 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.
  2. Done! See Add-on Plugins 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.
  3. Done! See Automating Updates to Docs 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:

Add-on Plugins

I wrote two add-on plugins to modify the behavior of phpdoc-parser.

  1. phpdoc-ignorer instructs phpdoc-parser to ignore the /vendor folder in my plugin
  2. phpdoc-deleter keeps track of every post created or updated by phpdoc-parser. After the run is complete, it deletes all posts that were not touched and then empty taxonomy terms.

Leave a comment

Your email address will not be published. Required fields are marked *