0
votes

I am writing a widget that list the headings in the post and then created hash links and edits the HTML to reflect that. I've got the list widget content figured out and I just need to edit the_content, i've tried to add a filter for the method that returns the updated code but it's not working.

What would be the best way to do this? My class is called post_headings_widget and the edited HTML content is stored within $this->the_content.

I was hoping I could do this within the widget class

public
function edited_content() {
    return $this->the_content;
}

and then to edit the content output here

add_filter( 'the_content', [ 'post_headings_widget', 'edited_content' ] );

It calls the class method fine but i'm not sure exactly how it works so i'm guessing it called the method directly without calling the constructors etc?

I have also tried to just create a filter from within the widget() method but that did not work either, heres what I tried:

add_filter( 'the_content', function() {
    return 'test';
} );

Any ideas on a solution?

2

2 Answers

0
votes

You have to pass the_content as a parameter in your filter function/callback.

Check the Wordpress docs: https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content

0
votes

On widgets you need to bind on widget_text

add_filter('widget_text', 'se24265_my_function');

function se24265_my_function( $content )
{
# replace code here on widget $content
return $content;
}