Search

bbPress Showcase » bbPress Chat

Need help merging 2 functions

RSS

  1. Null
    User has not uploaded an avatar

    member
    Joined: May '08
    Posts: 43

    offline

    Hi,

    I have two working functions;

    Function 1:
    http://pastebin.com/m460e6aa
    Gets the topics from a forum and displays them on a page (in this case view.php)

    Function 2:
    http://pastebin.com/d464fb7df
    Counts the words in every post. When reached 20 words, all following words in that post are filtered out.

    The problem:
    Function 2 filters EVERY post in the whole forum. I just want it to only filter the posts gotten (and displayed) by function 1.

    To achieve this, I believe I have to combine these 2 functions into 1 big function. So I need to put the word-filter into the get-topic-filter so it will only filter the posts function 1 gets. But how to do that? Any attempt by me resulted into an empty page...

    I am kinda stuck

    Thanks

    Posted 2 years ago #
  2. _ck_
    _ck_

    senior admin
    Joined: Jul '06
    Posts: 884

    offline

    You don't have to do it as a filter.

    Try it like this:

    echo blog_rm_post_text(apply_filters('post_text',$blog_topic_posts[0]->post_text,0));

    or cleaner:

    $text=apply_filters('post_text',$blog_topic_posts[0]->post_text,0);
    echo blog_rm_post_text($text);

    I think it's failing as a filter because you are allowing for a 2nd parameter and the filter might be passing a value that becomes null so it crashes with an error. It might work if you removed the 2nd parameter , $num = 20 or just use it like I show above.

    Posted 2 years ago #
  3. Null
    User has not uploaded an avatar

    member
    Joined: May '08
    Posts: 43

    offline

    OMG so simple!!! Thanks!

    Okay how about this one then:
    If the post text succeed 20 words, it puts in ... and filter all following words. Now, when this happens, I want to ad a link to the full post to the forum like:
    <div align="left"><?php printf(__('Link to full post'), get_topic_link()) ?></div>

    Do you have any idea how to do that? I can't put it in function 2, cause this only filters the text and returns it filtered. You can't add php stuff in there. Putting it in function 2 will result in always showing this link, even when there are no words filtered (less then 20 words).

    I guess I need to use an if
    If $total > 20 {show link;}.

    Unfortunately $total doesn't work, though it's declared in function 2:
    //-- count words
    $words = explode(" ", $post);
    $total = count($words);

    Or do I need to re-add this in function 1? Don't I do things double then?

    Posted 2 years ago #
  4. _ck_
    _ck_

    senior admin
    Joined: Jul '06
    Posts: 884

    offline

    There are several ways you could approach that.

    You could move the truncation routine back into the first function, therefore you'd have the total already available.

    A more complex way would be to return not just the altered text but an array (or object) of the altered text and the length.

    return $short;

    return array($total,$short);

    so in the main function you'd call

    $result=blog_rm_post_text($text);

    $result[0] is your total
    $result[1] is your text

    Posted 2 years ago #
  5. Null
    User has not uploaded an avatar

    member
    Joined: May '08
    Posts: 43

    offline

    Hmm I do somewhat follow what you say, but I don't understand how to put that into my code. I mean where do I put the Read full link then?

    I have now added this so far:
    <?php if ($total > $num) {printf(__('Link to full post'), get_topic_link());} ?>

    That has no effect. I clearly don't follow Please help me understand.

    Posted 2 years ago #
  6. _ck_
    _ck_

    senior admin
    Joined: Jul '06
    Posts: 884

    offline

    It goes in your first function wherever you want the link to be.

    Are you trying to use bbPress as a blog program? I get that impression from the code.

    Posted 2 years ago #
  7. Null
    User has not uploaded an avatar

    member
    Joined: May '08
    Posts: 43

    offline

    Well it's more a plugin using a part of your forum so you can blog and it's almost done, except for this stupid Read more function...

    The plugin works out of the box, with full admin control, just activate and blog It's intended for people that want to have bbPress installed and don't want to (or can't) install Wordpress too and get them working together.

    Funny thing is, I've started this for fun to learn some programming with bbPress... I am at 255 lines of code already :S (perhaps that aint much, but I find it a lot). Never thought it would need that much. Some code can be cleaned and shorten I guess, but getting this thing working is my main priority for now

    Demo (wip):
    http://www.bbportal.org/mssp/view.php?view=blog
    (if you see nothing or errors, than I am f*cking up the code :))

    So what do I put where????
    In function 2 I need to replace:
    return $short; with
    return array($total,$short);

    So this will give me my word count right?

    Then in function 1 I need to add?
    $num = 20;
    $result[0] = $total;

    or something? And then my IF and the link as stated above?

    Posted 2 years ago #
  8. Null
    User has not uploaded an avatar

    member
    Joined: May '08
    Posts: 43

    offline

    Ok I have fixed it. Demo:
    http://www.bbportal.org/mssp/view.php?view=blog

    The Read More displays correctly. There is only a strange bug though. The word count is set on 30. If there are 31 words, a small error occurs (see link given above). The ... are displayed below the text. If I look at the html I see that the ... is place outside the </p>.

    If there are more words in it 32 and above, everything looks ok.

    So there is something going wrong with the </p> when post words = word count + 1.

    Any idea? Why does it touch the </p> ? If I look furhter down in the html is see that this goes wrong more often... I see starting <p> but no where closing etc.

    Here are the 2 functions:
    http://pastebin.com/m7af33a3d

    For HTML see demo and see source

    Posted 2 years ago #
  9. Null
    User has not uploaded an avatar

    member
    Joined: May '08
    Posts: 43

    offline

    After some more investigation (with a beer and some sunshine) I've discovered that the </p> only disappeared when the Read More is shown. So, for now, I've added </p> hardcoded:
    Read More &raquo;</p>

    Problem solved. Need to do some more testing and then I can release this plugin!

    I still find it strange why the </p> is filtered out though...

    Update... well that ain't working either, it seems random that the </p> appears.

    Sometimes is displays good:
    ...</p>

    Sometimes wrong:
    </p>...

    Sometimes not at all:
    ...

    ??????????????????????? I am lost here!

    Posted 2 years ago #
  10. _ck_
    _ck_

    senior admin
    Joined: Jul '06
    Posts: 884

    offline

    It's because the bbpress filters try to close any open tags for security.

    WordPress works around that because you have to manually insert a "read more" tag so it knows where to break. bbPress doesn't have that.

    You can try checking double newline \n\n which will be the end of a paragraph, or if you don't see one, you'll have to add the closing tag.

    You might be able to fool bbpress's filters by adding "\n\n" after the read more.

    Posted 2 years ago #

RSS feed for this topic

Post a reply to “Need help merging 2 functions” »

You must log in to post.

keep _ck_ coding >> donate $5 <<     Theme Switcher:
26 users online from in the past 30 minutes. 12 bots 13 guests 1 members: mccawphoto
7,294 views today 8,665 yesterday 14,010 peak. Most at once 63 today 48 yesterday 131 peak. Visited today: Dickinsonjohn9, shairah27, xiaotian, _ck_, engin1984, Augustine01, MichaelGomez, mccawphoto
2,240 posts in 380 topics over 63 months by 425 of 1,992 members. Latest: shairah27, Augustine01, MichaelGomez