Advertisement
IzaacJ

Get custom headers from WordPress Themes

Nov 23rd, 2015
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.54 KB | None | 0 0
  1. // Imagine you have a style.css with these headers:
  2. /******************************
  3.  * Theme Name: Theme Name
  4.  * Theme URI: ThemeURI
  5.  * Author: Author Name
  6.  * Author URI: AuthorUri
  7.  * Description: ThemeDescription
  8.  * License: License
  9.  * License URI: LicenseURI
  10.  * Tags: ThemeTags
  11.  *
  12.  * Custom Header: Custom Value
  13.  *
  14.  * Custom Header Two: Custom Value
  15.  *
  16.  *****/
  17. // And you need to get the value of Custom Header and/or Custom Header Two
  18. // Here is a function to get standard and custom headers in style.css in WordPress Themes
  19. function getThemeInfo($stylesheet) {
  20.         if(file_exists($stylesheet)) {
  21.             $file = file($stylesheet);
  22.             $contents = array();
  23.             foreach ($file as $lines => $line) {
  24.                 preg_match("/\*(.*?):/s", $line, $name);
  25.                 preg_match("/:(.*)$/s", $line, $value);
  26.                 if($name && $value) {
  27.                     $contents[trim($name[1])] = trim($value[1]);
  28.                 }
  29.             }
  30.             return $contents;
  31.         }else{
  32.             return array();
  33.         }
  34.     }
  35. // Running this function on style.css with the headers specified above, would generate an array like this:
  36. // Array
  37. // (
  38. //     [Theme Name] => Theme Name
  39. //     [Theme URI] => ThemeURI
  40. //     [Author] => Author Name
  41. //     [Author URI] => AuthorUri
  42. //     [Description] => ThemeDescription
  43. //     [License] => License
  44. //     [License URI] => LicenseURI
  45. //     [Tags] => ThemeTags
  46. //     [Custom Header] => Custom Value
  47. //     [Custom Header Two] => Custom Value
  48. // )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement