ワードプレス便利なfunction


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/webstyle/php-fan.org/public_html/wp-content/plugins/wp-syntax/wp-syntax.php on line 383

そろそろワードプレスでよく使うファンクションを残しておきます。
ほとんど自作すれば何とかなる場合があるのですが、ワードプレスで独自関数をなるべく使うようにしておりますので、ここでコピペが出来る感じで残しておきます。すべてfunctions.phpに記述してください。


スポンサーリンク


ワードプレスのアクションフックはここから一覧で載っていますので探しやすいですね~
ワードプレスフックデータベース
ワードプレスのレシピ集です。コチラも色々と参考になります。
WpRecipes

ヘッダー不要なタグ削除

まずヘッダーに表示されるバージョンとかlink relだとか 不要な表記をサクっと消します。

1
2
3
4
5
6
7
8
9
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'wp_generator' );

ファビコン追加

それからファビコンアイコン追加します。一応管理画面も追加されるはず。。。

1
2
3
4
5
6
//追加します
function favicon_link() {
    echo '<link rel="shortcut icon" type="image/x-icon" href="'. get_bloginfo('template_url') . '/common/images/favicon.ico" />' . "\n";
}
add_action('wp_head', 'favicon_link');
add_action('admin_head', 'favicon_link');

管理画面カスタマイズ

管理画面のCSSを変更、、管理画面のID・パスワードを表示するロゴを変えたかったのです。
ワードプレス管理画面

1
2
3
4
function custom_login() {
  echo '<link rel="stylesheet" type="text/css" href="'. get_bloginfo('template_directory') .'/common/css/login.css" />';
}
add_action('login_head', 'custom_login');

これで管理画面用のCSSを追加して変更します。

1
2
3
4
5
@charset "utf-8";
 
#login h1 a {
	background: url(../images/loginlogo.png) no-repeat center center;
}

これで管理画面のロゴが変わります。

それからロゴにリンクとタイトルがついていてリンク先がワードプレスのURLになっているのでこれを変えたいと思ったんです。
マルチブログ化していたらこのURLもメインURLに変わるんですが、通常のワードプレスでは、なかなかそんなやり方の情報がなくて結構探し回ってもないのでjQueryで一時的に対応しました。
けど、やはり、コレはダメだな~、どっかにあるはずと思ったらこんなプラグインがありました。
WP-totalHacks
おお~、コレ使っても良いかもしれないけどあくまでfunctionsに追加したかったので中身を見てどこが変える場所か特定出来ました!

1
2
3
4
5
6
7
8
9
function login_header_url() {
	return get_bloginfo('url');	
}
 
function login_header_title() {
	return 'タイトルはここで変更';	
}
add_filter('login_headerurl',   'login_header_url');
add_filter('login_headertitle', 'login_header_title');

login_header_url関数は自分のサイトのURLを返しますが、return ‘http://www.xxx.xxx/’にすれば好きなURLに飛ばすことができます。

管理画面の中のロゴを非表示

1
2
3
4
function remove_admin_bar_menu($wp_admin_bar) {
    $wp_admin_bar->remove_menu('wp-logo');
}
add_action('admin_bar_menu', 'remove_admin_bar_menu', 70);

管理画面のフッター表示変更
ワードプレス管理画面フッター

1
2
3
4
5
function admin_footertext() {
	$text = '<a href="https://php-fan.org" target="_blank">PHPサンプル実験室</a>';
	return $text ;
}
add_filter('admin_footer_text', 'admin_footertext');

それからバージョンアップのお知らせを非表示の方法、管理者権限の方以外が見えなくなる方法です。

1
2
3
4
5
6
if (!current_user_can('edit_users')) {
  function wphidenag() {
    remove_action( 'admin_notices', 'update_nag');
  }
  add_action('admin_menu','wphidenag');
}

#moreリンク削除

もっと読むの#リンクを取り除く、これってリンクしたら途中まで進むやつですね。
個人的に入らない気がしますね~。

1
2
3
4
5
function custom_content_more_link( $output ) {
    $output = preg_replace('/#more-[\d]+/i', '', $output );
    return $output;
}
add_filter( 'the_content_more_link', 'custom_content_more_link' );

アイキャッチ画像表示

これは定番!アイキャッチ画像を表示させます。テーマによっては予め設定されているかもしれません。

1
add_theme_support('post-thumbnails');

表示させるときはコレで出力

1
<?php the_post_thumbnail(); ?>

以上便利な関数でした。
面白い関数があれば随時更新していきたいと思います。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です