RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)

Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)

In a couple of scheduled articles we had a need to evaluate syntax highlighted PHP code to determine the source of identified functions (PHP, WordPress, or our own) so we could linkify them back to the relevant page in the WordPress or PHP manual. Comparing the function against a large array (to determine where the function came from) proved problematic because the array came in at over 11,000 lines (for a feature not everybody would want to use). The solution ended up an easy one that we'll talk about another time.

This article will provide you with code that will scrape the PHP manual and WordPress Codex pages, and return the referenced functions in an array. In addition, and simply for the sake of it, we've got a couple of WordPress functions that'll render the function list from both manuals into a WP post or page. You can see examples of how we display the WP functions here and the PHP functions here. You may need to give both pages a few seconds to load (particularly the PHP page that weighs in at nearly 10,000 lines long).

The PHP Functions

The first function will construct an array of over 9000 functions and methods from the PHP manual. An example of the returned array is as follows: [key] => Description - FormattedLink.

The PHP function is as follows:

1
<?php 
2
/*
3
 Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)
4
 http://www.beliefmedia.com/wordpress-php-functions
5
*/
6
 
7
function beliefmedia_php_functions() {
8
 
9
  /* Get data from PHP manual reference page */
10
  $data = file_get_contents('http://php.net/manual/en/indexes.functions.php');
11
  preg_match_all('@<li><a[\s]*href=&quot;([^>&quot;]*)&quot;[\s]*class=&quot;index&quot;>([^>&quot;]*)</a>[\s]*-[\s]*([^>&quot;]*)</li>@si',$data,$matches);
12
 
13
  /* Match data */
14
  $keyarray = $matches['2'];
15
  $description = $matches['3'];
16
  $linkarray = $matches['2'];
17
  $array = array_combine($keyarray, $description);
18
 
19
  /* Create array */
20
  $i = 0; foreach ($array as $key=>$value) {
21
 
22
     /* Create link from $linkarray */
23
     $function = trim($linkarray[&quot;$i&quot;]);
24
     $function = strtolower(str_replace('_', '-', $function));
25
     $function = (strripos($function,'::') !== false) ? str_replace('::', '.', $function) : 'function.' . $function;
26
     $link = 'http://php.net/manual/en/' . $function . '.php';
27
 
28
     $key = trim($key); $newvalue = trim($newvalue);
29
     $description = trim($matches['3'][&quot;$i&quot;]);
30
       $description = preg_replace(&quot;/\r|\n/&quot;, ' ', $description);
31
       $description = trim(preg_replace('!\s+!', ' ', $description));
32
     $newarray[&quot;$key&quot;] = trim($newvalue) . ' ' . $description . ' - <a href=&quot;' . $link . '&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;>' . $key . '</a>';
33
 
34
    $i++;
35
  }
36
 
37
 return $newarray;
38
}
39
 
40
/* Return array of PHP functions */
41
echo 'pre' . print_r($newarray,true) . '/pre';
42
?>

The code to build an array of WordPress functions in the format key => Link is as follows:

1
<?php 
2
/*
3
 Return a list of WordPress functions. Returns array.
4
 Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)
5
 http://www.beliefmedia.com/wordpress-php-functions
6
*/
7
 
8
function beliefmedia_wordpress_functions() {
9
 
10
  /* Get data from WordPress reference page */
11
  $data = file_get_contents('https://codex.wordpress.org/Function_Reference');
12
  preg_match_all('@<tt>[\s]*<a[\s]href=&quot;([^>&quot;]*)&quot;[\s]*title=&quot;([^>&quot;]*)&quot;>([^>&quot;]*)</a>[\s]*</tt>@si',$data,$matches);
13
 
14
  $keyarray = $matches['3'];
15
  $valuearray = $matches['1'];
16
  $array = array_combine($keyarray, $valuearray);
17
 
18
   foreach ($array as $key=>$value) {
19
      $newvalue = 'https://codex.wordpress.org' . $value;
20
      $key = trim($key); $newvalue = trim($newvalue);
21
      $newarray[&quot;$key&quot;] = trim($newvalue);
22
   }
23
 
24
 return $newarray;
25
}
26
 
27
/* Return array */
28
echo 'pre' . print_r(beliefmedia_wordpress_functions(), true) . '/pre';
29
?>

The WordPress Shortcode Functions

The WordPress shortcode is used to populate a page with a list of functions with links back to the relevant manual.

WordPress Codex

The first shortcode will list all the WordPress functions listed on the WP Function Reference page .

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Return a list of WordPress functions (with WordPress shortcode)
4
 Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)
5
 https://codex.wordpress.org/Function_Reference
6
*/
7
 
8
9
  $atts = shortcode_atts(array(
10
    'url' => 'https://codex.wordpress.org/Function_Reference',
11
    'cache' => 3600 * 24 * 28
12
  ), $atts);
13
 
14
 $transientrecord = 'wpcodx_' . md5(serialize($atts));
15
 $cachedposts = get_transient($transientrecord);
16
 if ($cachedposts !== false) {
17
 return $cachedposts;
18
 
19
  } else {
20
 
21
   /* Get WordPress HTML Reference Page */
22
   $data = file_get_contents($atts['url']);
23
   preg_match_all('@<tt>[\s]*<a[\s]href=&quot;([^>&quot;]*)&quot;[\s]*title=&quot;([^>&quot;]*)&quot;>([^>&quot;]*)</a>[\s]*</tt>@si', $data, $matches);
24
   $keyarray = $matches['3'];
25
   $valuearray = $matches['1'];
26
   $array = array_combine($keyarray, $valuearray);
27
 
28
   /* Build list */
29
   $return = '<ul>';
30
    foreach ($array as $key=>$value) {
31
      $newvalue = 'https://codex.wordpress.org' . $value;
32
      $key = trim($key); $newvalue = trim($newvalue);
33
 
34
      /* Each line.. */
35
      $return .= '<li><a href=&quot;' . $newvalue . '&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;>' . $key . '</a>';
36
    }
37
   $return .= '</ul>';
38
 
39
  set_transient($transientrecord, $return, $atts['cache']);
40
  return $return;
41
 }
42
}
43
add_shortcode('wpcodexfunctions','beliefmedia_get_wordpress_codex_functions');

Use with the shortcode of [wpcodexfunctions].

PHP Functions

The second function will retrieve all the PHP functions listed on their Function and Method listing page.

Copy and paste the WordPress function into your theme's functions.php file or, if you sensibly have one installed, your custom functions plugin.

1
<?php 
2
/*
3
 Return a list of PHP functions with WordPress shortcode.
4
 Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)
5
 http://www.beliefmedia.com/wordpress-php-functions
6
*/
7
 
8
function beliefmedia_get_php_functions($atts) {
9
  $atts = shortcode_atts(array(
10
    'url' => 'http://php.net/manual/en/indexes.functions.php',
11
    'cache' => 3600 * 24 * 60
12
  ), $atts);
13
 
14
 $transient = 'phpf_' . md5(serialize($atts));
15
 $cachedposts = get_transient($transient);
16
 if ($cachedposts !== false) {
17
 return $cachedposts;
18
 
19
  } else {
20
 
21
   /* Get data from PHP manual reference page */
22
   $data = file_get_contents($atts['url']);
23
   preg_match_all('@<li><a[\s]*href=&quot;([^>&quot;]*)&quot;[\s]*class=&quot;index&quot;>([^>&quot;]*)</a>[\s]*-[\s]*([^>&quot;]*)</li>@si', $data, $matches);
24
   $keyarray = $matches['2'];
25
   $description = $matches['3'];
26
   $linkarray = $matches['2'];
27
   $array = array_combine($keyarray, $description);
28
 
29
    $i = 0; $return = '<ul>';
30
 
31
    foreach ($array as $key => $value) {
32
 
33
      /* Create link from $linkarray */
34
      $function = trim($linkarray[&quot;$i&quot;]);
35
      $function = strtolower(str_replace('_', '-', $function));
36
      $function = (strripos($function,'::') !== false) ? str_replace('::', '.', $function) : 'function.' . $function;
37
      $link = 'http://php.net/manual/en/' . $function  . '.php';
38
 
39
      $key = trim($key); $newvalue = trim($newvalue);
40
      $description = $matches['3'][&quot;$i&quot;];
41
 
42
      $newarray[&quot;$key&quot;] = $link;
43
 
44
      /* Generate HTML */
45
      $return .= '<li><a href=&quot;' . $link . '&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener noreferrer&quot;>' . $key . '</a> - ' . $description  . '</li>';
46
 
47
      $i++;
48
    }
49
 
50
   $return .= '</ul>';
51
 
52
  set_transient($transient, $return, $atts['cache']);
53
  return $return;
54
 }
55
}
56
add_shortcode('phpfunctions', 'beliefmedia_get_php_functions');

Use with the shortcode of [phpfunctions].

Considerations

  • Regular expressions are not normally rarely ever the most efficient solution for parsing HTML pages. PHP's DomDocument class is almost always a better option. In my case, the regex was just easy. Obviously, if the format for listing the functions on either page changes then the regex will have to be updated.

    In terms of using regular expressions to parse HTML, the following post from Stackoverflow comes to mind.

Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)
  • That said, parsing arbitrary HTML is a bad idea. The PHP and WordPress Codex page have maintained structure for a long time.
  • It's best to retrieve the data and insert into a database. This makes retrieval faster, and makes it super-simple to paginate into smaller chunks.
  • A secondary reason we wrote this functions was for the purpose of permitting free-text searches of all functions. An example screenshot is pictured below.
Get a List of WordPress and PHP Functions in an Array (and Display them with Shortcode)

Searching for str_* functions

  • If you use shortcode and retrieve the results, understand that it's a bad idea. Storing the returned data from the supplied functions in a database - or caching with our library to mitigate the high memory usage - is a must.

Download

These functions are due for a much-needed update. Check back soon.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

RELATED READING

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment