• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

関数リファレンス/wp register style

提供: WordPress Codex 日本語版
< 関数リファレンス
2018年6月7日 (木) 11:17時点におけるGblsm (トーク | 投稿記録)による版 (改めて en:Function Reference/wp_register_style 01:22, 18 August 2016 Tareiking 版を流し込み。)

移動先: 案内検索

このページ「関数リファレンス/wp register style」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

Description

A safe way to register a CSS style file for later use with wp_enqueue_style().

Usage

<?php wp_register_style( $handle, $src, $deps, $ver, $media ); ?>

Use the wp_enqueue_scripts /en action to call this function. Calling it outside of an action can lead to problems. See #17916 for details.

Parameters

$handle
string) (必須) Name of the stylesheet (which should be unique as it is used to identify the script in the whole system).
初期値: なし
$src
string) (必須) URL to the stylesheet. Example: 'http://example.com/css/mystyle.css'. You should never hardcode URLs to local styles, use plugins_url() (for Plugins) and get_template_directory_uri() (for Themes) to get a proper URL. Remote assets can be specified with a protocol-agnostic URL, i.e. '//otherdomain.com/css/theirstyle.css'.
初期値: なし
$deps
array) (optional) Array of handles of any stylesheets that this stylesheet depends on. Dependent stylesheets will be loaded before this stylesheet.
初期値: array()
$ver
string|boolean) (optional) String specifying the stylesheet version number, if it has one. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the stylesheet. The version is appended to the stylesheet URL as a query string, such as ?ver=3.5.1. By default, or if false, the WordPress version string is used. If null nothing is appended to the URL.
初期値: false
$media
string) (optional) String specifying the media for which this stylesheet has been defined. Examples: 'all', 'screen', 'handheld', 'print'. See this list for the full range of valid CSS-media-types.
初期値: 'all'

Return Values

(bool)
Whether the style has been registered. True on success, false on failure.

Examples

In a Plugin (outside a PHP class)

// Register style sheet.
add_action( 'wp_enqueue_scripts', 'register_plugin_styles' );

/**
 * Register style sheet.
 */
function register_plugin_styles() {
	wp_register_style( 'my-plugin', plugins_url( 'my-plugin/css/plugin.css' ) );
	wp_enqueue_style( 'my-plugin' );
}
  • Assumes the Plugin directory is named 'my-plugin'.
  • Assumes the Plugin style sheet is named 'plugin.css'.

In a Plugin (inside a PHP class)

class my_plugin {

	/**
	 * @TODO Add class constructor description.
	 */
	function __construct() {
		// Register style sheet.
		add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) );
	}

	/**
	 * Register and enqueue style sheet.
	 */
	public function register_plugin_styles() {
		wp_register_style( 'my-plugin', plugins_url( 'my-plugin/css/plugin.css' ) );
		wp_enqueue_style( 'my-plugin' );
	}
}
  • Assumes the Plugin class name is 'my_plugin'.
  • Assumes the Plugin directory is named 'my-plugin'.
  • Assumes the Plugin style sheet is named 'plugin.css'.

Notes

Change Log

  • Since: 2.1 (BackPress version: r79)

Source File

wp_register_style() is located in wp-includes/functions.wp-styles.php.

Resources

Related




関数リファレンステンプレートタグ目次もご覧ください。


最新英語版: WordPress Codex » Function Reference/wp register style最新版との差分

最新英語版: Reference / Functions / wp_register_style()