├── .gitignore ├── functions.php ├── functionsphp ├── admin │ └── admin.class.php ├── cleanup │ └── cleanup.class.php ├── frontend │ └── frontend.class.php ├── functionsphp.class.php └── includes │ ├── autoloader.class.php │ ├── bootstrap.php │ ├── loader.class.php │ └── theme.class.php ├── index.php ├── readme.md ├── script.js └── style.css /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vanaf1979/functionsphp/1cd9902abc1d632acc13e48022a9523825ae2c24/.gitignore -------------------------------------------------------------------------------- /functions.php: -------------------------------------------------------------------------------- 1 | 18 | -------------------------------------------------------------------------------- /functionsphp/admin/admin.class.php: -------------------------------------------------------------------------------- 1 | text_domain . '-css' , $this->theme_path . '/public/css/admin.css' , array() , $this->version , 'all' ); 33 | 34 | } 35 | 36 | } 37 | 38 | 39 | public function enqueue_scripts( $page ) { 40 | 41 | if( $page == 'post.php' ) { 42 | 43 | wp_enqueue_script( $this->text_domain . '-js' , $this->theme_path . '/public/js/admin.js' , array() , $this->version , true ); 44 | 45 | } 46 | 47 | } 48 | 49 | 50 | public function register_nav_menus() { 51 | 52 | register_nav_menus( 53 | array( 54 | 'header-menu' => __( 'Header Menu' ), 55 | 'footer-menu' => __( 'Footer Menu' ), 56 | 'mobile-menu' => __( 'Mobile Menu' ) 57 | ) 58 | ); 59 | 60 | } 61 | 62 | 63 | function register_widget_areas() { 64 | 65 | register_sidebar( array( 66 | 'name' => 'Footer area one', 67 | 'id' => 'footer_area_one', 68 | 'description' => 'This widget area discription', 69 | 'before_widget' => '', 71 | 'before_title' => '

', 72 | 'after_title' => '

', 73 | )); 74 | 75 | register_sidebar( array( 76 | 'name' => 'Footer area two', 77 | 'id' => 'footer_area_two', 78 | 'description' => 'This widget area discription', 79 | 'before_widget' => '', 81 | 'before_title' => '

', 82 | 'after_title' => '

', 83 | )); 84 | 85 | register_sidebar( array( 86 | 'name' => 'Footer area three', 87 | 'id' => 'footer_area_three', 88 | 'description' => 'This widget area discription', 89 | 'before_widget' => '', 91 | 'before_title' => '

', 92 | 'after_title' => '

', 93 | )); 94 | 95 | register_sidebar( array( 96 | 'name' => 'Footer area four', 97 | 'id' => 'footer_area_four', 98 | 'description' => 'This widget area discription', 99 | 'before_widget' => '', 101 | 'before_title' => '

', 102 | 'after_title' => '

', 103 | )); 104 | 105 | } 106 | 107 | 108 | public function register_custom_posttypes() { 109 | 110 | // $args = array( 111 | // 'label' => __('Portfolio'), 112 | // 'singular_label' => __('Project'), 113 | // 'public' => true, 114 | // 'show_ui' => true, 115 | // 'capability_type' => 'post', 116 | // 'hierarchical' => false, 117 | // 'rewrite' => array('slug' => 'POST-TYPE-SLUG-HERE'), 118 | // 'supports' => array('title', 'editor', 'thumbnail') 119 | // ); 120 | 121 | // register_post_type( 'custom-post-type' , $args ); 122 | 123 | } 124 | 125 | } 126 | -------------------------------------------------------------------------------- /functionsphp/cleanup/cleanup.class.php: -------------------------------------------------------------------------------- 1 | get_template() == 'index.php' ) { 31 | 32 | wp_enqueue_style( $this->text_domain . '-frontend' , $this->theme_path . '/front-page.css' , array() , $this->version , 'all' ); 33 | 34 | } 35 | 36 | wp_enqueue_style( $this->text_domain . '-styles' , $this->theme_path . '/style.css' , array() , $this->version , 'all' ); 37 | 38 | } 39 | 40 | 41 | public function enqueue_scripts() { 42 | 43 | if( $this->get_template() == 'front-page.php' ) { 44 | 45 | wp_enqueue_script( $this->text_domain . '-frontend' , $this->theme_path . '/front-page.js' , array() , $this->version , true ); 46 | 47 | } 48 | 49 | wp_enqueue_script( $this->text_domain . '-scripts' , $this->theme_path . '/script.js' , array() , $this->version , true ); 50 | 51 | } 52 | 53 | 54 | public function register_thumbnail_sizes() { 55 | 56 | add_theme_support( 'post-thumbnails' ); 57 | 58 | add_image_size( 'custom-thumbnail' , 900 , 600 , true ); 59 | 60 | } 61 | 62 | 63 | public function add_theme_support() { 64 | 65 | add_theme_support( 'html5' , array( 'comment-list' , 'comment-form' , 'search-form' , 'gallery' , 'caption' ) ); 66 | 67 | add_theme_support( 'menus' ); 68 | 69 | add_theme_support( 'post-formats' , array( 'aside' , 'gallery' , 'link' , 'image' , 'quote' , 'status' , 'video' , 'audio' , 'chat' ) ); 70 | 71 | } 72 | 73 | 74 | public function load_theme_textdomain() { 75 | 76 | load_theme_textdomain( $this->text_domain , $this->theme_path . '/languages' ); 77 | 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /functionsphp/functionsphp.class.php: -------------------------------------------------------------------------------- 1 | loader = new Loader(); 31 | 32 | $this->define_frontend_hooks(); 33 | $this->define_admin_hooks(); 34 | $this->define_cleanup_hooks(); 35 | 36 | $this->loader->run(); 37 | 38 | } 39 | 40 | 41 | private function define_frontend_hooks() { 42 | 43 | $frontend = new FrontEnd(); 44 | 45 | // Enqueue styles and scripts. 46 | $this->loader->add_action( 'wp_enqueue_scripts' , $frontend , 'enqueue_styles' , 10 , 1 ); 47 | $this->loader->add_action( 'wp_enqueue_scripts' , $frontend , 'enqueue_scripts' , 10 , 1 ); 48 | 49 | // Add theme support. 50 | $this->loader->add_action( 'init' , $frontend , 'add_theme_support' , 1 , 1 ); 51 | 52 | // Register thumbnail sizes. 53 | $this->loader->add_action( 'init' , $frontend , 'register_thumbnail_sizes' , 1 ); 54 | 55 | // Load theme's translated strings. 56 | $this->loader->add_action( 'after_setup_theme' , $frontend , 'load_theme_textdomain' , 1 ); 57 | 58 | } 59 | 60 | 61 | private function define_admin_hooks() { 62 | 63 | $admin = new Admin(); 64 | 65 | // Enqueue styles and scripts. 66 | $this->loader->add_action( 'admin_enqueue_scripts' , $admin , 'enqueue_styles' ); 67 | $this->loader->add_action( 'admin_enqueue_scripts' , $admin , 'enqueue_scripts' ); 68 | 69 | // Register navigational menus. 70 | $this->loader->add_action( 'init' , $admin , 'register_nav_menus' ); 71 | 72 | // Register widgert areas. 73 | $this->loader->add_action( 'widgets_init' , $admin , 'register_widget_areas' ); 74 | 75 | // Register custom posttypes. 76 | $this->loader->add_action( 'init' , $admin , 'register_custom_posttypes' ); 77 | 78 | } 79 | 80 | 81 | private function define_cleanup_hooks() { 82 | 83 | $cleanup = new CleanUp(); 84 | 85 | // Remove emoji's header. 86 | $this->loader->add_action( 'init' , $cleanup , 'disable_emoji_dequeue_script' ); 87 | 88 | // Remove junk from header. 89 | $this->loader->add_action( 'init' , $cleanup , 'clean_up_header' ); 90 | 91 | // Remove wpembed scripts. 92 | $this->loader->add_action( 'wp_footer' , $cleanup , 'remove_wpembed_scripts' ); 93 | 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /functionsphp/includes/autoloader.class.php: -------------------------------------------------------------------------------- 1 | mainNamespace = $namespace; 27 | 28 | $this->basePath = $basePath; 29 | 30 | $this->extention = $extention; 31 | 32 | $this->registerAutoloader(); 33 | 34 | } 35 | 36 | 37 | private function registerAutoloader() { 38 | 39 | spl_autoload_register( array( $this , 'loadClass' ) ); 40 | 41 | } 42 | 43 | 44 | private function loadClass( $namespace ) { 45 | 46 | $fullClassPath = $this->fullClassPath( $namespace ); 47 | 48 | if( $this->classInNamespace( $namespace ) && $this->classFileExists( $fullClassPath ) ) { 49 | 50 | require_once $fullClassPath; 51 | 52 | } 53 | 54 | } 55 | 56 | 57 | private function classInNamespace( $namespace ) { 58 | 59 | return strpos( $namespace , $this->mainNamespace ) === 0 ? true : false; 60 | 61 | } 62 | 63 | 64 | private function fullClassPath( $namespace ) { 65 | 66 | return $this->basePath . '/' . $this->convertNamespace( $namespace ) . $this->extention; 67 | 68 | } 69 | 70 | 71 | private function classFileExists( $fullpath ) { 72 | 73 | return file_exists( $fullpath ) ? true : false; 74 | 75 | } 76 | 77 | 78 | private function convertNamespace( $namespace ) { 79 | 80 | return strtolower( str_replace( '\\' , DIRECTORY_SEPARATOR , $namespace ) ); 81 | 82 | } 83 | 84 | } 85 | 86 | -------------------------------------------------------------------------------- /functionsphp/includes/bootstrap.php: -------------------------------------------------------------------------------- 1 | 2 | actions = array(); 21 | $this->filters = array(); 22 | 23 | } 24 | 25 | 26 | public function add_action( $hook , $component , $callback , $priority = 10 , $accepted_args = 1 ) { 27 | 28 | $this->actions = $this->add( $this->actions , $hook , $component , $callback , $priority , $accepted_args ); 29 | 30 | } 31 | 32 | 33 | public function add_filter( $hook , $component , $callback , $priority = 10 , $accepted_args = 1 ) { 34 | 35 | $this->filters = $this->add( $this->filters , $hook , $component , $callback , $priority , $accepted_args ); 36 | 37 | } 38 | 39 | 40 | private function add( $hooks , $hook , $component , $callback , $priority , $accepted_args ) { 41 | 42 | $hooks[] = array( 43 | 'hook' => $hook, 44 | 'component' => $component, 45 | 'callback' => $callback, 46 | 'priority' => $priority, 47 | 'accepted_args' => $accepted_args 48 | ); 49 | 50 | return $hooks; 51 | 52 | } 53 | 54 | 55 | public function run() { 56 | 57 | foreach ( $this->filters as $hook ) { 58 | 59 | add_filter( $hook['hook'] , array( $hook['component'] , $hook['callback'] ) , $hook['priority'] , $hook['accepted_args'] ); 60 | 61 | } 62 | 63 | foreach ( $this->actions as $hook ) { 64 | 65 | add_action( $hook['hook'] , array( $hook['component'] , $hook['callback'] ) , $hook['priority'] , $hook['accepted_args'] ); 66 | 67 | } 68 | 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /functionsphp/includes/theme.class.php: -------------------------------------------------------------------------------- 1 | theme_name = $theme->get( 'Name' ); 33 | 34 | $this->version = $theme->get( 'Version' ); 35 | 36 | $this->text_domain = $theme->get( 'TextDomain' ); 37 | 38 | $this->theme_path = get_template_directory_uri(); 39 | 40 | } 41 | 42 | 43 | public function get_template() { 44 | 45 | if( $this->template ) { 46 | 47 | return $this->template; 48 | 49 | } else { 50 | 51 | global $template; 52 | return $this->template = basename( $template ); 53 | 54 | } 55 | 56 | } 57 | 58 | } -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | > 13 | 14 |
15 | 16 |

FunctionsPhp

17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | 2 |

3 | 4 |

5 | 6 | ### FunctionsPhp: A Maintainable OOP WordPress functions.php boilerplate. 7 | 8 | #### What? 9 | FunctionsPhp is a Maintainable OOP WordPress functions.php boilerplate. based upon the [WordPress Plugin Boilerplate](https://github.com/devinvinson/WordPress-Plugin-Boilerplate/) by [Devin Vinson](https://twitter.com/DevinVinson). I refactored WPPB to be used in theme development and to include Php namespaces and Autoloading. 10 | 11 | #### Why? 12 | Becouse of it's legacy WordPress by nature is not written in modern Php. But that doesn't mean our theme's code can't be. Working in OOP means more maintanable code and prevents us from creating spaghetti code functions.php files. 13 | 14 | #### How? 15 | It's basic design is that all WordPress action and filter hooks are defined within the main class (functionsphp.class.php), and the hook callback functions are placed within seperated classes (admin/admin.class.php, and frontend/frontend.class.php etc). 16 | 17 | #### Now what? 18 | Download, check it out and make it your own. The code should be pretty self explanitory. I could really use some feedback on this so if you have any suggestions for improvement please do let me know. You can find me over at [Twitter @Vanaf1979](https://twitter.com/Vanaf1979). -------------------------------------------------------------------------------- /script.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vanaf1979/functionsphp/1cd9902abc1d632acc13e48022a9523825ae2c24/script.js -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | /*! 2 | Theme Name: Functions.php boilerplate 3 | Theme URI: https://vanaf1979.nl/ 4 | Description: Wordpress functions.php boilerplate. 5 | Version: 1.0.0 6 | Author: Vanaf1979 7 | Author URI: https://vanaf1979.nl 8 | Text Domain: functions-php 9 | License: MIT License 10 | License URI: http://opensource.org/licenses/MIT 11 | */ --------------------------------------------------------------------------------