Skip to content

Instantly share code, notes, and snippets.

@tylerlwsmith
Last active May 19, 2025 20:30
Show Gist options
  • Save tylerlwsmith/dc1f5fb04126189ec42980952cd124d7 to your computer and use it in GitHub Desktop.
Save tylerlwsmith/dc1f5fb04126189ec42980952cd124d7 to your computer and use it in GitHub Desktop.

Revisions

  1. tylerlwsmith revised this gist Jun 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion theme-class.php
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ public function __construct()
    'gallery',
    'caption'
    ])
    ->addStylesheet('theme-styles', get_stylesheet_uri())
    ->addStyle('theme-styles', get_stylesheet_uri())
    ->addCommentScript();
    }

  2. tylerlwsmith revised this gist Jun 7, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion theme-class.php
    Original file line number Diff line number Diff line change
    @@ -139,7 +139,7 @@ public function addNavMenu($location, $description)

    public function removeNavMenu($location){
    $this->actionAfterSetup(function() use ($location){
    unregister_nav_menu($locations);
    unregister_nav_menu($location);
    });
    return $this;
    }
  3. tylerlwsmith revised this gist Nov 29, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions theme-class.php
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,7 @@ public function __construct()
    'gallery',
    'caption'
    ])
    ->addStylesheet('theme-styles', get_stylesheet_uri())
    ->addCommentScript();
    }

  4. tylerlwsmith created this gist Nov 29, 2018.
    145 changes: 145 additions & 0 deletions theme-class.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,145 @@
    <?php

    class MyTheme
    {
    private function actionAfterSetup($function)
    {
    add_action('after_setup_theme', function() use ($function) {
    $function();
    });
    }

    private function actionEnqueueScripts($function)
    {
    add_action('wp_enqueue_scripts', function() use ($function){
    $function();
    });
    }

    public function __construct()
    {
    $this->addSupport('title-tag')
    ->addSupport('custom-logo')
    ->addSupport('post-thumbnails')
    ->addSupport('customize-selective-refresh-widgets')
    ->addSupport('html5', [
    'search-form',
    'comment-form',
    'comment-list',
    'gallery',
    'caption'
    ])
    ->addCommentScript();
    }

    public function addSupport($feature, $options = null)
    {
    $this->actionAfterSetup(function() use ($feature, $options) {
    if ($options){
    add_theme_support($feature, $options);
    } else {
    add_theme_support($feature);
    }
    });
    return $this;
    }

    public function removeSupport($feature)
    {
    $this->actionAfterSetup(function() use ($feature){
    remove_theme_support($feature);
    });
    return $this;
    }

    public function loadTextDomain($domain, $path = false)
    {
    $this->actionAfterSetup(function() use ($domain, $path){
    load_theme_textdomain($domain, $path);
    });
    return $this;
    }

    public function addImageSize($name, $width = 0, $height = 0, $crop = false)
    {
    $this->actionAfterSetup(function() use ($name, $width, $height, $crop){
    add_image_size($name, $width, $height, $crop);
    });
    return $this;
    }

    public function removeImageSize($name)
    {
    $this->actionAfterSetup(function() use ($name){
    remove_image_size($name);
    });
    return $this;
    }

    public function addStyle($handle, $src = '', $deps = array(), $ver = false, $media = 'all')
    {
    $this->actionEnqueueScripts(function() use ($handle, $src, $deps, $ver, $media){
    wp_enqueue_style($handle, $src, $deps, $ver, $media);
    });
    return $this;
    }

    public function addScript($handle, $src = '', $deps = array(), $ver = false, $in_footer = false)
    {
    $this->actionEnqueueScripts(function() use ($handle, $src, $deps, $ver, $in_footer){
    wp_enqueue_script($handle, $src, $deps, $ver, $in_footer);
    });
    return $this;
    }

    public function addCommentScript()
    {
    $this->actionEnqueueScripts(function(){
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
    wp_enqueue_script( 'comment-reply' );
    }
    });
    return $this;
    }

    public function removeStyle($handle)
    {
    $this->actionEnqueueScripts(function() use ($handle){
    wp_dequeue_style($handle);
    wp_deregister_style($handle);
    });
    return $this;
    }

    public function removeScript($handle)
    {
    $this->actionEnqueueScripts(function() use ($handle){
    wp_dequeue_script($handle);
    wp_deregister_script($handle);
    });
    return $this;
    }

    public function addNavMenus($locations = array())
    {
    $this->actionAfterSetup(function() use ($locations){
    register_nav_menus($locations);
    });
    return $this;
    }

    public function addNavMenu($location, $description)
    {
    $this->actionAfterSetup(function() use ($location, $description){
    register_nav_menu($location, $description);
    });
    return $this;
    }

    public function removeNavMenu($location){
    $this->actionAfterSetup(function() use ($location){
    unregister_nav_menu($locations);
    });
    return $this;
    }
    }