How to find Code Snippets in WordPress Core

There are a lot of places to find WordPress code snippets on the Web. But to find the source of a function inside WordPress Core itself you need to look elsewhere.

Codex

If you don’t know the name of the function, First you need to figure out what feature that function could belong to. For that there is of course Codex. Codex also includes guides for end-users of WordPress. So your starting point needs to be the Developer Docs section.

The Google powered custom search comes in handy to narrow down the search. If you already know the name of the function you are looking for, the Function Reference is another good starting point.

Wpseek

Once you have narrowed down the function(s) you can use wpseek to search for more details on that function. For a query like add_shortcode it includes the source code of the function and links to take you straight to the line number in the source file of the function on WordPress Trac or Github. It also includes some useful features like Similar Functions and User Discussions on wordpress.org around that function.

Silver Searcher

The true code wranglers though will want to go straight to the source code! For searching within your WordPress directory, You can use grep or git grep, but the output that these commands produce is more suited towards scripting.

For viewing purposes, A better alternative is Silver Searcher. And like its comic book superhero namesake, It is really fast. Searching for a function in the entire WordPress codebase takes only a fraction of a second! Installation is straightforward on Macs and Linux.

For instance, To search for places where the add_shortcode function is found in WordPress Core you can use,

ag add_shortcode /path/to/wordpress

The results for this query are conveniently grouped by files that matched the function add_shortcode alongside their matched line numbers.

wp-includes/shortcodes.php
60: * add_shortcode('footag', 'footag_func');
75: * add_shortcode('bartag', 'bartag_func');
85: * add_shortcode('baztag', 'baztag_func');
95:function add_shortcode($tag, $func) {</p>

<p>wp-includes/media.php
611:add_shortcode('wp_caption', 'img_caption_shortcode');
612:add_shortcode('caption', 'img_caption_shortcode');
706:add_shortcode('gallery', 'gallery_shortcode');
1054:add_shortcode( 'audio', 'wp_audio_shortcode' );
1251:add_shortcode( 'video', 'wp_video_shortcode' );</p>

<p>wp-includes/class-wp-embed.php
23:     add_shortcode( 'embed', '__return_false' );
44:  * @uses add_shortcode()
57:     add_shortcode( 'embed', array( $this, 'shortcode' ) );

For Vim users, ag.vim is a Vim plugin that integrates Silver Searcher with Vim’s quickfix window for even faster searching.