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 page content is generated by scanning a plugin folder and extracting PHPDoc comments from the project’s code. 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. 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. 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.

Leave a comment

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