How to Exclude Headings by CSS Class from Your Easy Table of Contents
Content
Many WordPress users of the Easy Table of Contents plugin encounter a common issue: unwanted headings from dynamic content, page builders, or other plugins appearing in their automatically generated table. The built-in "Exclude by Text" feature isn't always practical, especially when dealing with content that changes or is generated automatically.
This guide explains the most effective solution: using a custom PHP filter to exclude headings based on their CSS class.
Why Excluding by Class is Necessary
The native settings in Easy Table of Contents allow you to exclude headings by their text content. However, this method falls short for several scenarios:
- Dynamically generated content: Headings from plugins like WooCommerce, recipe cards, or form builders often have text that changes on every page.
- Page builder elements: Themes and builders like Elementor or Divi may inject headings with consistent classes but different text.
- Reusable components: If you use the same component across multiple pages, you want to exclude its heading by a single, stable identifier (a class), not by manually entering every possible text variation.
The Solution: Using the ez_toc_exclude_by_selector Filter
Fortunately, the Easy Table of Contents plugin provides a powerful filter hook, ez_toc_exclude_by_selector, for developers and advanced users. This filter allows you to specify CSS selectors; any heading that matches these selectors will be omitted from the table of contents.
Step-by-Step Implementation
Warning: This solution involves adding custom code to your website. Always back up your site before proceeding.
- Identify the CSS Class: Use your browser's inspector tool to examine the heading you want to exclude. Find the class name assigned to it. For example, a WooCommerce product title might have the class
woocommerce-loop-product__title. - Add the Code Snippet: You need to add the following PHP code to your website. The safest way to do this is by using a code snippet management plugin like "Code Snippets." Alternatively, you can add it to your child theme's
functions.phpfile.
/**
* Exclude headings with a specific CSS class from TOC
*/
add_filter( 'ez_toc_exclude_by_selector', function( $selectors ) {
// Add your CSS selector(s) here. Prefix class names with a period.
$selectors[] = '.your-custom-class-here';
$selectors[] = '.another-class-to-exclude';
return $selectors;
});
- Customize the Code: Replace
.your-custom-class-herewith the actual class name you identified in step 1. For example, to exclude the WooCommerce product title, you would use:
$selectors[] = '.woocommerce-loop-product__title';
- Save and Test: Save the code snippet and clear any caching on your site. Refresh a page with a table of contents to confirm the heading with that class is now excluded.
Example Use Cases from the Community
- WooCommerce: Exclude product titles in loops using
.woocommerce-loop-product__title. - WP Recipe Maker: Exclude recipe names using
.wprm-recipe-name. - Ninja Forms: Exclude template script tags that are mistaken for headings.
- Page Builders: Exclude headings within specific containers (e.g.,
.et_bloom_below_postfor Elegant Themes' Bloom plugin).
Troubleshooting Common Problems
- Code doesn't work: Double-check that you've prefixed your class name with a period (.) and that the class name is spelled correctly. Ensure the code snippet is active and there are no syntax errors.
- It stopped working after an update: While rare, plugin updates can sometimes change how filters work. The
ez_toc_exclude_by_selectorfilter has been the consistent, recommended method for years, but it's good to verify it's still functioning after a major plugin update. - Shortcode vs. Auto-insert: This filter works for both automatically inserted tables of contents and those generated via the
[ez-toc]shortcode.
By leveraging this filter, you gain precise control over what appears in your table of contents, ensuring a clean and relevant experience for your readers.
Related Support Threads Support
-
Exclude class from TOChttps://wordpress.org/support/topic/exclude-class-from-toc/
-
Exclude by Class programatically?https://wordpress.org/support/topic/exclude-by-classname-programatically/
-
Exclude Ninja Forms invisible H3 from scripthttps://wordpress.org/support/topic/exclude-ninja-forms-invisible-h3-from-script/
-
Widget does not auto highlight h3https://wordpress.org/support/topic/widget-does-not-auto-highlight-h3/
-
Does exclude by selector not work with shortcodes?https://wordpress.org/support/topic/does-exclude-by-selector-not-work-with-shortcodes/
-
Exclusion of headings by CSS classhttps://wordpress.org/support/topic/exclusion-of-headings-by-css-class/
-
H2 class exclusion not workinghttps://wordpress.org/support/topic/h2-class-exclusion-not-working/
-
Feature Request: Exclude by classhttps://wordpress.org/support/topic/feature-request-exclude-by-class/
-
problem exclude h2https://wordpress.org/support/topic/problem-exclude-h2/
-
How to exclude Headings by CSS class (Solution)https://wordpress.org/support/topic/how-to-exclude-headings-by-css-class-solution/
-
Missing H2 from Mediavine Create recipe cardhttps://wordpress.org/support/topic/missing-h2-from-mediavine-create-recipe-card/