Info
- 22 posts
- 7 voices
- Started 2 years ago by merlin214365
- Latest reply from minervaa
How to include a Meta Description and Keywords in bbpress without a plug-in.
-
- Posted 2 years ago #
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 if ($posts) { foreach ($posts as $bb_post) : $del_class = post_del_class(); $old=ob_get_contents(); ob_clean(); ob_start(); // you may leave ob_start(); post_text(); $out.=ob_get_contents(); ob_clean(); endforeach; $out = preg_replace('#<p[^>]*>(\s| ?)*</p>#', '', $out); // takes out <p> $out = substr($out,0,200); // only displays the first 200 lines of the first post. echo '<META NAME="Description" CONTENT="'; echo $out; echo '">'; } else { echo '<META NAME="Description" CONTENT="This is your defalt description edit this to what ever you want ">'; // This displays when where is no post } global $tags; if (!empty($tags)) { $keywords=""; foreach ($tags as $t) {$keywords.=$t->raw_tag.', ';} echo "\n".'<meta NAME="keywords" CONTENT="'.trim($keywords,", ").'">'."\n"; // This displays any tags associated with that post as a keyword } ?> -
- Posted 2 years ago #
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?
-
- Posted 2 years ago #
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
-
- Posted 2 years ago #
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.
-
- Posted 2 years ago #
@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.
-
- Posted 2 years ago #
@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 infunctions.bb-template.phptoo, 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_tagswill take out all HTML and PHP tags, so no need for an extra str_replace :) -
- Posted 2 years ago #
$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 -
- Posted 2 years ago #
Thanks will defiantly look into fucntions.bb-templete.php i appreciate you help.
-
- Posted 2 years ago #
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.'" />'; -
- Posted 1 year ago #
Where would you paste the code? on the post.php file?
-
- Posted 1 year ago #
Thanks for the plugin. But how would you set it up? it is not giving any options on the admin side.
-
- Posted 1 year ago #
hmmm ! actually this SEO plugin blocks the forum.
-
- Posted 1 year ago #
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.
-
- Posted 1 year ago #
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
-
- Posted 1 year ago #
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.
-
- Posted 1 year ago #
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
-
- Posted 1 year ago #
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?
-
- Posted 1 year ago #
its php 5. Are you talking about the error log on the server side?
-
- Posted 1 year ago #
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 -
- Posted 1 year ago #
Just spoke to my hosting company. Unfortunately they don't provide error logs.
-
- Posted 1 year ago #
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)
-
You must log in to post.