Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active July 12, 2016 00:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-hiroshi/388ecae71dec9b8a4b7e41f8ef7a19f0 to your computer and use it in GitHub Desktop.
Save s-hiroshi/388ecae71dec9b8a4b7e41f8ef7a19f0 to your computer and use it in GitHub Desktop.
WordPressのセットアップ(InfoTown_Setup, InfoTown_Head、InfoTown_Admin、InfoTown_Post)クラスのサンプルです。
<?php
/**
* HEAD処理
*
* @package InfoTown
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class InfoTown_Head {
/**
* Load stylesheets.
*
* @param array $args arguments of loading style.
*
* @since 1.0
*/
public function load_stylesheets( $args ) {
add_action(
'wp_enqueue_scripts',
function () use ( $args ) {
foreach ( $args as $arg ) {
wp_enqueue_style( $arg[0], $arg[1], $arg[2], $arg[3], $arg[4] );
}
}
);
}
/**
* Load script.
*
* @param array $args arguments of loading script.
*
* @since 1.0
*/
public function load_scripts( $args ) {
add_action(
'wp_enqueue_scripts',
function () use ( $args ) {
foreach ( $args as $arg ) {
wp_enqueue_script( $arg[0], $arg[1], $arg[2], $arg[3], $arg[4] );
}
}
);
}
/**
* Set viewport in head
*
* @param string $output view port.
*
* @since 1.0
*/
public function set_viewport( $output ) {
add_action(
'wp_head',
function () use ( $output ) {
echo "\n" . $output . PHP_EOL;
}
);
}
}
<?php
/**
* Set up theme.
*
* @package InfoTown
* @author Hiroshi Sawai <info@info-town.jp>
* @copyright Hiroshi Sawai
*/
class InfoTown_SetUp {
/**
* @var string $domain Text domain
*/
public $theme;
/**
* @var string $domain Text domain
*/
public $domain;
/**
* @return string テーマ名を返します。
*/
public function get_theme() {
return $this->theme;
}
/**
* @return bool|string テキストドメイン名を返します。
*/
public function get_text_domain() {
return $this->domain;
}
/**
* Specify theme name and text domain name.
*
* 可能文字は半角英数字、アンダースコア、ハイフンのみです(WordPressの仕様とはことなります)。
*
* @param string $theme テーマ名です。テーマ名の可能文字は半角英数字、アンダースコア、ハイフンのみです。
* @param string $domain テキストドメインです。ドメイン名の可能文字は半角英数字、アンダースコア、ハイフンのみです。
*/
function __construct( $theme, $domain = '' ) {
$this->theme = $theme;
$this->domain = $domain || $theme;
}
/**
* Specify translation file.
*
* @param string $dir 翻訳ファイルディレクトリです。
* 通常 テーマディレクトリ/languagesです。
*
* @since 1.0.0
*/
public function set_load_domain( $dir ) {
$dir = $dir || get_template_directory() . '/languages';
add_action(
'after_setup_theme',
function () use ( $dir ) {
load_theme_textdomain( $this->domain, $dir );
}
);
}
/**
* Set theme supports.
*
* @param array $args サポートする機能です。
*
* @since 1.0.0
*/
public function set_theme_support( $args ) {
foreach ( $args as $arg ) {
add_theme_support( $arg );
}
}
/**
* Set post thumbnail(eye catch).
*
* @param integer $width アイキャッチ横サイズです。
* @param integer $height アイキャッチ縦サイズです。
*/
public function set_post_thumbnails( $width = 640, $height = 480 ) {
add_action(
'after_setup_theme',
function () use ( $width, $height ) {
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( $width, $height );
}
);
}
/**
* Set menu.
*
* @param array $args メニューの配列です。
*/
public function set_menu( $args ) {
foreach ( $args as $key => $value ) {
register_nav_menu( $key, __( $value, $this->domain ) );
}
}
/**
* Register widget.
*
* @param array $widgets settings of widgets.
*
* @since 1.0.0
*/
function set_widget( $widgets ) {
add_action(
'widgets_init',
function () use ( $widgets ) {
foreach ( $widgets as $widget ) {
register_sidebar( $widget );
}
}
);
}
/**
* Add custom header to theme support.
*
* callback of after_setup_theme hook.
*
* @param array $options Associated array of custom header.
*
* @since 1.0
*/
function set_custom_header( $options ) {
add_theme_support( 'custom-header' );
$defaults = [
'width' => 1140,
'height' => 480,
'header-text' => false,
'header-text-color' => '333333',
'default-image' => esc_url( get_stylesheet_directory_uri() . '/images/common/default.jpg' ),
'random-default' => false,
];
$args = array_merge( $defaults, $options );
add_action(
'after_setup_theme',
function () use ( $args ) {
$args = apply_filters( 'custom_header_args', $args );
if ( function_exists( 'get_custom_header' ) ) {
add_theme_support( 'custom-header', $args );
}
}
);
}
/**
* Remove no use functions of wp_head.
*
* @param array $args 削除対象の配列です。
*
* @since 1.0.0
*/
function set_remove_no_use_wp_head( $args ) {
add_action(
'after_setup_theme',
function () use ( $args ) {
foreach ( $args as $arg ) {
remove_action( 'wp_head', $arg );
}
}
);
}
/**
* Load admin css.
*
* @param string $name css file for admin editor.
*/
public function set_editor_style( $name = 'editor-style.css' ) {
add_editor_style( $name );
}
}
<?php
/*
* Set timezone
*/
date_default_timezone_set( 'Asia/Tokyo' );
/*
* Set constant.
*/
define( 'THEME_NAME', 'my-theme' );
define( 'THEME_TEXT_DOMAIN', 'mytheme' );
define( 'THEME_URL', get_template_directory_uri() );
define( 'THEME_ASSETS_URL', THEME_URL );
define( 'THEME_ASSETS_JS_URL', THEME_ASSETS_URL . '/js' );
define( 'THEME_ASSETS_CSS_URL', THEME_ASSETS_URL . '/css' );
define( 'THEME_CUSTOM_HEADER_WIDTH', 940 );
define( 'THEME_CUSTOM_HEADER_HEIGHT', 330 );
define( 'THEME_EYECATCH_WIDTH', 480 );
define( 'THEME_EYECATCH_HEIGHT', 320 );
/*
* Set theme configuration.
*/
require_once( get_template_directory() . '/includes/class-setup.php' );
// add action and filter
$setup = new InfoTown_Setup( THEME_NAME, THEME_TEXT_DOMAIN );
// Load text domain.
$setup->set_load_domain( get_template_directory() . '/languages' );
// Set eye catch
$setup->set_post_thumbnails( THEME_EYECATCH_WIDTH, THEME_EYECATCH_HEIGHT );
// Set menu.
$setup->set_menu( [
'global__nav' => 'Global Menu',
'product' => 'Product Menu',
'footer' => 'Footer Menu',
] );
// Set editor css.
$setup->set_editor_style();
// Set widget.
$widgets = [
'front_slider' => [
'name' => __( 'トップスライダー画像', THEME_TEXT_DOMAIN ),
'description' => __(
'トップページのスライダー画像を設定します。',
THEME_TEXT_DOMAIN
),
'id' => 'front_slider',
'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-inner">',
'after_widget' => '</div></div>',
'before_title' => '<h4 class="title">',
'after_title' => '</h4>',
],
'side' => [
'name' => __( 'サイドバー', THEME_TEXT_DOMAIN ),
'description' => __(
'サイドバーウィジェットです。',
THEME_TEXT_DOMAIN
),
'id' => 'side',
'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="widget-inner">',
'after_widget' => '</div></div>',
'before_title' => '<h4 class="title">',
'after_title' => '</h4>',
],
];
$setup->set_widget( $widgets );
// Set custom header.
$setup->set_custom_header( [
'width' => THEME_CUSTOM_HEADER_WIDTH,
'height' => THEME_CUSTOM_HEADER_HEIGHT,
'header-text' => false,
'default-text-color' => '303030',
'default-image' => esc_url( get_stylesheet_directory_uri() . '/images/common/default.jpg' ),
'random-default' => false,
] );
// Set theme support.
$supports = [ 'automatic-feed-links', 'title-tag', 'custom-background' ];
$setup->set_theme_support( $supports );
// Remove no use function of wp_head.
$no_use = [ 'wp_generator', 'wlwmanifest_link', 'rsd_link' ];
$setup->set_remove_no_use_wp_head( $no_use );
/*
* Set header.
*/
require_once( get_template_directory() . '/includes/class-head.php' );
$header = new InfoTown_Head();
// Load stylesheet
$styles = [
0 => [
'bootstrap',
THEME_ASSETS_CSS_URL . '/vendor/bootstrap/css/bootstrap.min.css',
false,
'3.3.6',
'all',
],
1 => [
'font-awesome',
THEME_ASSETS_CSS_URL . '/vendor/font-awesome/css/font-awesome.min.css',
false,
'4.5.0',
'all',
],
2 => [
'animate',
THEME_ASSETS_CSS_URL . '/vendor/animate.min.css',
false,
'4.5.0',
'all',
],
3 => [
'scrollbar',
THEME_ASSETS_JS_URL . '/vendor/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.min.css',
false,
'3.1.13',
'all',
],
4 => [
'slick',
THEME_ASSETS_CSS_URL . '/vendor/slick/slick.css',
false,
'1.5.8',
'all',
],
5 => [
'slick-theme',
THEME_ASSETS_CSS_URL . '/vendor/slick/slick-theme.css',
false,
'1.5.8',
'all',
],
6 => [
'style',
get_stylesheet_uri(),
false,
'1.0',
'all',
],
];
$header->load_stylesheets( $styles );
// Load script.
$scripts = [
0 => [
'scrollbar',
THEME_ASSETS_JS_URL . '/vendor/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.concat.min.js',
[ 'jquery' ],
'3.1.13',
false,
],
1 => [
'slick',
THEME_ASSETS_JS_URL . '/vendor/slick.min.js',
[ 'jquery' ],
'1.5.8',
false,
],
2 => [
'theme',
THEME_ASSETS_JS_URL . '/theme.min.js',
[ 'jquery' ],
'1.0.0',
false,
],
];
$header->load_scripts( $scripts );
// Set viewport in head
$header->set_viewport( '<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />' );
/*
* Load utility classes of InfoTown package.
*/
require_once( get_template_directory() . '/inc/utils/class.attachments.php' );
require_once( get_template_directory() . '/inc/utils/class.categories.php' );
require_once( get_template_directory() . '/inc/utils/class.categories-view.php' );
require_once( get_template_directory() . '/inc/utils/class.date.php' );
require_once( get_template_directory() . '/inc/utils/class.device.php' );
require_once( get_template_directory() . '/inc/utils/class.links.php' );
require_once( get_template_directory() . '/inc/utils/class.str.php' );
/*
* Load short code classes of InfoTown package.
*/
require_once( get_template_directory() . '/inc/shortcode/class.shortcode-images.php' );
require_once( get_template_directory() . '/inc/shortcode/class.shortcode-multibr.php' );
require_once( get_template_directory() . '/inc/shortcode/class.shortcode-bloginfo.php' );
add_shortcode( 'br', [ new InfoTown_Shortcode_Multibr(), 'br' ] );
add_shortcode( 'img', [ new InfoTown_Shortcode_Images(), 'img' ] );
add_shortcode( 'bloginfo', [ new InfoTown_Shortcode_Bloginfo(), 'bloginfo' ] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment