实例演示删除或禁用WordPress中不必要的功能

都知道WordPress的功能非常强大,尤其是在安装插件后,几乎可以做成任何类型的网站,从简单的日记型博客到复杂的电子商务网站,但是功能越多,安全隐患也就越多,对于部分使用者来说, WordPress 的大部分功能可能都用不上,那么折用不上的功能我们可以移除或者禁用,以此来降低风险,也可减少网站的冗余。本文和大家介绍移除或禁用几个典型的不常用的WordPress功能。

1、移除 Feed

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

2、移除 Meta 中的 Post 信息

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 ); 

3、移除 emoji 和对应的 CSS

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

4、移除 Json 链接

remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );

5、移除 Shortlink

remove_action('wp_head', 'wp_shortlink_wp_head'); 

6、移除 DNSPrefetch

function wopus_remove_dns_prefetch( $hints, $relation_type ) {
  if ( 'dns-prefetch' === $relation_type ) {
    return array_diff( wp_dependencies_unique_hosts(), $hints );
  }

  return $hints;
}
add_filter( 'wp_resource_hints', 'wopus_remove_dns_prefetch', 10, 2 );

7、禁用 embed、REST API、oEmbed 相关

function wopus_disable_embeds_init() {
  global $wp;

  // Remove the embed query var.
  $wp->public_query_vars = array_diff( $wp->public_query_vars, array(
    'embed',
  ) );

  // Remove the REST API endpoint.
  remove_action( 'rest_api_init', 'wp_oembed_register_route' );

  // Turn off
  add_filter( 'embed_oembed_discover', '__return_false' );

  // Don't filter oEmbed results.
  remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );

  // Remove oEmbed discovery links.
  remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

  // Remove oEmbed-specific JavaScript from the front-end and back-end.
  remove_action( 'wp_head', 'wp_oembed_add_host_js' );
  add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );

  // Remove all embeds rewrite rules.
  add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
}
add_action( 'init', 'wopus_disable_embeds_init', 9999 );

8、移除 wpembed TinyMCE 插件

function wopus_disable_embeds_tiny_mce_plugin( $plugins ) {
  return array_diff( $plugins, array( 'wpembed' ) );
}

9、移除所有 embeds 相关 Rewrite 规则

/**
 *   移除所有 embeds 相关 Rewrite 规则
 */
function wopus_disable_embeds_rewrites( $rules ) {
  foreach ( $rules as $rule => $rewrite ) {
    if ( false !== strpos( $rewrite, 'embed=true' ) ) {
      unset( $rules[ $rule ] );
    }
  }
  return $rules;
}
/**
 *   移除插件启用时 embeds 相关 Rewrite 规则
 */
function wopus_disable_embeds_remove_rewrite_rules() {
  add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
  flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'disable_embeds_remove_rewrite_rules' );

/**
 *   冲掉插件停用时 embeds 相关 Rewrite 规则
 */
function wopus_disable_embeds_flush_rewrite_rules() {
  remove_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
  flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'disable_embeds_flush_rewrite_rules' );

以上是手动禁用或移除 WordPress功能 的方法,当然我们也可以借助 WordPress 插件来实现。

4.4/5 - (5 votes)
© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享