[Resolved] Problems when trying to to build a hook

Home Forums Support [Resolved] Problems when trying to to build a hook

Home Forums Support Problems when trying to to build a hook

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1986969
    eduard sans

    Hi there!

    Ok, so I’ve been working for the last hours building a PHP “Frankenstein” (copying / pasting / editing code I find) in the shape of a dynamic table of content. It is already looking good and functional but I’ve been doing it in the functions.php file and I’d like to place it as a hook but the moment I place the code and hit publish it just won’t work.

    Any ideas would be appreciated. Thanks!

    Here’s the code:

    function insert_table_of_contents($content) {
    
    	$html_comment = "<!--insert-toc-->";
    	$comment_found = strpos($content, $html_comment) ? true : false;
    	$fixed_location = true;
    
    	// return the $content if
    	// $comment_found and $fixed_location are false
    	if (!$fixed_location && !$comment_found) {
    		return $content;
    	}
    
    	// exclude the table of contents from all pages
    	// other exclusion options include:
    	// in_category($id)
    	// has_term($term_name)
    	// is_single($array)
    	// is_author($id)
        if (is_page()) {
            return $content;
        }
    		
    	$regex = "~(<h([2-3]))(.*?>(.*)<\/h[2-3]>)~";
    	preg_match_all($regex, $content, $heading_results);
    
    	$num_match = count($heading_results[0]);
    	if($num_match < 3) {
    		return $content;
    	}
    
    	// declare local variable
    	$link_list = "";
    	// loop through $heading_results
    	for ($i = 0; $i < $num_match; ++ $i) {
    	    $new_heading = $heading_results[1][$i] . " id='$i' " . $heading_results[3][$i];
    	    $old_heading = $heading_results[0][$i];
    		$content = str_replace($old_heading, $new_heading, $content);
    	    $link_list .= "<li class='heading-level-" . $heading_results[2][$i] .
    	    	"'><a href='#$i'>" . $heading_results[4][$i] . "</a></li>";
    	}
    	    
    	$start_nav = "<nav class='tabla-de-contenidos'>";
    	$end_nav = "</nav>";
    	$title = "<details><summary>Índice de contenidos:<span>[ mostrar ]</span></summary>";
    	$link_list = "<ul>" . $link_list . "</ul></details>";
    
    	$table_of_contents = $start_nav . $title . $link_list . $end_nav;
    
    	// if $fixed_location is true and
    	// $comment_found is false
    	// insert the table of contents at a fixed location
    	if($fixed_location && !$comment_found) {
    		$first_paragraph = strpos($content, '</p>', 0) + 4;
    		$second_paragraph = strpos($content, '</p>', $first_p_pos);
    	return substr_replace($content, $table_of_contents, $second_paragraph + 4 , 0);
    	}
    	else {
    		return str_replace($html_comment, $table_of_contents, $content);
    	}
    } 
    
    add_filter('the_content', 'insert_table_of_contents');
    #1986978
    David
    Staff
    Customer Support

    Hi there,

    that kind of function cannot be added to a Hook Element.
    Hook elements are designed for adding ‘content’ into the core Hooks or the Theme content.

    Your code is using an add_filter to the_content – which needs to go in your functions.php ( child theme )

    #1986980
    eduard sans

    Oh ok, nevermind then :). Good to know! Thanks!

    #1986982
    David
    Staff
    Customer Support
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.