今まで、テーマも自分で作っていたから(超シンプル)サイトのカスタマイズは、直接function.phpに書けばよかったけど、外部テーマを使うとまた各場所が変わるね。

とりあえずは今までのカスタマイズの健忘録です。

function require_login() {
    if ( ! is_user_logged_in() && ! preg_match( ‘/^(wp-login\.php|async-upload\.php)/’, basename( $_SERVER[‘REQUEST_URI’] ) ) && ! ( defined( ‘DOING_AJAX’ ) && DOING_AJAX )  && ! ( defined( ‘DOING_CRON’ ) && DOING_CRON ) ) {
        auth_redirect();
    }
}
add_action( ‘init’, ‘require_login’ );

//cronページへのアクセスは出来るように変更(分岐ページも)
    add_action(‘wp’, ‘access_restriction’);
    function access_restriction() {
        if (is_user_logged_in() || is_page(‘cron’) || is_page(‘branch’)) {
            return;
        }else{
        auth_redirect();
        }
  }

cssで非表示にしているだけだが、、

function login_css() {
    echo ‘<style type=”text/css”>#login #nav, #backtoblog {display:none;}</style>’.”\n”;
    }
add_action(‘login_head’, ‘login_css’);

function disable_password_reset() {
              return false;
              }
add_filter ( ‘allow_password_reset’, ‘disable_password_reset’ );

参照 https://tanweb.net/2019/04/16/26316/

// ログイン画面のロゴ変更
function login_logo() {
  echo '<style type="text/css">.login h1 a {background-image: url(ここにURL);width:●●px;height:●●px;background-size:●●px ●●px;}</style>';
}
add_action('login_head', 'login_logo');

参照 https://www.beginnerweb.net/wp.html

会員ページなど、管理者以外への通知はいらない

function disable_autosave() {
 wp_deregister_script(‘autosave’);
}
add_action( ‘wp_print_scripts’, ‘disable_autosave’ );
/*管理画面に任意のcssファイルを読み込む*/
function wp_custom_admin_css() {
    echo “\n” . ‘<link href=”‘ .get_bloginfo(‘template_directory’). ‘/css/wp-admin.css’ . ‘” rel=”stylesheet” type=”text/css” />’ . “\n”;
if (!current_user_can(‘administrator’)) { //admin以外
    echo “\n” . ‘<link href=”‘ .get_bloginfo(‘template_directory’). ‘/css/user.css’ . ‘” rel=”stylesheet” type=”text/css” />’ . “\n”;
    }else{
        echo “\n” . ‘<link href=”‘ .get_bloginfo(‘template_directory’). ‘/css/miyazaki.css’ . ‘” rel=”stylesheet” type=”text/css” />’ . “\n”;
    $screen = get_current_screen();
    
//スケジュール入力ページに
if($screen -> post_type ==’event’) { ?>
  <script type=”text/javascript”>
        jQuery(function(){
            jQuery(‘#eo-start-datetime-label’).text(‘開始日時’);
            jQuery(‘#eo-end-datetime-label’).text(‘終了日時’);
            jQuery(‘.eo-venue-combobox-select’).hide();
        })
        </script>
    <?php } 
  }
add_action(‘admin_head’, ‘wp_custom_admin_css’, 100);
//authorページをリダイレクト
function author_archive_redirect() {
   if( is_author() ) {
       wp_redirect( home_url());
       exit;
   }
}
add_action( 'template_redirect', 'author_archive_redirect' );

ログインすると、管理画面にリダイレクトされるが、会員サイトなど好ましくない。

//リダイレクトURLが設定してあるもの以外はhomeへ

add_filter(“login_redirect”, “edit_login_redirect”, 10, 3);
function edit_login_redirect ($redirect_to, $requested_redirect_to, $user){
 if ( $requested_redirect_to === ” ) {
    return home_url();
  }
  else {
    return $redirect_to;
  }    
}


/*ログアウト後もTOPへ移動*/
add_action(‘wp_logout’,’edit_logout_redirect’);
function edit_logout_redirect(){
    wp_safe_redirect(home_url());
    exit();
}

パスワードを設定する方法はあるが、スマートではない。

function require_login() {
    if ( ! is_user_logged_in() && ! preg_match( ‘/^(wp-login\.php|async-upload\.php)/’, basename( $_SERVER[‘REQUEST_URI’] ) ) && ! ( defined( ‘DOING_AJAX’ ) && DOING_AJAX )  && ! ( defined( ‘DOING_CRON’ ) && DOING_CRON ) ) {
        auth_redirect();
    }
}
add_action( ‘init’, ‘require_login’ );

//cronページへのアクセスは出来るように変更(分岐ページも)
    add_action(‘wp’, ‘access_restriction’);
    function access_restriction() {
        if (is_user_logged_in() || is_page(‘cron’) || is_page(‘branch’)) {
            return;
        }else{
        auth_redirect();
        }
  }
function event_add_autoterm( $post_id  ) {
    global $post;
    $new_post = get_post( $post_id );
    $content = $new_post->post_content;
    if( $new_post->post_type == ‘event’ ) {
        
        $custom_fields = get_post_custom($post_id);
        if($custom_fields[‘kind_event’][0]==’会社’ ) {
            wp_set_object_terms( $post_id, ‘company’, ‘event-category’, false );
            
        }elseif($custom_fields[‘kind_event’][0]==’アドバイザー’ ) {
            $cat = $custom_fields[‘adviser’][0];
            wp_set_object_terms( $post_id, $cat, ‘event-category’, false );
        
    }elseif($custom_fields[‘kind_event’][0]==’個人’ and $custom_fields[‘スケジュール共有’] [0]== ‘共有しない’) {
            //投稿者のスラッグを取得
            $author = $new_post->post_author;
            $author = get_userdata($author);
            $name = $author->user_login.’2′;
        wp_set_object_terms( $post_id, $name, ‘event-category’, false );
        }
            
    }
}
add_action( ‘save_post’, ‘event_add_autoterm’ );