• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Generate a Table of Contents for Your WordPress Post with PHP

AlwaysStackin

Data Exfiltration Specialist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
35
Likes
190
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 2 900 XP
In this tutorial, we will explore the creation of a custom PHP script for a WordPress website. The goal is to offer students and beginners a reference or guide to enhance their knowledge and skills in developing custom PHP scripts within WordPress. I'll be providing a simple snippet that generates an automated Table of Contents for post content.

What is the Purpose of Table of Contents in Website Content?

The purpose of a Table of Contents (TOC) in website content is to offer users a structured and organized navigation tool. It enhances website usability, facilitates navigation, and contributes to an improved overall user experience. This is particularly valuable for content-rich websites or lengthy articles.

How to Create an Auto-generated Table of Contents (TOC) in WordPress?

Below, I will provide a sample PHP script demonstrating an Auto-generated Table of Contents (TOC). Follow the steps outlined below:

Step 1: Add Custom Style

In your WordPress site theme, integrate the following Cascading Style Sheet (CSS) script below. This style defines the custom design for the Table of Contents.

  1. .custom-toc-container

    {
  2. display

    :

    block

    ;
  3. width

    :

    100%

    ;
  4. padding

    :

    0.75em

    1em

    ;
  5. border

    :

    1px

    solid

    #00000024

    ;
  6. box-shadow

    :

    2px

    5px

    5px

    #00000024

    ;
  7. margin-bottom

    :

    15px

    ;
  8. border-radius

    :

    7px

    ;
  9. }
  10. .custom-toc-title

    {
  11. font-size

    :

    1.5rem

    ;
  12. letter-spacing

    :

    .8px

    ;
  13. font-weight

    :

    600

    ;
  14. border-bottom

    :

    1px

    solid

    #d3c9c9

    ;
  15. padding-bottom

    :

    10px

    ;
  16. margin-bottom

    :

    15px

    ;
  17. }
  18. ul.custom-toc

    .custom-toc-item

    {
  19. padding

    :

    3px

    1px

    ;
  20. list-style

    :

    square

    ;
  21. }
  22. a.custom-toc-item

    {
  23. font-weight

    :

    500

    ;
  24. text-decoration

    :

    none

    ;
  25. }

Step 2: Add Custom PHP Script

WordPress provides a highly useful built-in filter hook called `the_content

`. Utilizing this filter, we can modify the post content in WordPress before rendering it on the front end. To achieve this, open the `functions.php

` file of your active WP Theme and add the custom PHP script with the mentioned filter, as shown below:

  1. <?php
  2. /**
  3. * Generate Table of Contents
  4. */
  5. function

    custom_content_toc(

    $content

    )

    {
  6. $new_content

    =

    $content

    ;
  7. if

    (

    is_single(

    )

    )

    {
  8. $list

    =

    [

    ]

    ;
  9. $ids

    =

    [

    ]

    ;
  10. // Find all Headings from Content
  11. $headings

    =

    preg_match_all

    (

    '/<h\d[^>]*>.*?<\/h\d>/si'

    ,

    $content

    ,

    $matches

    )

    ;
  12. if

    (

    $headings

    >

    0

    &&

    isset

    (

    $matches

    [

    0

    ]

    )

    )

    {
  13. foreach

    (

    $matches

    [

    0

    ]

    as

    $k

    =>

    $heading

    )

    {
  14. // Getting the Heading Level
  15. preg_match_all

    (

    '/<h(\d)[^>]*>/si'

    ,

    $heading

    ,

    $level

    )

    ;
  16. $level

    =

    $level

    [

    1

    ]

    [

    0

    ]

    ?? 1

    ;

  17. //update heading attributes
  18. $id

    =

    ""

    ;
  19. $dom

    =

    new

    DOMDocument(

    )

    ;
  20. $dom

    ->

    loadHTML

    (

    $heading

    )

    ;
  21. $html

    =

    new

    DOMXPath(

    $dom

    )

    ;
  22. foreach

    (

    $html

    ->

    query

    (

    '//h1 | //h2 | //h3 | //h4 | //h5'

    )

    as

    $el

    )

    {
  23. // Get Heading ID
  24. $id

    =

    $el

    ->

    getAttribute

    (

    'id'

    )

    ?? ''

    ;
  25. // Generate Heading ID if not exists
  26. if

    (

    empty

    (

    $id

    )

    )

    {
  27. $id

    =

    str_replace

    (

    [

    " "

    ,

    "."

    ,

    ","

    ]

    ,

    "-"

    ,

    trim

    (

    strtolower

    (

    strip_tags

    (

    $heading

    )

    )

    )

    )

    ;
  28. }
  29. $i

    =

    1

    ;
  30. while

    (

    true

    )

    {
  31. if

    (

    !

    in_array

    (

    $id

    ,

    $ids

    )

    )

    {
  32. $ids

    [

    ]

    =

    $id

    ;
  33. break

    ;
  34. }

    else

    {
  35. $id

    =

    $id

    .

    "_"

    .

    (

    $i

    ++

    )

    ;
  36. }
  37. }
  38. // Set Heading's Updated ID attribute
  39. $el

    ->

    setAttribute

    (

    'id'

    ,

    $id

    )

    ;
  40. }
  41. $new_heading

    =

    $dom

    ->

    saveHTML

    (

    )

    ;
  42. $new_content

    =

    str_replace

    (

    $heading

    ,

    $new_heading

    ,

    $new_content

    )

    ;

  43. $parent

    =

    false

    ;
  44. // Locate and set the parent Heading
  45. if

    (

    $level

    >

    1

    &&

    !

    empty

    (

    $list

    )

    )

    {
  46. $headingBeforeArr

    =

    array_slice

    (

    $list

    ,

    0

    ,

    $k

    +

    1

    )

    ;
  47. krsort

    (

    $headingBeforeArr

    )

    ;
  48. foreach

    (

    $headingBeforeArr

    as

    $key

    =>

    $headingBefore

    )

    {
  49. if

    (

    $headingBefore

    [

    'level'

    ]

    <

    $level

    )

    {
  50. $parent

    =

    $key

    ;
  51. break

    ;
  52. }
  53. }
  54. }
  55. // Add heading Item to array
  56. $list

    [

    ]

    =

    [

    "level"

    =>

    $level

    ,

    "html"

    =>

    $heading

    ,

    "parent"

    =>

    $parent

    ,

    "id"

    =>

    $id

    ]

    ;
  57. }
  58. }

  59. /**
  60. * Generate the Table of Content
  61. */
  62. $toc

    =

    ""

    ;
  63. if

    (

    !

    empty

    (

    $list

    )

    )

    {
  64. $toc

    .=

    "<div class='custom-toc-container'>"

    ;
  65. $toc

    .=

    "<div class='custom-toc-title'>Table of Contents</div>"

    ;
  66. $toc

    .=

    "<ul class='custom-toc'>"

    ;
  67. foreach

    (

    $list

    as

    $k

    =>

    $row

    )

    {
  68. if

    (

    $row

    [

    'parent'

    ]

    !==

    false

    )
  69. continue

    ;
  70. $toc

    .=

    "<li class='custom-toc-item'>"

    ;
  71. $toc

    .=

    "<a href ='#{$row['id']}

    ' class='custom-toc-item'>"

    .

    (

    strip_tags

    (

    $row

    [

    'html'

    ]

    )

    )

    .

    "</a>"

    ;
  72. // Find Child Headings
  73. $toc

    .=

    get_child(

    $list

    ,

    $k

    )

    ;
  74. $toc

    .=

    "</li>"

    ;
  75. }
  76. $toc

    .=

    "</ul>"

    ;
  77. $toc

    .=

    "</div>"

    ;
  78. }

  79. }
  80. // Return Post Content with Table of Contents
  81. $new_content

    =

    $toc

    .

    $new_content

    ;
  82. return

    $new_content

    ;
  83. }

  84. /**
  85. * Generate Sub-Headings for Table of Contents
  86. */
  87. function

    get_child(

    $list

    =

    [

    ]

    ,

    $parent

    =

    null

    )

    {
  88. $child_content

    =

    ""

    ;
  89. if

    (

    !

    empty

    (

    $list

    )

    &&

    !

    is_null

    (

    $parent

    )

    )

    {
  90. foreach

    (

    $list

    as

    $k

    =>

    $row

    )

    {
  91. if

    (

    !

    is_numeric

    (

    $row

    [

    'parent'

    ]

    )

    ||

    $row

    [

    'parent'

    ]

    !=

    $parent

    )
  92. continue

    ;
  93. $child_content

    .=

    "<li class='custom-toc-item'>"

    ;
  94. $child_content

    .=

    "<a href ='#{$row['id']}

    ' class='custom-toc-item'>"

    .

    (

    strip_tags

    (

    $row

    [

    'html'

    ]

    )

    )

    .

    "</a>"

    ;
  95. $child_content

    .=

    get_child(

    $list

    ,

    $k

    )

    ;
  96. $child_content

    .=

    "</li>"

    ;
  97. }
  98. }
  99. if

    (

    !

    empty

    (

    $child_content

    )

    )
  100. $child_content

    =

    "<ul class='custom-toc'>{$child_content}

    </ul>"

    ;
  101. return

    $child_content

    ;
  102. }

  103. add_filter(

    'the_content'

    ,

    'custom_content_toc'

    ,

    10

    ,

    1

    )

    ;
  104. ?>

Snapshots

Below are example snapshots utilizing the Custom CSS and PHP Script provided above to generate the Table of Contents for WordPress Posts:

WP Post without Table of Content

Table of Content UI

WP Post with Table of Content

There you have it! I trust that this Generate a Table of Contents for Your WordPress Post with PHP Tutorial proves beneficial for your needs and provides valuable insights for your future WordPress Website. Dive into more on this website for an array of Tutorials, Free Source Codes, and Articles covering various programming languages.

Consider exploring the following articles as well:

  • Dynamic Table of Contents in PHP
  • College Website - Content Management System in PHP
  • News Portal - CMS in Python using Django

Happy Coding =)


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

350,639

350,649

Top