TOC hooking via shortcode

#689388
  • Resolved Anonymous
    Rank Math free

    Hi,

    I was trying to create a hook element to automatically insert the RankMath TOC on all my posts.
    I had a setup with a different TOC plugin and worked perfectly.

    But I tried to use RankMath TOC instead and it did not work.

    The GeneratePress team suggested to get in touch with you to confirm if this could be possible.

    Code:
    /*Add Table of contents before first H2*/
    add_shortcode(‘portable_hook’, function($atts){
    ob_start();
    $atts = shortcode_atts( array(
    ‘hook_name’ => ‘no foo’
    ), $atts, ‘portable_hook’ );
    do_action($atts[‘hook_name’]);
    return ob_get_clean();
    });

    function insert_hook_before_first_h2( $content ) {
    global $post;
    $inserted_hook = do_shortcode(‘[portable_hook hook_name=”custom_toc_hook”]’);

    if ( is_single() && ! is_admin() ) {
    $pattern = ‘/<h2[^>]*>.*?<\/h2>\s*<p[^>]*>.*?<\/p>\s*<p[^>]*>.*?<\/p>/s’;
    $content = preg_replace_callback($pattern, function($match) use ($inserted_hook) {
    return $inserted_hook . $match[0];
    }, $content, 1); // Add hook before the first h2
    }
    return $content;
    }
    add_filter( ‘the_content’, ‘insert_hook_before_first_h2’, 20 );

    Thanks for your comments,
    Denise

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    We apologize for the inconvenience you’re experiencing with the Rank Math TOC. It seems that you are trying to create a hook element to automatically insert the Rank Math TOC on all your posts, but it’s not working as expected.

    To ensure that the Rank Math TOC works seamlessly with your setup, you may need to add a piece of code to your theme’s functions.php file or rank-math.php file. This will allow Rank Math to detect that you are using a Table of Contents section in your posts.

    Here’s the code snippet you can use:

    add_filter( 'rank_math/researches/toc_plugins', function( $toc_plugins ) {
           $toc_plugins['plugin-directory/plugin-filename.php'] = 'Plugin Name';
        return $toc_plugins;
    });

    In the above code snippet, you’ll need to replace plugin-directory/plugin-filename.php with the path to your TOC plugin file inside the WordPress plugins folder and Plugin Name with the name of your TOC plugin.

    Once you’ve added this code, Rank Math should be able to detect that you are using a Table of Contents section in your posts.

    If you’re still facing issues after adding this code, please feel free to open a support ticket directly from here, and we’re always here to help.

    We appreciate your understanding and patience.

    Kind regards.

    Sources:
    Why Isn’t Rank Math Detecting the Table of Contents? » Rank Math
    Score 100/100 With Rank Math Post Tests » Rank Math
    How to Use a Table of Contents to Improve Content Readability » Rank Math

    Anonymous
    Rank Math free

    Hi,
    Thanks for your quick response!

    I’m not totally clear about this section here:
    In the above code snippet, you’ll need to replace plugin-directory/plugin-filename.php with the path to your TOC plugin file inside the WordPress plugins folder and Plugin Name with the name of your TOC plugin.

    The code that I showed you was inserted on my child’s theme funtion.php file, so I guess I will need to add the code you’re suggesting there, as well.

    I’m using a GP Hook element to create the Rankmath TOC, please take a look here:
    https://prnt.sc/7O2lVXvB5Ob6
    So, not really sure about what plugin you are refering to.

    Thanks!
    Denise

    Hello,

    Thank you for contacting Rank Math support, and sorry for the unexpected delays.

    I went ahead and asked our developers about this and it appears it is currently impossible to display the TOC block via hook. Our TOC is designed to be inserted manually into the Gutenberg editor as it requires the current ID of the post.

    With that said, our developers will soon add a dedicated shortcode for the TOC that will work in most standard editors. We will let you know once we release that feature as it is currently in progress.

    Hope that helps and please do not hesitate to let us know if you need my assistance with anything else.

    Anonymous
    Rank Math free

    Thank you Jeremy!
    Denise

    Hello,

    We are super happy that we could address your concern.

    If you 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,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)

The ticket ‘TOC hooking via shortcode’ is closed to new replies.