-
Hello Admin Rankmath
Introducing me, Taufiq S, from Indonesia
I Have a Problem (Table Of Context) How to Detect (TOC) I have implemented the code that rankmath provided but it still fails to detect.
<?php /** * Filter to add plugins to the TOC list. * * @param array TOC plugins. */ add_filter( 'rank_math/researches/toc_plugins', function( $toc_plugins ) { $toc_plugins['public_html/wp-content/plugins/pixwell-core/includes/table-contents.php'] = 'table of contents'; return $toc_plugins; });you can see the picture of my application if it is correct.
help us to solve TOC problem.Thank you
Taufiq S
-
Hello,
Thanks for contacting us, and sorry for any inconvenience that might have been caused due to that.
Rank Math only detects the Table of Content if it is added with the compatible plugins.
Most likely, the filter you’ve added is not working due to the incorrect directory path. As the start of the directory in the filter should be after
/plugins/. So, I’d like to suggest you to try replacing the code below with your one:<?php /** * Filter to add plugins to the TOC list. * * @param array TOC plugins. */ add_filter( 'rank_math/researches/toc_plugins', function( $toc_plugins ) { $toc_plugins['pixwell-core/includes/table-contents.php'] = 'table of contents'; return $toc_plugins; });Let us know how it goes. Looking forward to helping you.
Thank you.
Hello Md. Sakib Khandaker
I’ve implemented something like the code you provided, but it still can’t be detected.
You can see my full TOC code below<?php /** Don't load directly */ if ( ! defined( 'ABSPATH' ) ) { exit; } if ( ! class_exists( 'Pixwell_Table_Contents', false ) ) { /** * Class Pixwell_Table_Contents * table of contents */ class Pixwell_Table_Contents { private static $instance; public $settings; public $supported_headings; public static function get_instance() { if ( self::$instance === null ) { return new self(); } return self::$instance; } function __construct() { self::$instance = $this; $this->get_supported_headings(); if ( ! is_admin() ) { add_filter( 'the_content', array( $this, 'the_content' ), 0 ); } } /** get all settings */ public function get_settings() { $this->settings = array( 'post' => $this->get_setting( 'table_contents_post' ), 'page' => $this->get_setting( 'table_contents_page' ), 'enable' => $this->get_setting( 'table_contents_enable' ), 'heading' => $this->get_setting( 'table_contents_heading' ), 'layout' => $this->get_setting( 'table_contents_layout' ), 'position' => $this->get_setting( 'table_contents_position' ), 'hierarchy' => $this->get_setting( 'table_contents_hierarchy' ), 'numlist' => $this->get_setting( 'table_contents_numlist' ), 'scroll' => $this->get_setting( 'table_contents_scroll' ), ); } /** * get supported heading settings */ public function get_supported_headings() { $this->supported_headings = array(); for ( $i = 1; $i <= 6; $i ++ ) { if ( $this->get_setting( 'table_contents_h' . $i ) ) { array_push( $this->supported_headings, $i ); } } } /** * @param string $setting_id * * @return false|mixed * get settings */ public function get_setting( $setting_id = '' ) { $setting = rb_get_meta( $setting_id ); if ( '-1' == $setting ) { return ''; } if ( ! $setting || 'default' == $setting ) { $setting = pixwell_get_option( $setting_id ); } return $setting; } /** * @param $content * * @return string|string[] * the_content filter */ public function the_content( $content ) { $this->get_settings(); if ( ! $this->is_enabled( $content ) ) { return $content; } $matches = $this->extract_headings( $content ); if ( ! $matches || ! is_array( $matches ) || ! $this->minimum_headings( $matches ) ) { return $content; } $table_contents = $this->create_table_contents( $matches ); $content = $this->replace_content( $content, $matches ); $content = $this->add_table_contents( $content, $table_contents ); return $content; } /** * @param $content * @param $matches * * @return string|string[] * replace content */ function replace_content( $content, $matches ) { $find = array(); $replace = array(); foreach ( $matches as $index => $value ) { if ( ! empty( $value[0] ) && ! empty( $value[1] ) && ! empty( $value[2] ) ) { array_push( $find, $value[0] ); array_push( $replace, '<h' . $value[2] . ' id="' . $this->generate_uid( $value[0] ) . '">' . strip_tags( $value[0] ) . '</h' . $value[2] . '>' ); } } return str_replace( $find, $replace, $content ); } /** * @param $matches * * @return string * create table content */ function create_table_contents( $matches ) { $output = ''; if ( $this->settings['hierarchy'] ) { $min_depth = 6; foreach ( $matches as $index => $value ) { if ( $min_depth > $value[2] ) { $min_depth = intval( $value[2] ); } } foreach ( $matches as $index => $value ) { $matches[ $index ]['depth'] = intval( $value[2] ) - $min_depth; } } $classes = 'rbtoc'; if ( ! empty( $this->settings['scroll'] ) ) { $classes .= ' rb-smooth-scroll'; } if ( ! empty( $this->settings['layout'] ) && '2' === (string) $this->settings['layout'] ) { $classes .= ' table-fw'; } elseif ( ! empty( $this->settings['layout'] ) && '3' === (string) $this->settings['layout'] ) { $classes .= ' table-left table-fw-single-col'; } else { $classes .= ' table-left'; } $output .= '<div id="ruby-table-contents" class="' . esc_attr( $classes ) . '">'; if ( ! empty( $this->settings['heading'] ) ) { $output .= '<div class="table-content-header"><span class="h3">' . esc_html( $this->settings['heading'] ) . '</span></div>'; } $output .= '<div class="inner">'; foreach ( $matches as $index => $value ) { $link_classes = 'table-link h5'; if ( ! empty( $value['depth'] ) ) { $link_classes .= ' depth-' . $value['depth']; } $output .= '<div class="' . esc_attr( $link_classes ) . '"><a href="#' . $this->generate_uid( $value[0] ) . '">'; $output .= strip_tags( $value[0] ); $output .= '</a></div>'; } $output .= '</div></div>'; return $output; } /** * @param $content * @param $table_contents * * @return string|string[] * add table of contents section */ function add_table_contents( $content, $table_contents ) { if ( strpos( $content, '<!--RUBY:TOC-->' ) ) { return str_replace( '<!--RUBY:TOC-->', $table_contents, $content ); } $pos = 0; $tag = '</p>'; if ( ! empty( $this->settings['position'] ) ) { $pos = absint( $this->settings['position'] ); } $content = explode( $tag, $content ); foreach ( $content as $index => $paragraph ) { if ( $pos == $index ) { $content[ $index ] = $table_contents . $paragraph; } if ( trim( $paragraph ) ) { $content[ $index ] .= $tag; } } $content = implode( '', $content ); return $content; } /** * @param $content * * @return false|mixed */ public function extract_headings( $content ) { $matches = array(); if ( preg_match_all( '/(<h([1-6]{1})[^>]*>).*<\/h\2>/msuU', $content, $matches, PREG_SET_ORDER ) ) { $matches = $this->filter_headings( $matches ); $matches = $this->remove_empty( $matches ); return $matches; } return false; } /** filter supported headings */ public function filter_headings( $matches ) { foreach ( $matches as $index => $value ) { if ( ! in_array( $value[2], $this->supported_headings ) ) { unset( $matches[ $index ] ); } } return $matches; } /** remove empty */ function remove_empty( $matches ) { foreach ( $matches as $index => $value ) { $text = trim( strip_tags( $value[0] ) ); if ( empty( $text ) ) { unset( $matches[ $index ] ); } } return $matches; } /** * @param $matches * * @return bool * minimum headings */ public function minimum_headings( $matches ) { if ( count( $matches ) < $this->settings['enable'] ) { return false; } return true; } /** * @param $text * * @return string * generate ID */ public function generate_uid( $text ) { $output = trim( strip_tags( $text ) ); $output = preg_replace( "/\p{P}/u", "", $output ); $output = str_replace( " ", " ", $output ); $output = remove_accents( $output ); $output = sanitize_title_with_dashes( $output ); return $output; } /** * @param $content * * @return bool * is enabled */ function is_enabled( $content ) { if ( is_front_page() || strpos( $content, 'id="ruby-table-contents"' ) || is_page_template( 'rbc-frontend.php' ) ) { return false; } if ( ( $this->settings['post'] && is_single() ) || ( $this->settings['page'] && is_page() ) ) { return true; } return false; } } }this is the default code for my theme that I’m currently using, but it always doesn’t detect ( Table Of Context ).
Hope you can help me regarding the problematic TOC
Thank you
Taufiq SHello,
I apologize for the misunderstanding. It seems like the TOC comes from your theme’s default. However, that won’t work with the filter you’re applying. As the filter only works with TOC plugins.
The following filter can be used to disable the Table of Contents test:
/** * Filter to disable Table of Content test */ add_filter('rank_math/researches/tests', function ($tests, $type) { unset($tests['contentHasTOC']); return $tests; }, 10, 2 );After applying the filter above, the TOC test will no longer be a part of the content analysis, and you shouldn’t experience any errors.
Hope that helps, and please do not hesitate to let us know if you need our assistance with anything else.
Thank you.
hello Md. Sakib Khandaker
I have implemented the code you provided to disable the TOC rankmath analytics.
I will use the TOC Plugin which supports rankmath.
you are very helpful for me to solve this problem.
Thank you
Taufiq SHello,
We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.
If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.
If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.
Thank you.
hello Md. Sakib Khandaker
I will give the best review, for the rankmath plugin it is very easy to manage and supports very friendly support.
Thank You
Taufiq SHello
I’ve given a review to RankMath, I’m very proud, because it helped me build my personal blog into SEO Friends
https://wordpress.org/support/topic/thank-you-best-seo-plugin-rankmath/#new-post
Thank You
Taufiq SetiawanHello,
Thank you so much for your kind feedback. We appreciate it.
If you do have any further issues or concerns with our plugin, just ask away. We are always here to help.
The ticket ‘TOC Customize Theme Pixwell’ is closed to new replies.