• Resolved jwashburn

    (@jwashburn)


    I have a plugin that I am working on hacking a bit. Currently it pulls some data and adds content to the bottom of a post using
    add_filter('the_content', 'showWhiteboard');

    It also uses add_filter( 'page_template', 'wodtogether_page_template' );

    to grab the page template

    It works fine, no problems with it. I am trying to get it post the data at the TOP of the post, right now its appending to the post. I have been reading the documentation https://codex.www.ads-software.com/Plugin_API/Filter_Reference/the_content

    and trying to understand priority but I dont think I have a handle on it yet. I tried editing the line from

    add_filter('the_content', 'showWhiteboard');

    to

    add_filter('the_content', 'showWhiteboard', '5'); to make it show up before the actual regular post content, but it doesn’t really change anything

    Can I get a shove in the right direction?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The problem is you are not understanding how ‘the_content’ filter works. The showWhiteboard() callback is not echoing out the whiteboard at a particular time in the page flow where playing with priority would change positioning relative to the content.

    Instead the callback is passed the entire content and the whiteboard is concatenated to the end of the content. Changing priority would change the order of other filtered things concatenated to the content, but not relative to the post content itself. In a sense the content is always first in priority because it is what is passed by the apply_filters() call.

    It’s quite simple though to put the whiteboard first. You do not alter priority. In the callback function you simply concatenate the content to the whiteboard instead of concatenating the whiteboard to the content.

    The callback may have code something like return $content . $whiteboard;. You just need to reverse it to return $whiteboard . $content;

    Thread Starter jwashburn

    (@jwashburn)

    Thanks, that did it. It broke some layout things, but I can work through that. My next step is to make an admin function that allows us to choose in the settings if we want it at the beginning or end of the post.

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Having trouble with adding content to top of post using add_filter’ is closed to new replies.