function mi_plugin_render_callback(attributes) {
global post, pages;headings = $attributes['headings'];
// Obtiene los enlaces permanentes para cada página del post
pagelinks=array();for(i = 1; i<=count(pages); i++) {
page_links[] = get_permalink(post−>ID).(i > 1 ? 'page/' . $i . '/' : '');
}
// Encuentra la página que contiene la cabecera
$page_number = 1;
for ($i = 0; $i < count($pages); $i++) {
if (strpos($pages[$i], 'id="' . $anchor_id . '"') !== false) {
$page_number = $i + 1;
break;
}
}
// Construye el enlace correcto para la cabecera en la lista
$link = $page_links[$page_number - 1] . '#' . $anchor_id;
$output .= '<li><a href="' . $link . '">' . $heading['text'] . '</a>';
if (!empty($heading['children'])) {
$output .= render_nested_headings($heading['children'], $page_links, $pages);
}
$output .= '</li>';
}
output.=′</ul>′;returnoutput;
}
Assistant
This is a code snippet for a WordPress plugin that adds a table of contents block to the Gutenberg editor. The block displays a nested list of headings from the post content, with each heading linked to its corresponding section on the page.
The mi_plugin_register_block function registers the block with WordPress and defines its editor script, render callback, and attributes. The add_ids_to_headings function adds unique ID tags to each heading in the post content. The mi_plugin_render_callback function renders the table of contents block on the front-end, using the saved attributes and page data. Finally, the render_nested_headings function recursively renders the nested list of headings and their links.