• Resolved MarcL

    (@marclindner)


    Hello,

    I would like to insert the ez-toc right before the second h2 tag of the posts automatically (not possible with the custom option of ez-toc-settings[position]).

    I use to use Table of Contents Plus (and I want to migrate to Easy Table of Contents), and the code below used to work without any issue but when I replace the shortcode toc by ez-toc it triggers error 500 :

    <?php

    function insert_shortcode_before_second_h2($content) {
    // Check if the post is not a page or any other custom type
    if (is_singular('post')) {
    // Check if the post doesn't contain [no_toc]
    if (strpos($content, '[no_toc]') === false) {
    // Find the second occurrence of <h2> tag
    $second_h2_position = strpos($content, '<h2', strpos($content, '<h2') + 1);

    // Find the opening <h2> tag after the second <h2> tag
    $opening_h2_tag_position = strpos($content, '<h2', $second_h2_position);

    // Insert the [toc] shortcode before the opening <h2> tag
    $content = substr_replace($content, '[toc]', $opening_h2_tag_position, 0);
    }
    }

    return $content;
    }
    add_filter('the_content', 'insert_shortcode_before_second_h2', 9);

    As a non-dev, I tried to find a solution with Claude/GPT to work on a code to do this insertion but nothing seems to be working : either the shortcode is not executed (just displayed) or the code triggers a error 500 memory limit (although i ticked the box on the admin page regarding that matter).

    Below is the last version that still doesn’t work.. but maybe someone sees something obvious that could fix that ? Maybe it’s useful for others. (I’m not using any website builders)

    <?php
    // Exit if accessed directly
    if (!defined('ABSPATH')) {
    exit;
    }

    function add_toc_before_second_h2($content) {
    // Only proceed if we're on a single post
    if (!is_single()) {
    return $content;
    }

    // Find position of first h2
    $pos1 = stripos($content, '<h2 class="wp-block-heading"');
    if ($pos1 === false) {
    return $content;
    }

    // Find position of second h2
    $pos2 = stripos($content, '<h2 class="wp-block-heading"', $pos1 + 1);
    if ($pos2 === false) {
    return $content;
    }

    // Build new content
    $content = substr($content, 0, $pos2) . '[ez-toc]' . substr($content, $pos2);

    return $content;
    }

    // Add our function to WordPress content filter
    add_filter('the_content', 'add_toc_before_second_h2', 5);

    Thanks a lot.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Akshay A

    (@akshaycode1)

    Hi,

    Thank you for reaching out to us. Could you please enable the debug.log and check the exact error you are getting? Kindly share that error with us so we can check further.

    • This reply was modified 2 weeks, 1 day ago by Akshay A.
    Thread Starter MarcL

    (@marclindner)

    Dear Akshay,

    Thanks for your prompt reply.

    FYI I have this in wp-config : define( ‘WP_MEMORY_LIMIT’, ‘640M’ );

    Here below some example of errors I encountered from the debug :

    [22-Feb-2025 16:40:47 UTC] PHP Fatal error:  Allowed memory size of 671088640 bytes exhausted (tried to allocate 32768 bytes) in /home/website/www/wp-includes/formatting.php on line 303

    [22-Feb-2025 16:40:47 UTC] PHP Fatal error:  Allowed memory size of 671088640 bytes exhausted (tried to allocate 53248 bytes) in /home/website/www/wp-includes/functions.php on line 5464

    [22-Feb-2025 16:40:47 UTC] PHP Fatal error:  Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

    [22-Feb-2025 16:37:57 UTC] PHP Fatal error:  Allowed memory size of 671088640 bytes exhausted (tried to allocate 262144 bytes) in /home/website/www/wp-includes/class-wp-term-query.php on line 308

    [22-Feb-2025 16:37:57 UTC] PHP Fatal error:  Allowed memory size of 671088640 bytes exhausted (tried to allocate 262144 bytes) in /home/website/www/wp-includes/functions.php on line 4294

    [22-Feb-2025 16:37:57 UTC] PHP Fatal error:  Allowed memory size of 671088640 bytes exhausted (tried to allocate 262144 bytes) in Unknown on line 0

    [22-Feb-2025 16:06:22 UTC] PHP Fatal error:  Allowed memory size of 671088640 bytes exhausted (tried to allocate 36864 bytes) in /home/website/www/wp-includes/formatting.php on line 616

    [22-Feb-2025 16:06:22 UTC] PHP Fatal error:  Allowed memory size of 671088640 bytes exhausted (tried to allocate 69632 bytes) in /home/website/www/wp-includes/functions.php on line 5464

    [22-Feb-2025 16:06:22 UTC] PHP Fatal error:? Unknown: Cannot use output buffering in output buffering display handlers in Unknown on line 0

    Thanks a lot for your help,

    Plugin Support Akshay A

    (@akshaycode1)

    Hi, kindly go to the theme’s functions.php file and add the following filter:

    add_filter( ‘ez_toc_apply_filter_status_manually’, ‘__return_false’ );

    Try it and let us know if it works.

    Thread Starter MarcL

    (@marclindner)

    Hi Akhsay,

    Thanks it works great!

    Plugin Support Akshay A

    (@akshaycode1)

    Great to hear that your issue has been resolved. We are closing this thread now. Have a great day ahead!

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.