Forum Replies Created
-
Hi Robin. Sorry for the delayed response. It worked! Thanks for the simple fix. Now we know that shortcodes can take two inputs. If you need any of my code for use in a plugin, let me know. Though I have done things that are unique to my website, so they may not be as useful.
We can close this thread now.
Hi Robin. Finally getting around to posting the error I see when trying to use two input parameters for a shortcode:
Warning: Illegal string offset 'id2' in /mysite/wp-content/plugins/recent-books-discussed/recent-books-discussed.php on line 33
I should probably start a different thread in the forums on this?
Hi Robin. I’m on my way to the UK to support the Farnborough airshow, so I can’t access my home computer to replicate the error for another week. What I can tell you is; when I removed the switch_to_blog id input parameter and instead assigned a variable $cat to ‘post_parent’, and set $cat = $attr[‘id’];, I received no errors. I hard-coded to the blog site, then I can adjust the id to equal the forum id. So my feeling is that shortcodes cannot accept two inputs? If I’m wrong about that, then I will re-address this upon my return to the states.
I have updated the code again, and I have a question to improve it a little. First the update; I had more trouble with formatting when I wanted to bring in topics from different sites. What I finally learned was to create a new id class and use it in my child theme style.css. Here is the updated code:
add_shortcode( 'recent-bbpress-topics', 'recent_bbpress_topics' ); // Add Recent Topics to BBPress from separate site within WP Multisite function recent_bbpress_topics($attr) { //Switch to the blog where the bbpress topics are located switch_to_blog($attr['id']); //this, along with $response variable below, allows for display within content - without these, shortcode will display topics at top of content ob_start(); if ( bbp_has_topics( array( 'author' => 0, 'show_stickies' => false, 'order' => 'DESC', 'post_parent' => 'any', 'posts_per_page' => 8 ) ) ) { //id class to be named "#recent-titles li.bbp-topic-title" and placed in style.css in the child theme ?> <ul id="recent-titles"> <?php while ( bbp_topics() ) : bbp_the_topic(); ?> <?php bbp_get_template_part( 'loop', 'single-topic' ); ?> <?php endwhile; ?> <?php //bbp_get_template_part( 'loop', 'topics' ); ?> </ul> <?php //restore the current blog to maintain formatting restore_current_blog(); } //this, along with ob_start(); above, allows for display within content, instead of at the top of the content/page $response = ob_get_contents(); ob_end_clean(); return $response; }
And here is what I added to style.css:
/*Changes made to accommodate formatting for plugin recent-bbpress-changes*/ #recent-titles li.bbp-topic-title { float: left; text-align: left; width: 12%; overflow: hidden; }
Notice how I declared the class id in the php code. One additional note; I have made several changes to my bbpress topics before I ever implemented this plugin. Perhaps other users will not require the css changes that I had to implement, post plugin activation.
Now for my question: Is it possible to have two input parameters to a shortcode? For this plugin, I would like to be able to change the input to “post_parent” as well as “switch_to_blog($attr[‘id’]);”. I’ve already tried to do this, but have failed. Here is what I tried:
function recent_bbpress_topics($attr,$attr2) { //Switch to the blog where the bbpress topics are located switch_to_blog($attr['id']); //this, along with $response variable below, allows for display within content - without these, shortcode will display topics at top of content ob_start(); if ( bbp_has_topics( array( 'author' => 0, 'show_stickies' => false, 'order' => 'DESC', 'post_parent' => $attr2['id2'], 'posts_per_page' => 8 ) ) )
I end up getting an illegal string swap error. Anyone got any ideas here?
Hi Robin. I made another improvement and added some comments. I noticed that, no matter where I inserted the shortcode, the topics displayed at the top of the content. After some searching I learned why – functions need to return a string. So I found an easy wordpress solution. I also simplified the shortcode, and removed the add_action line at the bottom as it was not doing anything. Here is the updated code:
add_shortcode( 'recent-bbpress-topics', 'recent_bbpress_topics' ); // Add Recent Topics to BBPress from separate site within WP Multisite function recent_bbpress_topics($attr) { //Switch to the blog where the bbpress topics are located switch_to_blog($attr['id']); //this, along with $response variable below, allows for display within content - without these, shortcode will display topics at top of content ob_start(); if ( bbp_has_topics( array( 'author' => 0, 'show_stickies' => false, 'order' => 'DESC', 'post_parent' => 'any', 'posts_per_page' => 9 ) ) ) { ?> <?php bbp_get_template_part( 'bbpress/loop', 'topics' ); //restore the current blog to maintain formatting restore_current_blog(); } //this, along with ob_start(); above, allows for display within content, instead of at the top of the content/page $response = ob_get_contents(); ob_end_clean(); return $response; }
All css changes are within my child theme. For my child-theme’s style.css, I added this at the very bottom:
/*Changes made to accommodate plugin recent-bbpress-changes*/ #content ul { padding: 0; margin-left: 0; margin-right: 0; list-style: none; }
For my bbpress.css in my child-theme, I added this around line 158 or so:
li.bbp-topic-title { float: left; text-align: left; width: 20%;
One note on bbpress.css. The original bbpress.css had some combined lines that I separated. For example, I think that li.bbp-topic-title was combined with li.bbp-forum-info, as in the following manner:
li.bbp-forum-info, li.bbp-topic-title { float: left; text-align: left; width: 20%;
But I could be wrong. Doesn’t matter. Separating them appears to be just fine.
Got it solved! First, sorry for all the responses. I got lazy in posting the above instead of searching for the solution in the wordpress codex first. Turns out there is a function to return to the current blog, which I inserted within the “if” statement. Specifically, it is “restore_current_blog();”. Here is the working code:
<?php /* Plugin Name: Recent bbpress Topics Plugin URI: http://whosgotbooks.com/wp-content/plugins Description: A plugin to display recent bbpress topics across the network Version: 1.0.0 Author: Jerry Caldwell Author URI: http://whosgotbooks.com License: Open Source */ /* Here is the shortcode required [recent-bbpress-topic] */ function recent_bbpress_topics_shortcode() { recent_bbpress_topics(); } add_shortcode( 'recent-bbpress-topics', 'recent_bbpress_topics_shortcode' ); //Add Recent Topics to BBPress function recent_bbpress_topics() { switch_to_blog(9); if ( bbp_has_topics( array( 'author' => 0, 'show_stickies' => false, 'order' => 'DESC', 'post_parent' => 'any', 'posts_per_page' => 7 ) ) ) { bbp_get_template_part( 'bbpress/loop', 'topics' ); restore_current_blog(); } // Hook into action add_action('bbp_template_after_forums_loop','recent_bbpress_topics'); } ?>
For anyone who wants to use this, a few more points. One, don’t forget the formatting for style.css. Two, I changed that formatting within a child theme. Three, I can’t guarantee you will have the same results with your formatting as I have made many other formatting changes the past few months for my site. Lastly, I did not “network activate” my plugin. I only activated it on the main site.
As I get time I will add comments and try to increase the functionality of this plugin. Right now it is very basic. Maybe I’ll get good enough to publish it?Update on this. It turns out my code was insufficient. If have fixed it, sort of, which has led to another problem. First, here is the updated code:
<?php /* Plugin Name: Recent bbpress Topics Plugin URI: http://whosgotbooks.com/wp-content/plugins Description: A plugin to display recent bbpress topics across the network Version: 1.0.0 Author: Jerry Caldwell Author URI: http://whosgotbooks.com License: Open Source */ function recent_bbpress_topics_shortcode() { recent_bbpress_topics(); } add_shortcode( 'recent-bbpress-topics', 'recent_bbpress_topics_shortcode' ); //Add Recent Topics to BBPress function recent_bbpress_topics() { switch_to_blog(9); if ( bbp_has_topics( array( 'author' => 0, 'show_stickies' => false, 'order' => 'DESC', 'post_parent' => 'any', 'posts_per_page' => 7 ) ) ) { bbp_get_template_part( 'bbpress/loop', 'topics' ); } // Hook into action add_action('bbp_template_after_forums_loop','recent_bbpress_topics'); } ?>
Using the line “switch_to_blog(9);” works great to get the topics from site 9 to the main site. But now my problem is; it brings some unwanted behavior. The main content looks great, but the sidebars change to the format of site 9. Perhaps this has become a wordpress issue and not a bbpress issue? Perhaps I need to use code in addition to “switch_to_blog(9);” that prevents the unwanted behavior? If I need to change this to a wordpress thread, please let me know and I will. Otherwise, if you can provide a solution, that would be great.
Thanks Robin! Thanks Peter! After some trouble-shooting, the answer is as follows:
#content ul { padding: 0; margin-left: 0; list-style: none; }
For some reason, if I use “ul” twice after “#content”, it doesn’t quite work.
I was using firebug, and had identified line 118 in style.css, but just didn’t make the exact connection on how to change it. I used firebug again this morning, and learned how to use it better, so thanks for leading me to a great lesson!
Robin; if you want to make this into a plugin where the user gets some options on the back-end, I will be happy to help with the grunt work and you can post it and get the credit. Just let me know, and give me some direction.
Thanks again…PS For anyone who uses this code, I made the css changes in my style.css within my Child Theme, not the main style.css.
Thanks for your answer Robin. I’ve saved it for the future, when I need to modify a function that does have the filtering in place. Thanks.
If you saw this post before I edited it, disregard! Your solution worked. As always, thank you for your help.
Cheers…
Thanks Stephen. Worked great! At first it did not change the wording of the two primary topic views already listed, but a quick inspection with firebug allowed me to add two more custom views and everything looks great now. Thanks much!
Hi Robin. Any luck on this?
Okay, working like you said. This issue was, which I didn’t catch completely in one of your above posts, is that it only shows in my reply section, not in the original topic section at all. Like you mention, we can probably figure that one out. I do know javascript/ajax a little, so I’ll play around in that area to see what is what. I like your idea of making this a bbpress plugin; much needed in my opinion.
Update! I’m editing this post. I just used the following, replacing some wording in your filter:
add_filter('bbp_get_topic_content', 'spr_filter');
It is showing in my topics and not my replies! Bravo. That is because of your previous help with the if statements and $countr variable. I will share that full code with you via email if you like. Now to the javascript. That could take me a few hours, or a few days. Sigh…
Thanks Robin. This is not working for me right now. Might be something I am doing since your answers have been spot on. Did you actually implement this, and if so, did you change the settings to topics? I know it’s evening for you now, so don’t worry too much about this. I really appreciate your help so far. I’ll keep working on this for a few hours today, then I’m heading out on travel; if I don’t get it working today then it will have to be next weekend.
Hi Robin. To answer your questions; I only need this to attach to books, so only for bbpress topics. I don’t want replies rated. I tested it on wordpress posts, and it works there, but I’m not using wordpress posts. Probably the only modification I’ll make is to let users change their rating. Right now, once you give a star rating, it won’t let you change it later. Yes, I agree; it needs to hook to bbpress. It seems like it would be easy to do that, but I’m not certain.
Very nice! I had wine and steak last night. We will probably get snipped at for chatting on these boards, so I’ll stop now. Thanks again for your help today.
Okay Robin, you asked, so here it is! 🙂
Simple Rating plugin for wordpress – get it working for bbpress?
I should also mention, for anyone who finds this useful, that I made these changes within my theme files, not within the core bbpress files. Any changes made to the core bbpress files will likely be lost on an upgrade.
Thanks Robin! Once again you led me down the correct path. I might post a much more difficult question next, so if you are still willing I would love your continued help. In the event that someone else needs this solution, below is my entire content for loop-single-reply.php. I inserted comments for the if and else statements to hopefully make it self-explanatory. Cheers!
<?php /** * Replies Loop - Single Reply * Modified by Jerry for the Book Review site * need the global $countr variable for use below, * which is incremented in loop-replies.php * @package bbPress * @subpackage Theme */ global $countr; /* this variable $countr is incremented in loop replies with the following code: while ( bbp_replies() ) : bbp_the_reply(); $countr++; the $countr variable is set to zero $countr=0;, before the while loop. */?> <?php //Adding an if statement to show reply headers ONLY for reviews, not for initial topic if ($countr > 1) { ?> <div id="post-<?php bbp_reply_id(); ?>" class="bbp-reply-header"> <div class="bbp-meta"> <span class="bbp-reply-post-date"><?php bbp_reply_post_date(); ?></span> <?php if ( bbp_is_single_user_replies() ) : ?> <span class="bbp-header"> <?php _e( 'in reply to: ', 'bbpress' ); ?> <a class="bbp-topic-permalink" href="<?php bbp_topic_permalink( bbp_get_reply_topic_id() ); ?>"><?php bbp_topic_title( bbp_get_reply_topic_id() ); ?></a> </span> <?php endif; ?> <a href="<?php bbp_reply_url(); ?>" class="bbp-reply-permalink">#<?php bbp_reply_id(); ?></a> <?php do_action( 'bbp_theme_before_reply_admin_links' ); ?> <?php bbp_reply_admin_links(); ?> <?php do_action( 'bbp_theme_after_reply_admin_links' ); ?> </div><!-- .bbp-meta --> </div><!-- #post-<?php bbp_reply_id(); ?> --> <?php } //this is the closing bar for the if statement //this display the word Description where normally the title bar would be, then the else statement is //what displays the word Description else { ?> <center><strong><span style="font-size: 14px"; >Description </span></strong></center> <?php } ?> <?php if ($countr > 1) { /* this if statement is to separate css class replies from topics */ ?> <div <?php bbp_reply_class(); ?>> <div class="bbp-reply-author"> <?php do_action( 'bbp_theme_before_reply_author_details' ); ?> <?php /*commented out by Jerry - this was for trouble-shooting purposes echo 'countr is ' . $countr; */?> <?php if ($countr > 1) bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => true ) ); ?> <?php if ( bbp_is_user_keymaster() ) : ?> <?php do_action( 'bbp_theme_before_reply_author_admin_details' ); ?> <div class="bbp-reply-ip"><?php bbp_author_ip( bbp_get_reply_id() ); ?></div> <?php do_action( 'bbp_theme_after_reply_author_admin_details' ); ?> <?php endif; ?> <?php do_action( 'bbp_theme_after_reply_author_details' ); ?> </div><!-- .bbp-reply-author --> <div class="bbp-reply-content"> <?php do_action( 'bbp_theme_before_reply_content' ); ?> <?php bbp_reply_content(); ?> <?php do_action( 'bbp_theme_after_reply_content' ); ?> </div><!-- .bbp-reply-content --> </div><!-- .reply --> <?php } else { /* this else statement is to separate css class topics from replies. The below class calls and div classes were changed from reply to topic or content as required. */ ?> <div <?php bbp_topic_class(); ?>> <div class="bbp-content-author"> <?php do_action( 'bbp_theme_before_content_author_details' ); ?> <?php /*commented out by Jerry - this was for trouble-shooting purposes echo 'countr is ' . $countr; */?> <?php if ($countr > 1) bbp_reply_author_link( array( 'sep' => '<br />', 'show_role' => true ) ); ?> <?php if ( bbp_is_user_keymaster() ) : ?> <?php do_action( 'bbp_theme_before_content_author_admin_details' ); ?> <div class="bbp-reply-ip"><?php bbp_author_ip( bbp_get_reply_id() ); ?></div> <?php do_action( 'bbp_theme_after_content_author_admin_details' ); ?> <?php endif; ?> <?php do_action( 'bbp_theme_after_content_author_details' ); ?> </div><!-- .bbp-content-author --> <div class="bbp-topic-content"> <?php do_action( 'bbp_theme_before_topic_content' ); ?> <?php bbp_topic_content(); ?> <?php do_action( 'bbp_theme_after_topic_content' ); ?> </div><!-- .bbp-topic-content --> </div><!-- .content--> <?php } ?>
Okay, got it working! Thanks much for your help. One note: I had to change “if ($countr !=0)” to “if ($countr > 1)” to get the result I wanted. Evidently, bbpress is considering the first topic author as the first reply author. Strange, but whatever; it now works. Thanks again!
Hi Robin, thanks for your help so far. I had already made the theme folder and have been working with that for some time now, so I had no trouble following your suggestion.
Unfortunately this is not quite working, but I think you have me on the right path. First, I had to move the global $countr; at the beginning of the file to prevent a parse error. Now, this code removes the topic author, which is great. But…, it also removes the reply authors.
I want to keep the reply authors. Any suggestions?Here is my code in loop-replies.php:
<li class="bbp-body"> <?php if ( bbp_thread_replies() ) : ?> <?php bbp_list_replies(); ?> <?php else : ?> <?php $countr=0; ?> <?php while ( bbp_replies() ) : bbp_the_reply(); $countr ++; ?> <?php bbp_get_template_part( 'loop', 'single-reply' ); ?> <?php endwhile; ?> <?php endif; ?> </li><!-- .bbp-body -->
If anyone is able to give me a hand on this, one note. I’ve tried modifying content-single-topic-lead.php as per the codex. That did not work. My next route is to try modifying something within buddypress, which I have installed. Perhaps something in there is preventing these changes within bbpress? Grasping at straws at this point.
In reply to: bbress custom fields, auto-populate topic content@robin-w, can you please help me with another issue I am having? Here is the link to my posted question:
Is it possible to remove topic-author, but keep reply-authors
In reply to: bbress custom fields, auto-populate topic content@robin-w, here is my code for the custom fields. You will see where arguments and names match the variables being passed from javascript. This code was placed in a separate file as an additional plugin:
add_action ( 'bbp_theme_before_topic_form_content', 'bbp_extra_fields'); function bbp_extra_fields() { //This is where the cover will go, from google book search $value = get_post_meta( bbp_get_topic_id(), 'bookCover', true); echo '<label for="bookCover">Book Cover URL</label><br>'; echo "<p><textarea style='width: 250px; height: 50px' name='bookCover' value='".$value."'></textarea></p>"; //Input field for the Author $value = get_post_meta( bbp_get_topic_id(), 'bookAuthor', true); echo '<label for="bookAuthor">Author</label><br>'; echo "<p><input type='text' name='bookAuthor' value='".$value."'></p>"; $value = get_post_meta( bbp_get_topic_id(), 'bookPublished', true); echo '<label for="bookPublished">Published</label><br>'; echo "<p><input type='text' name='bookPublished' value='".$value."'></p>"; //Input field for the number of pages in print $value = get_post_meta( bbp_get_topic_id(), 'bookPages', true); echo '<label for="bookPages">Pages</label><br>'; echo "<p><input type='text' name='bookPages' value='".$value."'></p>"; //Input field for the Publisher that corresponds to the Published data $value = get_post_meta( bbp_get_topic_id(), 'bookPublisher', true); echo '<label for="bookPublisher">Publisher</label><br>'; echo "<p><input type='text' name='bookPublisher' value='".$value."'></p>"; //Input field for the ISBN $value = get_post_meta( bbp_get_topic_id(), 'bookISBN', true); echo '<label for="bookISBN">ISBN</label><br>'; // echo "<p><input type='number' name='bookISBN' value='".$value."'></p>"; echo "<p><input type='text' name='bookISBN' value='".$value."'></p>"; } add_action ( 'bbp_new_topic', 'bbp_save_extra_fields', 10, 1 ); add_action ( 'bbp_edit_topic', 'bbp_save_extra_fields', 10, 1 ); function bbp_save_extra_fields($topic_id=0) { if (isset($_POST) && $_POST['bookCover']!='') update_post_meta( $topic_id, 'bookCover', $_POST['bookCover'] ); if (isset($_POST) && $_POST['bookAuthor']!='') update_post_meta( $topic_id, 'bookAuthor', $_POST['bookAuthor'] ); if (isset($_POST) && $_POST['bookPublished']!='') update_post_meta( $topic_id, 'bookPublished', $_POST['bookPublished'] ); // if (isset($_POST) && $_POST['bookDescription']!='') // update_post_meta( $topic_id, 'bookDescription', $_POST['bookDescription'] ); if (isset($_POST) && $_POST['bookPages']!='') update_post_meta( $topic_id, 'bookPages', $_POST['bookPages'] ); if (isset($_POST) && $_POST['bookPublisher']!='') update_post_meta( $topic_id, 'bookPublisher', $_POST['bookPublisher'] ); if (isset($_POST) && $_POST['bookISBN']!='') update_post_meta( $topic_id, 'bookISBN', $_POST['bookISBN'] ); } add_action('bbp_template_before_replies_loop', 'bbp_show_extra_fields'); //add_action('bbp_template_before_main_content', 'bbp_show_extra_fields'); function bbp_show_extra_fields() { $topic_id = bbp_get_topic_id(); $valuec = get_post_meta( $topic_id, 'bookCover', true); $value1 = get_post_meta( $topic_id, 'bookAuthor', true); $value2 = get_post_meta( $topic_id, 'bookPublished', true); // $value3 = get_post_meta( $topic_id, 'bookDescription', true); $value4 = get_post_meta( $topic_id, 'bookPages', true); $value5 = get_post_meta( $topic_id, 'bookPublisher', true); $value6 = get_post_meta( $topic_id, 'bookISBN', true); //Below is the table format for displaying a book. Might consider changing this //to a form??? html header??? ?> <table style="width:900px"> <col width="100"> <col width="265"> <col width="535"> <tr> <td style="vertical-align:top"> <img src=<?php printf(rawurldecode($valuec))?> /> </td> <td style="vertical-align:top"> <strong><p style="font-size: 14px";>Author: </strong><?php printf( $value1); ?> </p> <strong><p style="font-size: 14px";>Published: </strong><?php printf( $value2); ?></p> <strong><p style="font-size: 14px";>Page(s): </strong><?php printf( $value4); ?></p> <strong><p style="font-size: 14px";>Publisher: </strong><?php printf( $value5); ?></p> <strong><p style="font-size: 14px";>ISBN: </strong><?php printf( $value6); ?></p><br> <strong><div style="font-size: 14px";>Description </strong></div> </td> </tr> </table> <?php }
It would probably also be instructive for me to include the review-meta.php file that is called by javascript, so here is that code as well:
if ( isset( $_POST['cover'] )) { $cover = $_POST['cover']; } if ( isset( $_POST['coverSmall'] )) { $coverSmall = $_POST['coverSmall']; } if ( isset( $_POST['title'] )) { $title = $_POST['title']; } if ( isset( $_POST['author'] )) { $author = $_POST['author']; } if ( isset( $_POST['published'] )) { $published = $_POST['published']; } if ( isset( $_POST['description'] )) { $description = $_POST['description']; } if ( isset( $_POST['pages'] )) { $pages = $_POST['pages']; } if ( isset( $_POST['publisher'] )) { $publisher = $_POST['publisher']; } if ( isset( $_POST['ISBN'] )) { $ISBN = $_POST['ISBN']; } $r=array("cover"=>$cover,"coverSmall"=>$coverSmall,"title"=>$title,"author"=>$author,"published"=>$published, "description"=>$description,"pages"=>$pages,"publisher"=>$publisher,"ISBN"=>$ISBN); print(json_encode($r));