Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to include a Meta Description and Keywords in bbpress without a plug-in.


  • merlin214365
    Member

    @merlin214365

    So after trying many plug-ins for meta data i found out that most don’t work at all with the newest version of bbpress and others do a poor job so i decided to write my own little php code no plug-in needed.

    I wrote some php code to include a meta description based on the first 200 characters of the post and a default description if there is no post ( i.e the front-page ). Also meta keyword’s that displays any tags associated with that post as keywords.

    So just add this to your header

    <?php<br />
    if ($posts) {<br />
    foreach ($posts as $bb_post) : $del_class = post_del_class();<br />
    $old=ob_get_contents(); ob_clean(); ob_start(); // you may leave ob_start();<br />
    post_text();<br />
    $out.=ob_get_contents(); ob_clean();<br />
    endforeach;<br />
    $out = preg_replace('#<p[^>]*>(s| ?)*</p>#', '', $out); // takes out <p><br />
    $out = substr($out,0,200); // only displays the first 200 lines of the first post.<br />
    echo '<META NAME="Description" CONTENT="';<br />
    echo $out;<br />
    echo '">';<br />
    }<br />
    else {<br />
    echo '<META NAME="Description" CONTENT="This is your defalt description edit this to what ever you want ">'; // This displays when where is no post<br />
    }<br />
    global $tags;<br />
    if (!empty($tags)) {<br />
    $keywords=""; foreach ($tags as $t) {$keywords.=$t->raw_tag.', ';}<br />
    echo "n".'<meta NAME="keywords" CONTENT="'.trim($keywords,", ").'">'."n"; // This displays any tags associated with that post as a keyword<br />
    } ?>
Viewing 23 replies - 1 through 23 (of 23 total)

  • psycheangels
    Member

    @psycheangels

    thanks, but meta description not working because it will insert embed code and image code to header if the first line of the post have markup code. thats a bad thing ryt?

    Cripes, no need for all that object-based markey, bbPress has functions to pass as a string rather than echo…

    <?php
    /*
    Plugin Name: Meta Headers
    */

    function meta_heads() {

    global $posts, $tags;

    if ( $posts ) {
    $out = $posts[0]->post_text;
    $out = strip_tags( $out );
    $out = str_replace( array( "n", "r" ), ' ', $out );
    $out = substr($out, 0, 200); // only display the first 200 characters of the first post
    }

    if( empty( $out ) )
    $out = 'Default description';

    echo "n".'<meta name="description" content="'.$out.'" />';

    if ( !empty( $tags ) ) {
    $keywords = '';
    foreach ($tags as $t) {
    $keywords.=$t->raw_tag.', ';
    }
    echo "n".'<meta name="keywords" content="'.substr($keywords,0,-2).'">'."n"; // displays any tags associated with a topic as keywords
    }

    }

    add_action( 'bb_head', 'meta_heads' );

    That will strip HTML, but not bbCode


    merlin214365
    Member

    @merlin214365

    I should have pointed out im still learning php and bbpress functions still mystify me but thanks Kawauso for cleaning up the code i appreciate it.


    merlin214365
    Member

    @merlin214365

    @psycheangels just add some more filters to

    if ( $posts ) {
    $out = $posts[0]->post_text;
    $out = strip_tags( $out );

    Like this one

    $out = str_replace( '<br />', '', $out );

    there might be an easier way but im still learning.

    @merlin214365: As a golden rule, most functions will return a string by putting get_ in front of the function name, rather than echoing (which is just an alias for the get_ anyway). Most functions you’re likely to need are in functions.bb-template.php too, so have a read through that and I’d recommend getting an editor like Programmer’s Notepad to be able to search through bbPress’ code all at once to understand usage.

    strip_tags will take out all HTML and PHP tags, so no need for an extra str_replace :)

    $out = preg_replace( '|/!]*?[^[*?]|si', '', $out );

    That should take bbCode out, put it after $out = strip_tags( $out ); . Not tested it, but it’s adapted from a function I found on Google :P


    merlin214365
    Member

    @merlin214365

    Thanks will defiantly look into fucntions.bb-templete.php i appreciate you help.

    Thanks all, got it working with this:

    if ( $posts ) {
    $out = $posts[0]->post_text;
    $out = strip_tags( $out );
    $out = str_replace( array( "n", "r" ), ' ', $out );
    $out = preg_replace( '|/!]*?[^[*?]|si', '', $out );
    $out = substr($out, 0, 200); // only display the first 200 characters of the first post
    }

    echo "n".'<meta name="description" content="'.$out.'" />';


    minervaa
    Participant

    @minervaa

    Where would you paste the code? on the post.php file?


    kevinjohngallagher
    Member

    @kevinjohngallagher


    minervaa
    Participant

    @minervaa

    Thanks for the plugin. But how would you set it up? it is not giving any options on the admin side.


    minervaa
    Participant

    @minervaa

    hmmm ! actually this SEO plugin blocks the forum.


    kevinjohngallagher
    Member

    @kevinjohngallagher

    hmmm ! actually this SEO plugin blocks the forum.

    Could you find it in the goodness of your heart to give a little more information?

    What do you mean by block?

    What version of bbpress are you using?

    What error message did you get?

    Basically, anything that gives actual information would be benefitial, and I’ll try and get a fix to you.


    minervaa
    Participant

    @minervaa

    After activating the plugin, you can’t access the forum. Actually it is more like you will see just an empty white screen.

    bbPress version = 0.9.0.6

    Theme is Hybrid but a little bit customised.

    Also, is this plugin suppose to give any option to set up on the admin side? After activating I didn’t see anything on admin side.

    Let me know if you have any other questions.

    Cheers


    kevinjohngallagher
    Member

    @kevinjohngallagher

    After activating the plugin, you can’t access the forum. Actually it is more like you will see just an empty white screen.

    Great, much better info, thanks.

    Now what’s the error message please?

    bbPress version = 0.9.0.6

    This is probably the reason, but I’m sure I have it working on a bb0.9 version. i’ll double check.

    Theme is Hybrid but a little bit customised.

    And does it work on the default theme?

    Also, is this plugin suppose to give any option to set up on the admin side?

    No.


    minervaa
    Participant

    @minervaa

    It doesn’t give any error messages, it just present you an empty white screen.

    tried with the default theme too, still it gives you an empty white screen


    chrishajer
    Participant

    @chrishajer

    The white screen is a 500 Internal Server Error. If you have access to error logs, you should be able to see what error is being logged. If not, you’re going to have to either display errors or log errors. Then you can see what’s happening.

    What PHP version are you using?


    minervaa
    Participant

    @minervaa

    its php 5. Are you talking about the error log on the server side?


    kevinjohngallagher
    Member

    @kevinjohngallagher

    Sorry, yeah, the error log on the server will let us see what’s going on.

    The plugin doesn’t do anything fancy, nor make any native calls to the database or anything, it simply calls exisiting bbPress functions. That said, I might need to just accept that it’s bbPress1.0 only (though I was kinda sure it wasn’t).

    Any info you could give us would be benefitial,

    THANKS


    minervaa
    Participant

    @minervaa

    Just spoke to my hosting company. Unfortunately they don’t provide error logs.


    minervaa
    Participant

    @minervaa

    Just get working the meta description generator using Kawauso’s code on topic posts.

    Although it seems it garbled the forum home page description so I am guessing that has to be done manually.

    It also take tags as keywords (if provided)


    gowdemon
    Participant

    @gowdemon

    @kevinjohngallagher
    Hi, I tried installing the plugin since I am not good with codes. When I tried to activate the plugin I got the following error.

    “The package could not be installed. PCLZIP_ERR_BAD_FORMAT (-10) : Unable to find End of Central Dir Record signature.”

    Any idea how to fix it?


    senton1
    Participant

    @senton1

    thanks, but meta description not working since it will insert embed code and image code to header if the first brand of the post have markup code. thats an unsatisfactory thing ryt?
    my blog

Viewing 23 replies - 1 through 23 (of 23 total)

You must be logged in to reply to this topic.