├── .gitignore ├── assets ├── style.css └── script.js ├── plugin.php ├── google-font.php ├── control.php ├── README.md └── google-font-collection.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .AppleDouble 3 | .LSOverride 4 | Icon 5 | 6 | 7 | # Thumbnails 8 | ._* 9 | 10 | # Files that might appear on external disk 11 | .Spotlight-V100 12 | .Trashes -------------------------------------------------------------------------------- /assets/style.css: -------------------------------------------------------------------------------- 1 | .fontPickerCustomControl .fancyDisplay{ 2 | display: none; 3 | } 4 | .fontPickerCustomControl .fancyDisplay ul{ 5 | list-style: none; 6 | margin: 0; 7 | padding: 0; 8 | } 9 | .fontPickerCustomControl .fancyDisplay ul li{ 10 | cursor: pointer; 11 | float: left; 12 | font-size:1.6em; 13 | margin: .3em 5% .3em 0; 14 | padding: 2% 0; 15 | padding-right: 2%; 16 | width: 40%; 17 | } -------------------------------------------------------------------------------- /plugin.php: -------------------------------------------------------------------------------- 1 | title = $title; 18 | $this->location = $location; 19 | $this->cssDeclaration = $cssDeclaration; 20 | $this->cssClass = $cssClass; 21 | } 22 | 23 | /** 24 | * Getters 25 | * taken from: http://stackoverflow.com/questions/4478661/getter-and-setter 26 | **/ 27 | public function __get($property) 28 | { 29 | if (property_exists($this, $property)) { 30 | return $this->$property; 31 | } 32 | } 33 | } // Custom_Font -------------------------------------------------------------------------------- /assets/script.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Hide default select box, replace with fancy font picker 3 | */ 4 | jQuery(document).ready(function($) { 5 | // initialize the custom control for the font picker 6 | $(".fontPickerCustomControl").fontPickerCustomControl(); 7 | }); 8 | 9 | /** 10 | * A small jquery plugin that does all of the hard work 11 | */ 12 | (function( $ ) { 13 | $.fn.fontPickerCustomControl = function() { 14 | 15 | //return the 'this' selector to maintain jquery chainability 16 | return this.each(function() { 17 | 18 | // cache this selector for further use 19 | thisFontPickerCustomControl = this; 20 | 21 | // hide select box 22 | $("select", this).hide(); 23 | 24 | // show fancy content 25 | $(".fancyDisplay", this).show(); 26 | 27 | // add event listeners to fancy display 28 | $(".fancyDisplay ul li").on("click", function(event){ 29 | // get index of clicked element 30 | var index = $(".fancyDisplay ul li", thisFontPickerCustomControl).index(this); 31 | 32 | // unselect all options 33 | $('select option', thisFontPickerCustomControl).removeAttr('selected'); 34 | 35 | // select new element 36 | // simulate a change 37 | $('select :nth-child('+(index+1)+')', thisFontPickerCustomControl).attr('selected', 'selected').change(); 38 | }); 39 | 40 | }); 41 | 42 | }; 43 | })( jQuery ); -------------------------------------------------------------------------------- /control.php: -------------------------------------------------------------------------------- 1 | choices ) ) 29 | { 30 | // if there are no choices then don't print anything 31 | return; 32 | } 33 | 34 | //print links to css files 35 | $this->fonts->printThemeCustomizerCssLocations(); 36 | 37 | //print css to display individual fonts 38 | $this->fonts->printThemeCustomizerCssClasses(); 39 | ?> 40 |
41 | 47 |
48 | 59 |
60 |
61 | "Ubuntu Condensed", 20 | "location" => "Ubuntu+Condensed", 21 | "cssDeclaration" => "'Ubuntu Condensed', sans-serif", 22 | "cssClass" => "ubuntuCondensed" 23 | ); 24 | $customFontFamilies = new Google_Font_Collection($fonts); 25 | } 26 | ``` 27 | * **title** - the title you wish the user to see. 28 | * **location** - the query string on the stylesheet for the google font. If the file name for the font is `http://fonts.googleapis.com/css?family=Ubuntu+Condensed` then the `location` should be `Ubuntu+Condensed`. 29 | * **cssDeclaration** - the code to be applied to the `font-family` css property. You can keep is the same as Google suggests or you can set up your own fallbacks. 30 | * **cssClass** - the CSS class for the theme cusomizer. You can create any CSS class you want. 31 | 32 | 5. Create the control 33 | 34 | ``` php 35 | if (class_exists('Google_Font_Picker_Custom_Control')) 36 | { 37 | // make sure we have the control included 38 | $wp_customize->add_control( new Google_Font_Picker_Custom_Control( $wp_customize, 'font_family_control', array( 39 | 'label' => __( 'Font Family', 'mytheme' ), 40 | 'section' => 'mytheme_new_section_fonts', 41 | 'settings' => 'font_family', 42 | 'choices' => $customFontFamilies->getFontFamilyNameArray(), 43 | 'fonts' => $customFontFamilies 44 | ))); 45 | } 46 | ``` 47 | 48 | 49 | ## Results 50 | 51 | ![screenshot](http://img.photobucket.com/albums/v357/BFTrick/Web/google-font-picker-custom-tool-for-wordpress_zps76ece86d.png) 52 | 53 | ## FAQ 54 | 55 | 1. **Can I only add one font to the $fonts array in step 4?** 56 | 57 | No, you can add as many as you like. IMO eight choices looks pretty and gives the user tons of options. 58 | 59 | 2. **What's up with the class_exists() functions?** 60 | 61 | Those are there so that if you were to ever deactivate or uninstall the plugin that your site wouldn't crash. If you do uninstall the plugin the control will simply not load. There shouldn't be any errors. 62 | 63 | ## Version 64 | 65 | **1.1** 66 | 67 | * Updated array select box to save font names instead of array indices. See issue #1. 68 | 69 | **1.0** 70 | 71 | * Initial release 72 | -------------------------------------------------------------------------------- /google-font-collection.php: -------------------------------------------------------------------------------- 1 | $value) 25 | { 26 | $this->fonts[$value["title"]] = new Google_Font($value["title"], $value["location"], $value["cssDeclaration"], $value["cssClass"]); 27 | } 28 | } 29 | 30 | /** 31 | * getFontFamilyNameArray Function 32 | * this function returns an array containing all of the font family names 33 | **/ 34 | function getFontFamilyNameArray() 35 | { 36 | $result; 37 | foreach ($this->fonts as $key => $value) 38 | { 39 | $result[] = $value->__get("title"); 40 | } 41 | return $result; 42 | } 43 | 44 | /** 45 | * getTitle 46 | * this function returns the font title 47 | **/ 48 | function getTitle($key) 49 | { 50 | return $this->fonts[$key]->__get("title"); 51 | } 52 | 53 | /** 54 | * getLocation 55 | * this function returns the font location 56 | **/ 57 | function getLocation($key) 58 | { 59 | return $this->fonts[$key]->__get("location"); 60 | } 61 | 62 | /** 63 | * getCssDeclaration 64 | * this function returns the font css declaration 65 | **/ 66 | function getCssDeclaration($key) 67 | { 68 | return $this->fonts[$key]->__get("cssDeclaration"); 69 | } 70 | 71 | 72 | /** 73 | * getCssClassArray 74 | * this function returns an array of css classes 75 | * this function is used when displaying the fancy list of fonts in the theme customizer 76 | * this function is used to send a JS file an array for the postMessage transport option in the theme customizer 77 | **/ 78 | function getCssClassArray() 79 | { 80 | $result; 81 | foreach ($this->fonts as $key => $value) 82 | { 83 | $result[$value->__get("title")] = $value->__get("cssClass"); 84 | } 85 | return $result; 86 | } 87 | 88 | /** 89 | * getTotalNumberOfFonts 90 | * this function returns the total number of fonts 91 | **/ 92 | function getTotalNumberOfFonts() 93 | { 94 | return count($this->fonts); 95 | } 96 | 97 | /** 98 | * printThemeCustomizerCssLocations 99 | * this function prints the links to the css files for the theme customizer 100 | **/ 101 | function printThemeCustomizerCssLocations() 102 | { 103 | foreach ($this->fonts as $key => $value) 104 | { 105 | ?> 106 | " rel='stylesheet' type='text/css'> 107 | 118 | 130 |