• Resolved egh9

    (@egh9)


    I have a function in wordpress to add id= AutoNumber for heading in content:

     function anchor_content_headings($content) {
      $content = preg_replace_callback("/\<h([1|2|3])\>(.*?)\<\/h([1|2|3])\>/", function ($matches) {
        $hTag = $matches[1];
        $title = $matches[2];
    
       $slug = "" . sanitize_title_with_dashes($title ); 
    
        return '<a href="#'. $slug .'"><h'. $hTag .' id="' . $slug . '">' . $title . '</h'. $hTag .'></a>';
      }, $content);
      return $content; 
    }
    add_filter('the_content', 'anchor_content_headings');

    The function outputs:

    <2 id=#title-test>title test</h2>
    
    <2 id=#title-two>title two</h2>
    
    <3 id=#title-test-test>title test test</h3>

    I want edit function to output:

    <2 id=#1>title test </h2>
    
    <2 id=#2>title test test </h2>
    
    <3 id=#3>title test test </h3>

    This mod is because there are problems with non-English languages ??with their use of JavaScript

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    I take it you know you should be keeping a count that is inserted into the id attribute and incremented after each use. The problem with that is the callback cannot remember without help what the count is. The count needs to be stored somewhere in between calls. There are a number of ways to do so. A global value is the easiest option. A class property or session variable are others in the same vein.

    BTW, the anchor tag and H# tags should not be assigned the exact same value as id attribute. That would make for HTML that cannot validate and cause problems with scripts accessing elements by ID.

    Your output and what you say you want doesn’t make sense. Neither are valid HTML.
    It looks to me like you want each heading to contain an id attribute with the heading title as a slug value. Be aware that identifiers should not start with a number, for consistency. But your code is making the heading a link, which is linked to itself, which makes no sense.
    Don’t you want <h2>My 1st heading</h2> to look like <h2 id="my-1st-heading">My 1st heading</h2> ?
    How about <h3 class="big">1 way to stop hunger</h3> ? I don’t think your regular expression is enough to handle other attributes that might be on the headings, and it’s not removing leading numbers.

    Thread Starter egh9

    (@egh9)

    @bcworkz @joyously
    There is no way to make ID AutoNumber
    Because the Title in the Arabic language contradict the JavaScript

    You can use a static variable in your function, to make it count the headings, but you shouldn’t use an id attribute that starts with a number. You can use any ASCII character in front of the number, though.
    It still doesn’t need to have a <a href= or </a> around it, though.

    Thread Starter egh9

    (@egh9)

    @joyously
    is not work It uses a fixed number for all operations

    function anchor_content_headings($content) {
      $content = preg_replace_callback("/\<h([1|2|3])\>(.*?)\<\/h([1|2|3])\>/", function ($matches) {
        $hTag = $matches[1];
        $title = $matches[2];
     for($i = 1; $i <= 30; $i++) {
           $formattedNumber = sprintf('%02d', $i);
           $formattedNumber;
    
        } 
    
        $slug = "" . $formattedNumber;
        return '<a href="#'. $slug .'"><h'. $hTag .' id="' . $slug . '">' . $title . '</h'. $hTag .'></a>';
      }, $content);
      return $content; 
    }
    add_filter('the_content', 'anchor_content_headings');

    this code is work but random number i want number 1 2 3 4 5 6 without duplication

    function anchor_content_headings($content) {
      $content = preg_replace_callback("/\<h([1|2|3])\>(.*?)\<\/h([1|2|3])\>/", function ($matches) {
        $hTag = $matches[1];
        $title = $matches[2];
    
      for($i = 1; $i <= 99; $i++) {
           $formattedNumber = sprintf('%02d', $i);
           $formattedNumber = rand(1,5);
    
        }
    
       $slug = "" . sanitize_title_with_dashes($formattedNumber); 
    
        return '<a href="#'. $slug .'"><h'. $hTag .' id="' . $slug . '">' . $title . '</h'. $hTag .'></a>';
      }, $content);
      return $content; 
    }
    add_filter('the_content', 'anchor_content_headings');
    • This reply was modified 4 years, 11 months ago by egh9.

    It looks like you didn’t read about static variables, or listen to my other advice.
    How about this?

    function my_heading_ids($content) {
      $content = preg_replace_callback("/\<h([1|2|3])/", function ($matches) {
        static $num = 1;
        $hTag = $matches[1];
        return '<h'. $hTag .' id="my' . $num++ . '"';
      }, $content);
      return $content; 
    }
    add_filter('the_content', 'my_heading_ids');
    Thread Starter egh9

    (@egh9)

    @joyously

    It already worked
    Thank you

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Edit function in WordPress to add id AutoNumber for heading’ is closed to new replies.