3

I would like to fetch the WordPress functions, methods, classes, and hooks from here: https://developer.wordpress.org/ in JSON format. I know that it is a WordPress site, and that I can use WP API to pull posts and such from https://developer.wordpress.org/wp-json/wp/v2/, but I'm not finding where the functions, etc are located. Or is there a different API I can tap into?

I'm building a discord bot for a WordPress development server, hoping to make a command that will display what a function does and link to the source page.

3
  • 3
    I did this myself a little over a year ago - sadly I was working remotely on a Droplet and neglected to pay a bill - the code is gone O.O. But the REST API is self-documenting. If you read through the JSON in the REST API link you'll find like the https://developer.wordpress.org/wp-json/wp/v2/wp-parser-function/ route for the documentation pages which are auto-parsed from code comments, for example. You can perform a search on those wp-parser post-types.
    – bosco
    Jun 30 at 19:59
  • 2
    I recall finding some oddities in the REST API responses, however - my final solution depended on a mix of REST API, WordPress search XML feed, and HTML parsing. I seem to recall that I could only get the full content of documentation posts through specific routes in the REST API, and that using an XML feed of the general documentation search (e.g. https://developer.wordpress.org/?s=get_posts&post_type%5B%5D=wp-parser-function&post_type%5B%5D=wp-parser-hook&post_type%5B%5D=wp-parser-class&post_type%5B%5D=wp-parser-method) was sometimes more beneficial than the REST API search.
    – bosco
    Jun 30 at 20:05
  • 2
    I believe I mostly used the REST API and search feed to resolve the "subject" of the query to a documentation page URL, then ultimately relied on parsing markup of the indicated documentation page to get and display the contents. Also - check out the <meta> tags in the <head> section of doc pages for more hints - some of the rel="link"s will point to equivalent REST routes. All of that said, as this is not a question specifically regarding the WordPress software itself, it may be considered off-topic on this stack. But feel free to hit me up in the stack chat if you run into more issues
    – bosco
    Jun 30 at 20:09

1 Answer 1

3

It seems to be accessible from:

https://developer.wordpress.org/wp-json/wp/v2/wp-parser-class

https://developer.wordpress.org/wp-json/wp/v2/wp-parser-method

https://developer.wordpress.org/wp-json/wp/v2/wp-parser-hook

https://developer.wordpress.org/wp-json/wp/v2/wp-parser-function

and search with e.g.

https://developer.wordpress.org/wp-json/wp/v2/wp-parser-function?search=get_post_embed

but it's probably best to generate your own with the WP Parser here:

https://github.com/WordPress/phpdoc-parser

This will create custom post types for

  • wp-parser-hook
  • wp-parser-function
  • wp-parser-class
  • wp-parser-method

and related post meta (to e.g. store function input arguments, line numbers, class type, hook type and namespace) and taxonomies (to store info on files, package, since, namespaces) in your own WP install.

Here's the theme that runs on the code reference's site:

https://github.com/WordPress/wordpress.org/tree/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer

There we can see how it displays e.g. a function's source code and a list of a input parameters, using the theme's helper functions get_source() and get_params().

I ran a test with:

cd wp-content
cd plugins
git clone https://github.com/WordPress/phpdoc-parser.git
cd phpdoc-parser/
composer install
wp plugin activate phpdoc-parser
wp parser create /path/to/source/ --user=test

Here's a screenshot from my test install:

enter image description here

Also more DevHub info here:

https://make.wordpress.org/docs/handbook/devhub/

8
  • Ah that's a good thought! Running the parser oneself would really streamline the whole process
    – bosco
    Jun 30 at 20:27
  • @bosco I just noticed your informative comments under the questions too. I don't know the rate limit on the DevHub REST API so having own doc install might be an alternative for those that need to make many queries :-)
    – birgire
    Jun 30 at 20:39
  • 1
    By all means! I had even started implementing a caching mechanism to that end - I can't believe I never thought to just run the parser myself :P
    – bosco
    Jun 30 at 20:47
  • Thank you @birgire!! This is exactly what I was hoping for. :)
    – Aristocles
    Jun 30 at 21:56
  • 1
    Here is the theme that is used github.com/WordPress/wordpress.org/tree/trunk/wordpress.org/… maybe that can help with the extra info for the self hosting case.
    – birgire
    Jun 30 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.