File "functions.php"

Full path: /home/xs854402/smcse.co.jp/public_html/test.smcse.co.jp/wp-content/themes/Sumitomo-Mitsui/functions.php
File size: 18.52 B (18.52 KB bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Edit   Advanced Editor   Back

<?php

// add_filter( 'show_admin_bar', '__return_false' );

function my_remove_blanklines( $content ) {
	return preg_replace( '/(\s|&nbsp;|<p>&nbsp;<\/p>)+$/', '', $content );
}
add_filter( 'content_save_pre', 'my_remove_blanklines' );

/* テーマの設定 */
function my_setup() {
    add_theme_support( 'title-tag' );
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'my-blog', 350, 233, true );
    add_theme_support( 'html5', array(
        'search-form'
    ) );
    register_nav_menus(array('global' => 'global menu'));
}
add_action( 'after_setup_theme', 'my_setup' );

/* CSSとJavaScriptの読み込み */
function my_script_init() {
    wp_enqueue_style('googlefonts', 'https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@100..900&display=swap', array(), null );
    wp_enqueue_style( 'style-name', get_template_directory_uri() . '/css/styles.css?2026061703', array(), '1.0.1' );
    wp_enqueue_style( 'swiper', 'https://cdnjs.cloudflare.com/ajax/libs/Swiper/9.3.2/swiper-bundle.min.css', array(), '1.0.1' );
    wp_deregister_script( 'jquery' ); // WordPressのjquery.jsを読み込ませない
    wp_enqueue_script( 'jquery', 'https://code.jquery.com/jquery-3.6.3.min.js','', '3.6.0' );
    wp_enqueue_script( 'swiper-js', 'https://cdnjs.cloudflare.com/ajax/libs/Swiper/9.3.2/swiper-bundle.min.js', '', '9.3.2');
    if(is_page(array('contact', 'entry'))) {
        wp_enqueue_script( 'form', 'https://form.movabletype.net/dist/parent-loader.js', '', '1.0.0', array(
            'in_footer' => true,
            'strategy' => 'async',
        ));
    }
    wp_enqueue_script( 'main', get_template_directory_uri() . '/js/script.js', array('jquery'), '1.0.1', true );
}
add_action( 'wp_enqueue_scripts', 'my_script_init' );

function custom_style_loader_tag($tag, $handle) {
    if($handle == 'swiper') {
        return str_replace(' />', ' integrity="sha384-rS8jUBdvoP7mlBEvzy/RLM6+1bbCfWXccKbcnEZWKzJW/w+jU4JYTebYAd2fzur/" crossorigin="anonymous" referrerpolicy="no-referrer" />', $tag);
    }else{
        return $tag;
    }
}
add_filter('style_loader_tag', 'custom_style_loader_tag', 10, 2);

function custom_script_loader_tag($tag, $handle) {
    if($handle == 'jquery') {
        return str_replace('></script>', ' integrity="sha384-Ft/vb48LwsAEtgltj7o+6vtS2esTU9PCpDqcXs4OCVQFZu5BqprHtUCZ4kjK+bpE" crossorigin="anonymous"></script>', $tag);
    }else if($handle == 'swiper-js') {
        return str_replace('></script>', ' integrity="sha384-zQeyhcRgg1GEhcSLv+EHGKZCTDN+vGVxYBtHJHKvQhsYvmYzNGm/mlvU6aQvHMw/" crossorigin="anonymous" referrerpolicy="no-referrer"></script>', $tag);
    }else{
        return $tag;
    }
}
add_filter('script_loader_tag', 'custom_script_loader_tag', 10, 2);

/* カスタム投稿作成 - お知らせ */
function create_post_type_news() {
    register_post_type(
        'news', // 投稿タイプ名(スラッグ)
        array(
            'labels' => array(
                'name' => __('お知らせ'), // 投稿タイプ名(表示名)
                'hierarchical' => false,
                'singular_name' => __('お知らせ') // 投稿タイプ名(表示名)
            ),
            'menu_icon' => 'dashicons-building', //管理画面で表示するアイコン
            'public' => true,
            'menu_position' => 5,
            'supports' => array(
                'title',
                'editor',
                'thumbnail'
            ), // この投稿タイプでサポートする機能。
            'has_archive' => true,
            'show_in_rest' => true,
            'rewrite' => array(
                'slug' => 'news', //投稿タイプ名(スラッグ) アーカイブページのURLになります
                'with_front' => false
            )
        )
    );

    register_taxonomy(
        'news-cat',
        'news',
        array(
            'label' => 'お知らせカテゴリー',
            'hierarchical' => true,
            'public' => true,
            'show_in_rest' => true,
        )
    );
}
add_action( 'init', 'create_post_type_news' );

/* カスタム投稿作成 - 施工実績 */
function create_post_type_construction() {
    register_post_type(
        'construction', // 投稿タイプ名(スラッグ)
        array(
            'labels' => array(
                'name' => __('施工実績'), // 投稿タイプ名(表示名)
                'hierarchical' => false,
                'singular_name' => __('施工実績') // 投稿タイプ名(表示名)
            ),
            'menu_icon' => 'dashicons-building', //管理画面で表示するアイコン
            'public' => true,
            'menu_position' => 5,
            'supports' => array(
                'title',
                'editor',
                'thumbnail'
            ), // この投稿タイプでサポートする機能。
            'has_archive' => true,
            'show_in_rest' => true,
            'rewrite' => array(
                'slug' => 'construction', //投稿タイプ名(スラッグ) アーカイブページのURLになります
                'with_front' => false
            )
        )
    );

    register_taxonomy(
        'construction-cat',
        'construction',
        array(
            'label' => '施工実績カテゴリー',
            'hierarchical' => true,
            'public' => true,
            'show_in_rest' => true,
        )
    );
}
add_action( 'init', 'create_post_type_construction' );

/* カスタム投稿作成 - 技術紹介 */
function create_post_type_products() {
    register_post_type(
        'products', // 投稿タイプ名(スラッグ)
        array(
            'labels' => array(
                'name' => __('技術紹介'), // 投稿タイプ名(表示名)
                'hierarchical' => false,
                'singular_name' => __('技術紹介') // 投稿タイプ名(表示名)
            ),
            'menu_icon' => 'dashicons-building', //管理画面で表示するアイコン
            'public' => true,
            'menu_position' => 5,
            'supports' => array(
                'title',
                'editor',
                'thumbnail'
            ), // この投稿タイプでサポートする機能。
            'has_archive' => true,
            'show_in_rest' => true,
            'rewrite' => array(
                'slug' => 'products', //投稿タイプ名(スラッグ) アーカイブページのURLになります
                'with_front' => false
            )
        )
    );

    register_taxonomy(
        'products-cat',
        'products',
        array(
            'label' => '技術紹介カテゴリー',
            'hierarchical' => true,
            'public' => true,
            'show_in_rest' => true,
        )
    );
}
add_action( 'init', 'create_post_type_products' );

/* カスタム投稿作成 - 社員インタビュー */
function create_post_type_interview() {
    register_post_type(
        'interview', // 投稿タイプ名(スラッグ)
        array(
            'labels' => array(
                'name' => __('社員インタビュー'), // 投稿タイプ名(表示名)
                'hierarchical' => false,
                'singular_name' => __('社員インタビュー') // 投稿タイプ名(表示名)
            ),
            'menu_icon' => 'dashicons-businessperson', //管理画面で表示するアイコン
            'public' => true,
            'menu_position' => 5,
            'supports' => array(
                'title',
                'editor',
                'thumbnail'
            ), // この投稿タイプでサポートする機能。
            'has_archive' => true,
            'show_in_rest' => true,
            'rewrite' => array(
                'slug' => 'employment/interview', //投稿タイプ名(スラッグ) アーカイブページのURLになります
                'with_front' => false
            )
        )
    );

    register_taxonomy(
        'interview-cat',
        'interview',
        array(
            'label' => 'インタビューカテゴリー',
            'hierarchical' => true,
            'public' => true,
            'show_in_rest' => true,
        )
    );
}
add_action( 'init', 'create_post_type_interview' );

/* カスタム投稿作成 - 求人検索 */
function create_post_type_jobs() {
    register_post_type(
        'jobs', // 投稿タイプ名(スラッグ)
        array(
            'labels' => array(
                'name' => __('求人検索'), // 投稿タイプ名(表示名)
                'hierarchical' => true,
                'singular_name' => __('求人検索') // 投稿タイプ名(表示名)
            ),
            'menu_icon' => 'dashicons-businessperson', //管理画面で表示するアイコン
            'public' => true,
            'menu_position' => 5,
            'supports' => array(
                'title',
                'editor',
                'thumbnail'
            ), // この投稿タイプでサポートする機能。
            'has_archive' => true,
            'show_in_rest' => true,
            'rewrite' => array(
                'slug' => 'employment/jobs', //投稿タイプ名(スラッグ) アーカイブページのURLになります
                'with_front' => false
            )
        )
    );

    register_taxonomy(
        'jobs-cat',
        'jobs',
        array(
            'label' => '求人検索カテゴリー',
            'hierarchical' => true,
            'public' => true,
            'show_in_rest' => true,
        )
    );

    register_taxonomy(
        'jobs-tag',
        'jobs',
        array(
            'label' => '求人検索タグ',
            'hierarchical' => true,
            'public' => true,
            'show_in_rest' => true,
        )
    );
}
add_action( 'init', 'create_post_type_jobs' );

/* カスタム投稿作成 - スタッフブログ */
function create_post_type_blog() {
    register_post_type(
        'blog', // 投稿タイプ名(スラッグ)
        array(
            'labels' => array(
                'name' => __('スタッフブログ'), // 投稿タイプ名(表示名)
                'hierarchical' => false,
                'singular_name' => __('スタッフブログ') // 投稿タイプ名(表示名)
            ),
            'menu_icon' => 'dashicons-businessperson', //管理画面で表示するアイコン
            'public' => true,
            'menu_position' => 5,
            'supports' => array(
                'title',
                'editor',
                'thumbnail'
            ), // この投稿タイプでサポートする機能。
            'has_archive' => true,
            'show_in_rest' => true,
            'rewrite' => array(
                'slug' => 'employment/blog', //投稿タイプ名(スラッグ) アーカイブページのURLになります
                'with_front' => false
            )
        )
    );

    register_taxonomy(
        'blog-cat',
        'blog',
        array(
            'label' => 'スタッフブログカテゴリー',
            'hierarchical' => true,
            'public' => true,
            'show_in_rest' => true,
        )
    );
}
add_action( 'init', 'create_post_type_blog' );

/* カスタム投稿作成 - インターンシップ */
function create_post_type_internship() {
    register_post_type(
        'internship', // 投稿タイプ名(スラッグ)
        array(
            'labels' => array(
                'name' => __('インターンシップ'), // 投稿タイプ名(表示名)
                'hierarchical' => false,
                'singular_name' => __('インターンシップ') // 投稿タイプ名(表示名)
            ),
            'menu_icon' => 'dashicons-businessperson', //管理画面で表示するアイコン
            'public' => true,
            'menu_position' => 5,
            'supports' => array(
                'title',
                'editor',
                'thumbnail'
            ), // この投稿タイプでサポートする機能。
            'has_archive' => true,
            'show_in_rest' => true,
            'rewrite' => array(
                'slug' => 'employment/internship', //投稿タイプ名(スラッグ) アーカイブページのURLになります
                'with_front' => false
            )
        )
    );
}
add_action( 'init', 'create_post_type_internship' );

/* カスタム投稿一覧にカテゴリーを表示 */
function add_custom_column($column) {
    global $post_type;
    // カスタム投稿のスラッグを記載
    if ($post_type === 'interview') {
        // カスタム投稿のタクソノミーを記載
        $column['interview-cat'] = 'カテゴリ-';
    }
    if ($post_type === 'blog') {
        // カスタム投稿のタクソノミーを記載
        $column['blog-cat'] = 'カテゴリ-';
    }
    return $column;
}
add_filter('manage_posts_columns', 'add_custom_column');

function add_custom_column_id($column_name, $id) {
    // カスタム投稿のタクソノミーを記載
    if ($column_name === 'interview-cat') {
        echo get_the_term_list($id, 'interview-cat', '', ', ');
    }
}
// manage_"カスタム投稿のタクソノミーを記載"_custom_column
add_action('manage_interview_posts_custom_column', 'add_custom_column_id', 10, 2);

function add_custom_blog_id($column_name, $id) {
    // カスタム投稿のタクソノミーを記載
    if ($column_name === 'blog-cat') {
        echo get_the_term_list($id, 'blog-cat', '', ', ');
    }
}
// manage_"カスタム投稿のタクソノミーを記載"_custom_column
add_action('manage_blog_posts_custom_column', 'add_custom_blog_id', 10, 2);

/* 投稿メニューを非表示 */
function remove_menus () {
    remove_menu_page('edit.php');
}
add_action('admin_menu', 'remove_menus');

/* メディアのリサイズ停止 */
add_filter( 'big_image_size_threshold', '__return_false' );

// 記事の閲覧回数を計測
function set_post_views($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

/* 抜粋の文字数調整 */
function my_excerpt_length($length) {
    return 80;
}
add_filter('excerpt_length', 'my_excerpt_length');

/* 抜粋の省略文字 */
function my_excerpt_more($more) {
    return '…';
}
add_filter('excerpt_more' , 'my_excerpt_more' );

/* カスタム投稿にタグを紐付け */
function add_tags() {
    register_taxonomy( 'post_tag', [ 'post', 'blog' ],
        [
            'hierarchical' => false,
            'query_var'    => 'tag',
            'show_in_rest' => true, // ブロックエディタ対応
			'rewrite' => array( 'slug' => 'tag' ),
        ]
    );
};
add_action( 'init', 'add_tags' );

function my_modify_main_query($query) {
    if ( is_admin() && ! $query->is_main_query() ) {
        return;
    }
    if ( $query->is_category() || $query->is_tag() ) {
        $query->set('post_type', ['post','blog']);
    }
};
add_action( 'pre_get_posts', 'my_modify_main_query' );

/* カスタム投稿一覧にカテゴリーを表示 */
function add_custom_column_construction($column) {
    global $post_type;
    if ($post_type === 'construction') {
        $column['construction-cat'] = 'カテゴリ';
    }
    if ($post_type === 'products') {
        $column['products-cat'] = 'カテゴリ';
    }
    if ($post_type === 'jobs') {
        $column['jobs-cat'] = 'カテゴリ';
        $column['jobs-tag'] = 'タグ';
    }
    if ($post_type === 'news') {
        $column['news-cat'] = 'カテゴリ';
    }
    return $column;
}
add_filter('manage_posts_columns', 'add_custom_column_construction');

function add_custom_column_construction_id($column_name, $id) {
    if ($column_name === 'construction-cat') {
        echo get_the_term_list($id, 'construction-cat', '', ', ');
    }
}
add_action('manage_construction_posts_custom_column', 'add_custom_column_construction_id', 10, 2);

function add_custom_column_products_id($column_name, $id) {
    if ($column_name === 'products-cat') {
        echo get_the_term_list($id, 'products-cat', '', ', ');
    }
}
add_action('manage_products_posts_custom_column', 'add_custom_column_products_id', 10, 2);

function add_custom_column_jobs_id($column_name, $id) {
    if ($column_name === 'jobs-cat') {
        echo get_the_term_list($id, 'jobs-cat', '', ', ');
    }
    if ($column_name === 'jobs-tag') {
        echo get_the_term_list($id, 'jobs-tag', '', ', ');
    }
}
add_action('manage_jobs_posts_custom_column', 'add_custom_column_jobs_id', 10, 2);

function add_custom_column_news_id($column_name, $id) {
    if ($column_name === 'news-cat') {
        echo get_the_term_list($id, 'news-cat', '', ', ');
    }
}
add_action('manage_news_posts_custom_column', 'add_custom_column_news_id', 10, 2);

function custom_menus() {
    global $menu;
    $menu[19] = $menu[10];
    unset($menu[10]);
}
add_action('admin_menu', 'custom_menus');

function my_get_archives_link($html){
    return preg_replace ( '/<\/a>/', '年</a>', $html );
}
add_filter('get_archives_link','my_get_archives_link');

remove_filter('the_content', 'wpautop');

// スラッグ名が日本語だったら自動的に"投稿タイプ_ID"付与へ変更(スラッグを設定した場合は適用しない)
function auto_post_slug( $slug, $post_ID, $post_status, $post_type ) {
    if ( preg_match( '/(%[0-9a-f]{2})+/', $slug ) ) {
        $slug = $post_type . '_' . $post_ID;
    }
    return $slug;
}
add_filter( 'wp_unique_post_slug', 'auto_post_slug', 10, 4  );

/* 記事の自動保存停止 */
function disable_autosave() {
    wp_deregister_script('autosave');
}
add_action('admin_init', 'disable_autosave');

/* functions.phpに記載 */
/* 更新日が公開日より新しい日付かどうか */
function is_new_modified_date($format) {
    $ptime = get_the_time('Ymd');
    $mtime = get_the_modified_time('Ymd');
    return $mtime > $ptime;
}

function add_custom_jobs_entry_rewrite() {
    add_rewrite_rule(
        '^employment/jobs/entry/?$',
        'index.php?pagename=entry',
        'top'
    );
}
add_action('init', 'add_custom_jobs_entry_rewrite');

function flush_rewrite_rules_on_activation() {
    add_custom_jobs_entry_rewrite();
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'flush_rewrite_rules_on_activation');

/* 閲覧者が非ログインの場合コンソールを表示させない */
function disable_console_log_in_production() {
    if (!is_user_logged_in()) {
        echo "<script>console.log = function(){}; console.warn=function(){}; console.error=function(){};</script>";
    }
}
add_action('wp_head', 'disable_console_log_in_production');