ホーム >  編集カテゴリー一覧 > めばえ いろいろ > PHP > functions.php > 

使うかもしれないファンクション覚書

2015年11月9日 / functions.php

9. 検索結果から指定したページを削除

検索結果に、特定のページを表示させたくない場合もありますよね。そんな時は、functions.php に以下のコードを追加すればOKです。

functions.php
function fb_search_filter($query) {
if ( !$query -> is_admin && $query -> is_search) {
$query -> set(‘post__not_in’, array(28, 35) );
}
return $query;
}
add_filter( ‘pre_get_posts’, ‘fb_search_filter’ );

‘post__not_in’, array( 28, 35 ) では、表示したくないページの ID を指定します。もちろんもっとたくさん指定することもできます!


 

11 管理画面にもファビコンをつけたい(take7はついているが?)

さっきはブログ自体のファビコンを設定しましたが、管理画面にもファビコンを設定したいです。また、同じファビコンじゃなくて違うファビコンの方が、ぱっと見て分かりやすいかもですね!

管理画面のファビコン

functions.php
function admin_favicon() {
echo ‘<link rel=”shortcut icon” type=”image/x-icon” href=”‘.get_bloginfo(‘template_url’).’/images/admin-favicon.icon” />’;
}

add_action(‘admin_head’, ‘admin_favicon’);


 

13.2. 管理画面からサクッと更新

結構長めの CSS なら上記の方法がいいかなーと思いますけど、ちょっとしたスタイルを指定するくらいだと、わざわざサーバーに CSS ファイルをあげるのも面倒です …。以前はカスタムフィールドを使ってましたが、下記ウィジットが気にいってます!functions.php に以下のコードを追加するだけです。

functions.php

1
//Custom CSS Widget
2
add_action(‘admin_menu’, ‘custom_css_hooks’);
3
add_action(‘save_post’, ‘save_custom_css’);
4
add_action(‘wp_head’,’insert_custom_css’);
5
function custom_css_hooks() {
6
add_meta_box(‘custom_css’, ‘Custom CSS’, ‘custom_css_input’, ‘post’, ‘normal’, ‘high’);
7
add_meta_box(‘custom_css’, ‘Custom CSS’, ‘custom_css_input’, ‘page’, ‘normal’, ‘high’);
8
}
9
function custom_css_input() {
10
global $post;
11
echo ‘<input type=”hidden” name=”custom_css_noncename” id=”custom_css_noncename” value=”‘.wp_create_nonce(‘custom-css’).'” />’;
12
echo ‘<textarea name=”custom_css” id=”custom_css” rows=”5″ cols=”30″ style=”width:100%;”>’.get_post_meta($post->ID,’_custom_css’,true).'</textarea>’;
13
}
14
function save_custom_css($post_id) {
15
if (!wp_verify_nonce($_POST[‘custom_css_noncename’], ‘custom-css’)) return $post_id;
16
if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) return $post_id;
17
$custom_css = $_POST[‘custom_css’];
18
update_post_meta($post_id, ‘_custom_css’, $custom_css);
19
}
20
function insert_custom_css() {
21
if (is_page() || is_single()) {
22
if (have_posts()) : while (have_posts()) : the_post();
23
echo ‘<style type=”text/css”>’.get_post_meta(get_the_ID(), ‘_custom_css’, true).'</style>’;
24
endwhile; endif;
25
rewind_posts();
26
}
27
}


http://weblog.hy-z.com/skill/1095 こちらがよろしいようで・・・ふむふむ三輪さんも利用したのかな?good

14.11. ユーザーIDを指定してメニューを非表示

今度は権限じゃなくて、ユーザーIDを指定して非表示にしてみます。管理者が複数いる場合など、固定ページの部分は、他の管理者に触って欲しくないなーって時に使えますね!

functions.php

function remove_menus () {
global $menu;
global $current_user;
get_currentuserinfo();
if($current_user -> user_login == ‘Nori‘) {
$restricted = array(
__(‘メディア’),
__(‘リンク’)
);
end ($menu);
while (prev($menu)){
$value = explode(‘ ‘,$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:”” , $restricted)){
unset($menu[key($menu)]);
}
}
}
}
add_action(‘admin_menu’, ‘remove_menus’);

if($current_user->user_login == ‘Nori’) の ‘Nori’ の部分がユーザーIDです。こうすると、ユーザーID Nori さんには、メディアとリンクのメニューが表示されません。