默认Wordpress菜单生成的Html代码
代码在菜单列表中li有多个Id,也有多个Class的类,而且定义整个菜单样式,根本不需要这么多选择器,下面介绍删除Wordpress沉余选择器的方法,代码如下
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1); //删除Class选择器 add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1); //删除Id选择器 add_filter('page_css_class', 'my_css_attributes_filter', 100, 1); function my_css_attributes_filter($var) { return is_array($var) ? array_intersect($var, array('current-menu-item','current-post-ancestor','current-menu-ancestor','current-menu-parent')) : ''; //删除当前菜单的四个选择器 }
在开发WordPress主题的过程中我们经常发现有很多多余无用的WordPress自带无效代码。那么就分享一下本人自用的常用移除多余WordPress代码集合。
/********************************************************************** 去除多余代码 **********************************************************************/ add_filter('show_admin_bar', '__return_false'); 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', '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', 'locale_stylesheet'); remove_action('wp_head', 'noindex', 1); remove_action('wp_head', 'wp_print_head_scripts', 9); remove_action('wp_head', 'wp_generator'); remove_action('wp_head', 'rel_canonical'); remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0); remove_action('wp_head', 'wp_oembed_add_host_js'); remove_action('wp_head', 'wp_resource_hints', 2); remove_action('wp_head', 'rest_output_link_wp_head', 10); remove_action('wp_head', 'wp_oembed_add_discovery_links', 10); remove_action('wp_footer', 'wp_print_footer_scripts'); remove_action('publish_future_post', 'check_and_publish_future_post', 10, 1); remove_action('template_redirect', 'wp_shortlink_header', 11, 0); remove_action('template_redirect', 'rest_output_link_header', 11, 0); remove_action('rest_api_init', 'wp_oembed_register_route'); remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4); remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10); remove_filter('oembed_response_data', 'get_oembed_response_data_rich', 10, 4); add_filter( 'pre_option_link_manager_enabled', '__return_true' ); add_filter('rest_enabled', '__return_false'); add_filter('rest_jsonp_enabled', '__return_false'); add_shortcode('reply', 'reply_to_read'); add_filter('pre_site_transient_update_core',create_function('$a',"return null;")); // 关闭核心提示 add_filter('pre_site_transient_update_plugins',create_function('$a',"return null;")); // 关闭插件提示 add_filter('pre_site_transient_update_themes',create_function('$a',"return null;")); // 关闭主题提示 remove_action('admin_init','_maybe_update_core');// 禁止 WordPress 检查更新 remove_action('admin_init','_maybe_update_plugins');// 禁止 WordPress 更新插件 remove_action('admin_init','_maybe_update_themes'); // 禁止 WordPress 更新主题 //禁用文章自动保存 add_action('wp_print_scripts','disable_autosave'); function disable_autosave(){wp_deregister_script('autosave'); } //禁用文章修订版本 add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 ); function specs_wp_revisions_to_keep( $num, $post ) {return 0; } // 阻止站内文章互相Pingback function theme_noself_ping( &$links ) {$home = get_theme_mod( 'home' );foreach ( $links as $l => $link )if ( 0 === strpos( $link, $home ) )unset($links[$l]); } add_action('pre_ping','theme_noself_ping');