Web制作忘備録

function.phpの書き方と中身

トップ MEMORANDAM WORDPRESS

function.phpの書き方と中身

function.phpは色々書いて長文になりやすいため外部ファイルで管理すると見やすい。

外部ファイルを作成

//ファイルパスは任意
//テーマフォルダ内のset_funcフォルダの中のfunc_remove-src_add-setting.phpなので下記の記載になる

require get_template_directory() . '/set_func/func_remove-src_add-setting.php';

WPのデフォルトタグを調整

//ヘッダーのソース消し
remove_action('wp_head', 'wp_resource_hints', 2);
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('admin_print_scripts', 'print_emoji_detection_script');

remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_action('wp_head', 'rest_output_link_wp_head');
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
//nextpage,prevpage
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
//canonicalのmetaタグ
remove_action('wp_head', 'rel_canonical');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
//いらないcss削除
remove_action('wp_head', 'wp_print_styles', 8);
remove_action('wp_head', 'wp_print_head_scripts', 9);
function remove_recent_comments_style()
{
    global $wp_widget_factory;
    remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
}

add_action('widgets_init', 'remove_recent_comments_style');

エディタの調整

/*---------- エディタのビジュアル・テキスト切替で空タグ消滅を防止 ----------*/
function my_tiny_mce_before_init($init_array)
{
    $init_array['valid_elements'] = '*[*]';
    $init_array['extended_valid_elements'] = '*[*]';
    return $init_array;
}

add_filter('tiny_mce_before_init', 'my_tiny_mce_before_init');
/*---------- エディタにスタイルシート適応 ----------*/
add_editor_style("editor_style.css");

/*---------- ACFエディタにテンプレートボタン追加 ----------*/
add_filter('tinymce_templates_enable_media_buttons', function () {
return true;
});

/*---------- img吐き出し時いらないタグを除去 ----------*/
add_filter('image_send_to_editor', 'remove_image_attribute', 10);
add_filter('post_thumbnail_html', 'remove_image_attribute', 10);
function remove_image_attribute($html)
{
$html = preg_replace('/(width|height)="\d*"\s/', '', $html); // width と heifht を削除
$html = preg_replace('/class=[\'"]([^\'"]+)[\'"]/i', '', $html); // class を削除
$html = preg_replace('/title=[\'"]([^\'"]+)[\'"]/i', '', $html); // title を削除
$html = preg_replace('/<a href=".+">/', '', $html); // a タグを削除
$html = preg_replace('/<\/a>/', '', $html); // a の閉じタグのを削除
return $html;
}
/*--------------------- 管理画面にfavicon ---------------------*/
function ad_favicon_dashboard()
{
echo '<link rel="shortcut icon" type="image/x-icon" href="/main/wp-content/themes/sugioka/images/favicon.ico">';
}
add_action('admin_head', 'ad_favicon_dashboard');

/*---------- 画像やリンクの出力URLを相対パスに ----------*/
function delete_domain_from_attachment_url($url)
{
if (preg_match('/^http(s)?:\/\/[^\/\s]+(.*)$/', $url, $match)) {
$url = $match[2];
}
return $url;
}

/*--------------------- ブロックエディタにcss適応 ---------------------*/
add_action('after_setup_theme', 'gutenberg_editor_css_setup_theme');
function gutenberg_editor_css_setup_theme()
{
add_theme_support('editor-styles');
add_editor_style('editor-style.css');
}