Skip to content

Instantly share code, notes, and snippets.

@tylerlwsmith
Last active January 27, 2024 14:05
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save tylerlwsmith/dc1f5fb04126189ec42980952cd124d7 to your computer and use it in GitHub Desktop.
Save tylerlwsmith/dc1f5fb04126189ec42980952cd124d7 to your computer and use it in GitHub Desktop.
<?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'
])
->addStyle('theme-styles', get_stylesheet_uri())
->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($location);
});
return $this;
}
}
@tylerlwsmith
Copy link
Author

Edits made, thanks!

@halbia
Copy link

halbia commented Sep 12, 2019

it seems addSupport function dos not work!

@halbia
Copy link

halbia commented Sep 12, 2019

`<?php

class themeClass {

public function __construct() {

    @ini_set( 'upload_max_size' , '64M' );
    @ini_set( 'post_max_size', '64M');
    @ini_set( 'max_execution_time', '300' );

    $this->addSupport( 'title-tag' );
    $this->addSupport( 'custom-logo' );
    $this->addSupport( 'post-thumbnails' );
    $this->addSupport( 'customize-selective-refresh-widgets' );
    $this->addSupport('html5', [ 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption' ]);
    $this->addSupport( 'responsive-embeds' );
    $this->addStyle('theme-styles',  get_stylesheet_uri());
}


// private function that will fire 'after_setip_theme' action
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 addSupport ( $feature, $options = null ) {
    $this->actionAfterSetup ( function() use ( $feature, $options ) {
        
        if ($options) {
            add_theme_support( $feature, $options );
        } else {
            add_theme_support( $feature );
        }
        
    });
    return $this;
} //end addSupport function


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


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 = true)
{
    $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($location);
    });
    return $this;
}


// my function for get menu Items
public function getMenuItems($location_id){
//$locations = get_registered_nav_menus();
$menus = wp_get_nav_menus();
$menu_locations = get_nav_menu_locations();

    if (isset($menu_locations[ $location_id ]) && $menu_locations[ $location_id ]!=0) {
        foreach ($menus as $menu) {
            if ($menu->term_id == $menu_locations[ $location_id ]) {
                $menu_items = wp_get_nav_menu_items($menu);
                break;
            }
        }
        return $menu_items;
    }
}//end getMenuItems()



public function cptProjects( $tax = boolean ) {
    function cpt_projects() {
        // Set UI labels for Custom Post Type
        $labels = array(
            'name'                => 'پروژه ها',
            'singular_name'       => 'پروژه',
            'menu_name'           => 'پروژه ها',
            'parent_item_colon'   => 'پروژه',
            'all_items'           => 'همه پروژه ها',
            'view_item'           => 'مشاهده پروژه',
            'add_new_item'        => 'افزودن پروژه',
            'add_new'             => 'افزودن پروژه',
            'edit_item'           => 'ویرایش پروژه',
            'update_item'         => 'به روز رسانی پروژه',
            'search_items'        => 'جستجوی پروژه',
            'not_found'           => 'پیدا نشد',
            'not_found_in_trash'  => 'در زباله دان پیدا نشد',
        );
        
        // Set other options for Custom Post Type
        $args = array(
            'label'               => 'پروژه ها',
            'description'         => 'مشاهده و درج پروژه ها',
            'labels'              => $labels,
            // Features this CPT supports in Post Editor
            'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
            // You can associate this CPT with a taxonomy or custom taxonomy.
            /* A hierarchical CPT is like Pages and can have
            * Parent and child items. A non-hierarchical CPT
            * is like Posts.
            */ 
            'hierarchical'        => false,
            // 'show_in_rest'        => true, // for showing gutenburg editor
            'public'              => true,
            'show_ui'             => true,
            'show_in_menu'        => true,
            'show_in_nav_menus'   => true,
            'show_in_admin_bar'   => true,
            'can_export'          => true,
            'has_archive'         => true,
            'rewrite'             => array('slug' => 'projects'),
            'exclude_from_search' => false,
            'publicly_queryable'  => true,
            'menu_icon'           => 'dashicons-schedule',
            'capability_type'     => 'page',
        );
         
        // Registering your Custom Post Type
        register_post_type( 'projects', $args );
        
    }
    add_action( 'init', 'cpt_projects' );
    
    
    //if $tax == true then taxonomy init
    if ( $tax ) {
    // ساخت تکسونومی برای پروژه ها
        function cpt_projects_taxonomy()
        {
            $labels= array(
            'name'              => 'دسته بندی پروژه ها',
            'singular_name'     => 'دسته پروژه ها',
            'search_items'      => 'جستجوی پروژه ها',
            'all_items'         => 'همه دسته ها',
            'edit_item'         => 'ویرایش دسته بندی',
            'update_item'       => 'به روزرسانی دسته بندی',
            'add_new_item'      => 'افزودن دسته بندی',
            'new_item_name'     => 'افزودن دسته بندی',
            'menu_name'         => 'دسته ها',
            );
            $settings = array(
                'hierarchical'          => true,
                'labels'                => $labels,
                'show_ui'               => true,
                'query_var'             => true,
                'show_in_menu'          =>  true,
                'show_in_nav_menus'     =>  true,
                'show_in_quick_edit'    =>  true,
                'show_admin_column'     =>  true,
                'rewrite'               => array('slug' => 'projects', 'with_front' =>  true),
                'query_var'             =>  'projects',
            );
            register_taxonomy( 'projects', array('projects'), $settings);
        }
        add_action('init','cpt_projects_taxonomy',0);
    }
} //end cptProjects()

}`

even if i do $mythemeobj->addSupport( 'title-tag' ); in my funcitons.php file

@punei123andrei
Copy link

Hello, can I have a simple example of a theme build this way?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment