WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Trouble adding filter to the_content() (2 posts)

  1. Patrick Rauland
    Member
    Posted 3 years ago #

    Hello There,

    I am working on a plugin that will automatically add anchor tags to the content with the user having to do anything. Basically I want to replace all header tags (h1, h2, h3, etc.) with

    <a name='foo1'></a><h3>foo2</h3>.

    But for right now I would be happy just replacing 'a' with a 'b'.

    function kroc_add_anchors_to_content( ) {
    
    	$kroc_result = the_content();
    
    	$kroc_patterns[] = "/a/";
    	$kroc_replacements[] = "/b/";
    	$kroc_result = preg_replace($kroc_patterns, $kroc_replacements, $kroc_result);
    
    	return $kroc_result;
    
    }//function
    
    add_filter( 'the_content', 'kroc_add_anchors_to_content', 9 );

    When I run this code I get errors:

    Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 10071 bytes) in /home/therefor/public_html/kroc/wp-includes/media.php on line 1256

    Any ideas?

  2. Patrick Rauland
    Member
    Posted 3 years ago #

    Took me most of the day to figure it out, you need to call get_the_content() instead of the_content().

    function kroc_add_anchors_to_content( ) {
    
    	$kroc_result = get_the_content();
    
    	$kroc_patterns[] = "/<h3/";
    	$kroc_replacements[] = "<a name='test'></a><h3";
    	$kroc_result = preg_replace($kroc_patterns, $kroc_replacements, $kroc_result);
    
    	return $kroc_result;
    
    }//function

Topic Closed

This topic has been closed to new replies.

About this Topic