117.info
人生若只如初见

dux主题自定义配置代码增强功能

很多实用dux主题的小伙伴,都因为dux本身没有设置一些功能而用来不方便,比如没有禁止古腾堡小工具啥的,老秦根据自己的使用习惯,准备了一下dux主题自定义代码,可以让dux更好用。

可以将下面代码段放入dux主题目录中的functions.php文件的// Customize your functions下面即可。

老秦已经做好每个代码的注释,自己根据需求选择。

//标签自动别名

add_action('create_term', 'my_custom_term_slug', $priority = 10, $accepted_args = 3);

function my_custom_term_slug($term_id, $tt_id, $taxonomy) {

if ($taxonomy == 'post_tag') {

wp_update_term($term_id, $taxonomy, $args = array('slug' => 't-' . $term_id));

}

}

//WordPress 移除头部 global-styles-inline-css 

add_action('wp_enqueue_scripts', 'fanly_remove_global_styles_inline'); 

function fanly_remove_global_styles_inline(){ 

wp_deregister_style( 'global-styles' ); 

wp_dequeue_style( 'global-styles' ); 

}

// 屏蔽古腾堡编辑器,恢复经典编辑器,因为新的dux主题自带这个功能开关,所以这里注释掉了

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

//remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );

//禁用古腾堡风格小工具

add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' );

add_filter( 'use_widgets_block_editor', '__return_false' );

//禁用谷腾堡之后已出classic-theme-styles

add_action( 'wp_enqueue_scripts', function() {

wp_dequeue_style( 'classic-theme-styles' );

}, 20 );

// 页面链接添加html后缀

add_action('init', 'html_page_permalink', -1);

function html_page_permalink() {

    global $wp_rewrite;

    if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){

        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';

    }

}

//目录添加斜杠

function nice_trailingslashit($string, $type_of_url) {

    if ( $type_of_url != 'single' && $type_of_url != 'page' )

      $string = trailingslashit($string);

    return $string;

}

add_filter('user_trailingslashit', 'nice_trailingslashit', 10, 2);

//禁用s.w.org

add_filter( 'emoji_svg_url', '__return_false' );

//移除短链接shortlink

remove_action('wp_head','wp_shortlink_wp_head',10,0);

//删除wlwmanifest_link

remove_action( 'wp_head', 'wlwmanifest_link' );

//wordpress优化禁用xmlrpc.php并删除前台链接

add_filter('xmlrpc_enabled','__return_false');

remove_action( 'wp_head', 'rsd_link' );

//禁用 REST API、移除 wp-json

add_filter('rest_enabled', '_return_false');

add_filter('rest_jsonp_enabled', '_return_false');

remove_action( 'wp_head', 'rest_output_link_wp_head' );

remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 );

// 首先移除多余的feed链接

remove_action( 'wp_head','feed_links', 2 );

remove_action( 'wp_head','feed_links_extra', 3 );

//禁用健康检查

function prefix_remove_site_health( $tests ) {

unset( $tests['direct']['php_version'] );

unset( $tests['direct']['wordpress_version'] );

unset( $tests['direct']['plugin_version'] );

unset( $tests['direct']['theme_version'] );

unset( $tests['direct']['sql_server'] );

unset( $tests['direct']['php_extensions'] );

unset( $tests['direct']['utf8mb4_support'] );

unset( $tests['direct']['https_status'] );

unset( $tests['direct']['ssl_support'] );

unset( $tests['direct']['scheduled_events'] );

unset( $tests['direct']['http_requests'] );

unset( $tests['direct']['is_in_debug_mode'] );

unset( $tests['direct']['dotorg_communication'] );

unset( $tests['direct']['background_updates'] );

unset( $tests['direct']['loopback_requests'] );

unset( $tests['direct']['rest_availability'] );

return $tests;

}

add_filter( 'site_status_tests', 'prefix_remove_site_health' );

//禁用自带sitemap

add_filter( 'wp_sitemaps_enabled', '__return_false' );

//禁止更新

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 更新主题

//文章图片自动添加alt和title属性

add_filter( 'the_content', 'image_alt_tag');

function image_alt_tag($content){

global $post;preg_match_all('/<img (.*?)\/>/', $content, $images);

if(!is_null($images)) {

foreach($images[1] as $index => $value){

$new_img = str_replace('<img', '<img alt="'.get_the_title().'" title="'.get_the_title().'-'.get_bloginfo('name').'"', $images[0][$index]);

$content = str_replace($images[0][$index], $new_img, $content);

}

}

return $content;

}

// 移除WordPress加载的JS和CSS链接中的版本号

function wpdaxue_remove_cssjs_ver( $src ) {

if( strpos( $src, 'ver=' ) )

$src = remove_query_arg( 'ver', $src );

return $src;

}

add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );

add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
赞(0)
未经允许不得转载:117笔记 » dux主题自定义配置代码增强功能
分享到