Step by step guide to setting up a bbPress forum – Part 1

Step by step guide to setting up a bbPress forum – Part 1

Step by step guide to setting up a bbPress forum – Part 1
Codex Home → Step by step guide to setting up a bbPress forum – Part 1
bbPress is a plugin for WordPress that adds forum capability to your site.
bbPress inherits your currently active theme』s styles where it can to try and match your themes style. This can work really well on some themes, but sometimes doesn』t work well on others, so some styling may be needed. Out of the box, it works well with most of the default WordPress themes , but like I said it might need minor CSS styling to fix some layout issues.
Setting up WordPress requires some or all of the following stages depending on how you want it to look.
The instructions here generate a forum working within a twentyten theme that looks like:

 
In summary you need to

Install the plugin
Set up some test forums or import a forum
Create a WordPress page for your forum display
Amend any other settings
Hidden vs. public forums – decide which or a mixture suits you
Decide on registration/user logins – automatic or manual?
Manually adding users to your forum – if you have chosen this route.
Make sure bbPress uses the page template we want
Add a custom sidebar or use existing sidebar with conditional logic

So let』s get started.
 
1. Installing the plugin
So we』ll assume you have a site with wordpress installed
Go to dashboard>plugins>add new
You will see bbPress part of the featured plugins then install and activate.
You can also type 『bbpress』 into the search bar , and that should lead you to the latest version then install and activate from there.
 
2. Set up some test forums
Go into Dashboard>forums>add new
Add a forum and name it whatever you want, but you need at least one to get the forum page going (you can always delete it later).
Now go into the dashboard>topics and create a new topic.  Again you can trash this later, but you』ll want to see what one looks like.
Okay, so now we have bbPress with one forum and one topic, but we』ll need to show it somewhere,  so go to 3 below.
2. Alternative  –  Import a forum !
If you』re planning on migrating from a different forum and would like to use bbPress but keep all of your original forum data, then you will need to import your existing forum data into bbPress.
To import forums go to Dashboard > Tools > Forums and go to the tab 「import forums」
If you need anymore help during this importing process there is a guide that will help you here.
https://codex.bbpress.org/document/
 
3. Creating a forum page
Now we have some forums, we need to have somewhere to display them.
There are two ways to do this. Most themes support method 1, and this can (can not will) produce tighter theme integration on style . However Method 2 allows you to have some introductory words. Your choice…!
Method 1
This requires a couple of checks first
a. In Dashboard>settings>forums look for what is set in the forum root under the forum root slug heading. The default is 『forums』 but you can set this to anything
b. In Dashboard>settings>permalinks check that the common settings check is set to 『postname』 If it isn』t and you don』t want to change it, then use method 2.
Now just create a wordpress page that has the heading that was found in a. above eg if the root slug is 『forums』 create a page with the title 『forums』, and save.
Once the permalink has been created, you can rename the page to whatever you wish eg 『mysite forums』 (as the permalink will stay the same, and it is this permalink that bbpress looks for)
Save this page, add it to a menu if you are using custom menus, or decide its order if you have automatic menus. Then publish it to your site.
Job done!
Method 2
If either your theme doesn』t work with method 1, you don』t want permalinks with postname, or you want to use some introductory text (eg 「welcome to our forums…」, then method 2 is for you.
In dashboard>pages
go to add new page
Call this 『Forums』 or whatever you want your menu item to be.
Then in the content section, you may want to have some introductory words 「welcome to the forum」 etc. then add the following bbpress shortcode
[bbp-forum-index]
Save this page, add it to a menu if you are using custom menus, or decide its order if you have automatic menus. Then publish it to your site.
You will now have a forum page, and a forum to display.
 

4. Amending any other settings
There are several parameters which you can set to get bbPress working as you want it.
These are accessed via Dashboard>Settings>forums
See  https://codex.bbpress.org/document/configuring-bbpress/forum-settings/  for further details.
 
5. Forum Visibility and access
You can use a combination of forum visibility and registration to set up who can :
see your forums
post in your forums
Forum visibility:
Public – Anyone can see these forums
Private – Only logged in registered users with a forum role  can see these forums
Hidden: Only Moderators/keymasters can see these forums
Visibility is set for each forum, go into Dashboard>forums> and when you add/edit a forum, you will see visibility options on the right hand side in the forum attributes.
Users then have a forum role which can be
spectator
particiapant
moderator
keymaster
blocked
 
Forum access
You can set whether users need to be registered and control the registration process.
These two controls give you various degrees of control
Public viewing, public posting
By default forums are available to be publically viewed.  If you set anonymous posting to be on (see 4. Above) then anyone can also post.  This is the most public version of the forum.  It can lead to lots of spam being posted, and does hand control of what』s said in your forums to the internet community, with you effectively being able to only delete afterwards.
Public viewing, registered user posting
Here anyone can view the forums, but only registered users can join in and post.  Registration can be automatic or manual – see 6. below
private viewing, registered user posting
Here you have a private forum. Only registered users can view and post.  Here it would be very unusual to allow automatic registration, and this will be typically a manual process.  To set a forum as private go into dashboard>forums and select edit for the forum you wish to be hidden.  Under the forum attributes on the right hand side, set visibility to private.
 
6. Automatic vs manual registration
If you do not permit anonymous posting, then your users will need to register with your site. It is up to you to decide which method is preferable.
Automatic registration is easier and it does allow people to quickly sign up, but by the same token gives you less control over who is contributing to your forums, and can lead to spam postings.
Manual signup involves you in more administration, but does give you complete control over who has write access.
Neither is better overall, but one will be better for you !
To allow automatic registration, go to Dashboard > Settings > General settings and under membership, tick 「anyone can register」.  Then in Dashboard > settings > forums tick the auto role, and ensure this is only at participant level.
To set manual registration, go to Dashboard > Settings > General settings and under membership, and ensure  「anyone can register」 is not ticked.  Then in Dashboard>settings>forums tick the auto role, and ensure this is only at participant level.
In both cases, you will then need a screen for users to register – we』ll come back to where to put this screen later, but for the moment go into dashboard>pages>add new page and create a page.  You can call this page anything you like but 「register」 or 「registration」 would be logical when users view or access its URL.
If you are doing automatic registration, then put whatever preamble you』d like (「to register for our forums」 etc.) , and then add the following shortcode  [bbp-register].  Save the page.
If you opt for manual registration, then you』ll either need to get your users to email you, or set up a contact form for them to use.
To get you going quickly, maybe just set the page to have you preamble followed by an email link such as
email us your details here
Remember to write this in the 「text」 tab of the content, so that the html is understood.
There are lots of contact form plugins, which you can use later to get a registration form going on this screen.  By all means set this up now, but a placeholder email will get you enough to see how it works.
So now we have a page where our users can register.
 
7. Manually adding forum users to your site and editing them.
If you have opted for manual registration (and it』s what  I do), then when you receive either a contact form or email as set up in 6 above, you』ll need to add each user.
Go into Dashboard>users>add user
Enter their details as required.  You can use the 「send email」 to automatically send them their details from within your site, or you can email external to the site afterwards using your favourite software.  I use an email template within outlook as I can then send 「joining instructions」  and 「forum rules」 as part of the signup.
You』ll not see any bbPress forum related user options/settings at this stage.
If you want to set/amend access level – say to make a user a moderator, then you』ll need to create the user first, and then go back into edit, and now you』ll see an option at the bottom of the amend screen to allow you to change access levels.
Manually adding users also allow you to take away the 「toolbar」 , which I feel always draws away from the look and feel of the site.  We』ll give profile access later in this setup, so it』s not needed.
WHERE HAVE WE GOT TO?
So we now have a forum (or two) on a page which is accessible from the menu.  We』ve decided if any/all forums will be hidden, and who and when people can contribute.  We』ve set up a registration process (albeit the manual one might be a bit basic).
However we』ve not given anyone a place to logon, or indeed register, or anywhere where they can see the latest posts.
You can do this by adding these into a sidebar, or if you are using a full-width page, but adding them to a menu.
But first you need to see if you need to change what page template bbPress is using for your site.
 
8. Make sure bbPress uses the page template we want
So you may want bbpress to display differently than the template it chooses for instance you might want your forum on a full width page and find that it uses a sidebar template, or visa versa.
If you have some knowledge of editing files then follow this guide to help create a bbpress.php file to use for all your bbPress pages.
https://codex.bbpress.org/theme-compatibility/getting-started-in-modifying-the-main-bbpress-template/
Otherwise a more basic explanation follows :
By default bbPress will use a page template in your theme root folder.
It will look for the existence of several files in the following order, using the first one it finds.
plugin-bbpress.php
bbpress.php
forums.php
forum.php
generic.php
page.php
single.php
index.php
Typically most (but not all) themes have a page.php, so if you do nothing else, chances are that that』s the one it will use.
To get bbpress to use a different template from the one your theme is using just copy that template and rename it bbpress.php within your theme root. This is explained a bit more below.
So you may want bbpress to display differently than the template it chooses for instance you might want your forum on a full width page and find that it uses a sidebar template, or visa versa.
You can even find that bbPress appears differently when displaying the forum index than it does with topics, which is probably not what you want. Nowadays theme developers will quite often use 「if…」 code within the page template to change the display of types of pages. Since topics share the same characteristics as posts, but the index page is more like an 「about」 page, the theme can believe that it should show then in different ways.
If bbPress is using the page layouts you want, then that』s fine, and you can ignore the rest of this section.
If not, then the following will let you set bbPress up in the way you want.
You theme will have page templates in the root of the directory, that is :
wp-content/themes/%mytheme%/ where %mytheme% is your theme』s name.
You』ll need to find the name of the page template that you want to use. Look in your theme』s directory – normally they are named very obviously – eg pagefullwidth or pagesidebar. If it』s not obvious, you can see what the template is called by editing a page that has the correct layout, and looking at the name of the template on the right hand side of the edit screen in a panel under the publish/update panel. If you then open the various page.php files, you see that they are named in the first few lines, so you can easily match the name.
So once you know which page template file you need, copy this and rename the copy to bbpress.php
Place this file in the root of your theme ie wp-content/themes/%mytheme%/bbpress.php where %mytheme% is your theme』s name.
bbpress will the use this file for all it』s pages.
If you want to customise the layout for your forums, you can then do this within this file.
 
9. Creating a forum specific sidebar
For most sites, the forum is part of a larger experience, and it is really useful to have a separate sidebar for the forum page that has forum specific widgets on it.
If your forum is all that is on your site, then the default sidebar may be all you need. But If you want to add a sidebar just for your bbPress pages then please follow this tutorial.
Creating a bbpress specific sidebar
 
10. Adding forum menu items
If you don』t use sidebars or footers to display your login and register items, (or even if you do) you』ll probably want to add some menu items.
You can do this quite easily here is how.
First you go to Appearance > Menus.
If you haven』t created a menu for your site , then create one.
You then select the menu you want to edit.
Now you go to the Custom Links section to your left and enter the url of the form you want to lead them to.
So the url of the login form of your site would be
http://yoursite.com/wp-login.php
Where 「http://yoursite.com」 is your sites url.
And the Link Text would be 「Login」.
If you want to put a link to the registration form it would be
http://yoursite.com/wp-login.php?action=register
And the lost password form would be
http://yoursite.com/wp-login.php?action=lostpassword
You can of course use front-end login forms and link to them,
You can also use this plugin to control when to show these menu links to users when they or logged out or haven』t created an account to your site.
https://wordpress.org/plugins/menu-items-visibility-control/
 
AND THAT』S IT !
Now you have a fully functional forum !!!!
Users can register, login, see what forums are available, and contribute.
It may look a little rough at points, this will be clashes between bbPress and your theme, and we』ll start to deal with these in part 2

Step by step guide to setting up a bbPress forum – part 2

Step by step guide to setting up a bbPress forum – part 2

Step by step guide to setting up a bbPress forum – part 2
Codex Home → Step by step guide to setting up a bbPress forum – part 2
WHERE ARE WE?
Part 1 dealt with getting the forum going.
Part 2 deals briefly with plugins, but mainly with changes to styling and bbPress.css
Part 3 will dealt with layout and functionality.
The forum is now fully functional, but probably you』ll want to change some of how it looks. If you』re using anything but a default theme, I expect that the colors won』t look right, and you may want to change some fonts.
You may also want to change where elements are displayed. Or add other functions to your site.
So there are two types of change we might want to make to our site –

changes in style – how it looks, and
changes in functionality – how it works.

Changing bbPress to achieve both these can consist of 4 things:
1. Changing CSS
2. Adding Plugins
3. Changing function files
4. Changing PHP files
This part 2 deals with changes in style which means primarily changing css.
Summary for techies
If you』re already into css, then bbpress uses by default a file called bbpress.css located in wp-content/plugins/bbpress/templates/default/css
Either just override styling in your theme』s css, or copy the bbpress.css file to a folder
/wp-content/themes/%your-theme%/css/
and bbpress will use that file, so make changes there.
A quick bbpress styling crib is available to get you started.
1. What is css?
CSS stands for Cascade Style Sheet – it』s a simple way to define elements of the site that will be used often. For instance if parts of your website have a heading, then defining a heading style once (font size, color, font used, bold, italic etc.), and then simply referring to it each time a heading is displayed is much easier than defining it each time.
Now we can override the default CSS elements in 4 ways

With a css plugin
Adding code to our child theme』s style.css
Copying/creating a bbpress stylesheet in your child theme
With functions (see below)

2. Using a CSS plugin
There are several around, including https://wordpress.org/plugins/simple-custom-css/ just google 『WordPress css plugin』 to find one right for you.
Using these you can add css to your site without needing to code in files, and if you』re only doing a few small changes, this can be an easy way without fearing that you』ll ruin a file on your website.
However if you doing all but a few small changes, you would be better looking at the remaining options
So in this section we』ll deal with b & c.
But before we do that, we need to understand a bit more about how to change files without losing our work later on.
3. Changing files Overview – get a child theme !
To make bbPress work or look better without plugins, we』ll need to change files.
Now if we simply change the files in bbPress, then any updates to bbPress will risk losing these changes, and undoing all your hard work.
So we will make changes in our WordPress themes, leaving alone the bbpress plugin directory.
If you』re in to writing your own WordPress themes, then I suspect you』ll be skim reading this article, so the next bit is for those of you that are either using a WordPress default (twentyten, twentyeleven etc.) or have chosen one of the many free or paid for themes.
If you have gone for any of the above, then again if you simply make changes to the theme, then any updates to your theme will risk losing these changes, and again undoing all your hard work.
If you are a bit more advanced, you may already have a child theme, and it is here that we』ll make additions and changes. That way updates will not affect lose the changes we have carefully crafted.
If you don』t currently have a WordPress child theme, then the best way to proceed is to create one, and then you can make changes there.
If you don』t know how to create one, the WordPress Child Theme training module is a great place to start, also search google 「child theme wordpress video」 and you』ll find lots of demonstrations. If someone has built a site for you, it may already be a in a child theme, and again the video』s will let you see whether this is the case.
So let』s presume you now have a child theme…
bbPress picks on the styles used in your theme for some elements, but uses its own CSS style sheet for others. This default css works well with the wordpress twentyten theme, but may not look right with your theme.
4. Adding code to our child theme』s style.css
If you』re making just a few changes, then adding the code to your child theme』s style.css is the easiest way to go
We』ll come later to the key bits of code you might want to change, but for the moment let』s suppose that you don』t like the way bbPress lists the forums.
The default looks like :

And you』d like it to look like :

This element is styled in the original bbPress.css file as followed
#bbpress-forums .bbp-forums-list li {
display: inline ;
font-size: 11px;
}
Changing the 『display:inline』 to 『display:list-item』 will achieve this. But as we said above, whilst you could simply amend line 121 (in wp-content/plugins/bbpress/templates/default/css/bbpress.css) you don』t want to alter this, as upgrades to bbPress versions might well overwrite this.
So we simply enter the appropriate code in our child theme』s style.css.
We can enter the entire 「chunk」 of code, and if you』re copying from bbpress.css, then copying the entire chunk and then altering just the bits you want will help you remember what other parameters are there
So
#bbpress-forums .bbp-forums-list li {
display: list-item !important;
font-size: 11px;
}
achieves this. Equally well you could post
#bbpress-forums .bbp-forums-list li {
display: list-item !important;
}
ie just the bits of the code that you actually want to change.
NOW you may have spotted that I』ve added 『!important』 to the line. This is because our theme style.css file will execute first, then the bbpress.css (being a plugin) will execute afterwards. Since the later bbpress.css file contains the original code, this will simply overwrite the changes you have made. By entering 『!important』 to the line, you tell WordPress not to allow this code to be overwritten by later code, so preserving your change.
So that』s method 1, using your child theme』s style.css.
5. Copying/creating a bbpress stylesheet in your child theme
The alternative method is to either take a copy of bbpress』 default css or create a brand new one, and put this in a specific area of your theme that bbpress looks for.
For .css files, bbpress looks at you theme files in the following location :
/wp-content/themes/%your-theme%/css/
where %your-theme% is the actual directory name of your theme.
So any .css file found in this location will be used. I am not sure if this is instead of the bbpress.css file in the plugin default location or in addition to it. You will need to check that yourself, as at the time of writing I have not!
So your best bet is to copy the bbpress.css from wp-content/plugins/bbpress/templates/default/css/bbpress.css
To a directory called css as above in your theme directory.
You can then edit this theme to your heart』s content, knowing that it won』t be overwritten by any bbpress upgrades.
So if you』re now ready to start changing code but what can you change?
See bbPress styling crib to get you started.
6. Changing colors
For people starting out with css, knowing what exact color something is  can be tricky. So if your main theme has a background of a light blue – how do you get that exact same shade in bbpress.
More technical people will use features within browsers to show them the underlying information. For instance many people use a product called 「firebug」 with a Firefox browser to show them exactly what is happening, including what exact colours are being used for text and backgrounds. These tools can be extremely useful as you progress, and it』s worth getting to know them if you plan more than a couple of changes. Google 「firebug video」 to get lots of help such as Firebug tutorial
An alternative if colors are what you』re after is to use a product like colorcop which will let you see exactly what color is being used on any part of your screen, so copying either your own theme』s colors or someone else』s is really easy.
The next section deals with changing layout and functionality part 3

Step by step guide to setting up a bbPress forum – part 3

Step by step guide to setting up a bbPress forum – part 3

Step by step guide to setting up a bbPress forum – part 3
Codex Home → Step by step guide to setting up a bbPress forum – part 3
Part 2 of the guide dealt with styling changes.
This part will deal with changing what appears and where it appears – changing some of the functionality of bbPress.
Now you can alter functionaility in many ways. These include
1. with plugins
2. with functions
3. by changing bbPress files
4. adding filters
5. adding hooks
1. Plugins
If you』re not familiar with coding, then plugins can offer a great way to add functionality without needing to do coding. There are lots around, if you』re after something it』s always worth trying a google search with 「bbpress plugin」 and whatever you are after.
Some I have used include :
bbP signature – lets users add a 『signature』 to their posts – used a lot in forums
bbP topic views – displays how many times a topic has been viewed
and ones that might well be useful, but I haven』t used :
bbpress-notify – notifies admins, moderators etc. (fully configurable to role) when new post and replies are made
The rest of this part will assume that you have a child theme, if you』re not familiar with what this is and why you need it, please visit part 2 of this guide where it is all explained.
So lets assume you have a child theme.
2. Functions
A short guide on how to create a child theme and functions file can be found here
Functions files and child themes explained
3. Changing bbPress files
This part of the guide is primarily going to deal with the bbPress templates that the default bbPress plugin uses to display your forum.
NOTE: This part deals with changing templates that belong to the bbPress Plugin, and these changed templates should then reside within a bbpress folder in your theme.  HOWEVER if you create a bbpress.php file from a theme template or php file (that is a file that sites in your theme such as page.php) , then this resides in your theme』s root.  So if the source is the plugin, then bbpress folder;  if the source is your theme, then your theme folder !
Changing these files gives you a powerful way to improve how your forum works for your users, and in some cases making it easier for you to administer.
In part 2 we explained how changing files in the bbPress plugin was not a good idea, and these files are frequently overwritten on an upgrade. So to change files, you』ll need to copy them you your child theme.
Bbpress will normally use the default files found at
wp-content/plugins/bbpress/templates/default/bbpress
However if you create a directory in your child theme at
/wp-content/themes/%your-theme%/bbpress/
(where %your-theme% is your child theme』s name)
then any files stored there will be used instead.
This means that if you want to alter how a topic displays, then copying the relevant file (in this case loop-single-reply) to this folder will mean that bbPress uses this file instead, so you can now alter it to work how you want.
There are two sources that you might want to use for files – one is at
wp-content/plugins/bbpress/templates/default/bbpress
For instance in this bbPress folder you』ll find
loop-single-reply – this file displays a topic and all its replies letting you change the heading and the format within a post – for instance removing the avatar or adding extra words below it.
If you want to alter lower level files, then the other source is
wp-content/plugins/bbpress/templates/default/extras
Here you can alter for instance whether a single topic has a footer.
To alter any of these lower level files they need to be copied to the root of your child theme that is to
/wp-content/themes/%your-theme%/
(where %your-theme% is yout themes name).
So bbPress offers a simple way to change the layout and information displayed.
As an example, the default forum display looks like

which displays the forum counts for the sub-topics.
If you wanted to take these out to look like

Then you would need to change
loop-single-forum.php by changing how the code displays
so you would change line 44  from
 

 
to read
 
false,
『show_reply_count』    => false,
『separator』 => 」,
));  ?>
 
So copy the file loop-single-forum.php from wp-content/plugins/bbpress/templates/default/bbpress to /wp-content/themes/%your-theme%/bbpress/
and add the above function. Then save, and hey presto, the counts have gone !
For some starters on what you might want to change have a look at Layout and Functionality – Examples you can use
Part 4 deals with adding actions and
Part 5 deals with adding filters

bbPress 样式参考

bbPress 样式参考

本指南将向您展示新 bbPress 论坛的区域,您可以使用 CSS 以及 bbPress 插件中使用的类和选择器来设置每个区域的样式。

设计您的论坛

这些可以添加到您的 css 中,或添加到 bbpress.css 的自定义版本中 – 请参阅设置 bbpress 论坛的分步指南第 2 部分了解更多信息。

论坛婴儿床

论坛

bbPress 论坛中的所有内容

这是用于 bbPress 论坛所有部分的 CSS 。根据您的需要增加字体大小。

#bbpress-forums {
background: transparent;
clear: both;
margin-bottom: 20px;
overflow: hidden;
font-size: 12px;
}

1. 论坛页眉和页脚

这是显示作者、帖子和声音等标签的内容。

#bbpress-forums li.bbp-header,
#bbpress-forums li.bbp-footer {
 background: #f3f3f3;
 border-top: 1px solid #eee;
 font-weight: bold;
 padding: 8px;
 text-align: center;
}


#bbpress-forums li.bbp-header {
 background: #eaeaea;
}

2. 回复标题

#bbpress-forums div.bbp-reply-header {
    background-color: #f4f4f4;
}

3. 模板通知信息

div.bbp-template-notice.info {
    border: #cee1ef 1px solid;
    background-color: #f0f8ff;
}

4. 话题/回复背景

赔率,即使如此回复也有交替的颜色

#bbpress-forums div.even,
#bbpress-forums ul.even {
 background-color: #fff;
}


#bbpress-forums div.odd,
#bbpress-forums ul.odd {
 background-color: #fbfbfb;
}

如果话题很粘或超级粘,那么

.bbp-topics-front ul.super-sticky,
.bbp-topics ul.super-sticky,
.bbp-topics ul.sticky,
.bbp-forum-content ul.sticky {
 background-color: #ffffe0 !important;
 font-size: 1.1em;
}

自定义关闭话题文本的颜色。

#bbpress-forums .status-closed,
#bbpress-forums .status-closed a {
 color: #ccc;
}

每个线程的线程回复边距。

#bbpress-forums ul.bbp-threaded-replies {
 margin-left: 50px;
}

5. 各种文字

这些通常继承自您的主题。

但是,您可以使用修改内容

#bbpress-forums .bbp-topic-content,
#bbpress-forums .bbp-reply-content {
    font-size: 12px;
}

上面的代码不在 bbpress.css 中,因此如果需要,请添加它,或放入您的 .css 中,请参阅顶部提到的指南以获取更多信息

6. 管理链接

span.bbp-admin-links {
 float: right;
 color: #ddd;
}


span.bbp-admin-links a {
 color: #bbb;
 font-weight: normal;
 font-size: 10px;
 text-transform: uppercase;
 text-decoration: none;
}

7. 模板通知

div.bbp-template-notice,
div.indicator-hint {
 border-width: 1px;
 border-style: solid;
 padding: 0 0.6em;
 margin: 5px 0 15px;
 border-radius: 3px;
 background-color: #ffffe0;
 border-color: #e6db55;
 color: #000;
 clear: both;
}
div.bbp-template-notice a {
 color: #555;
 text-decoration: none;
}
 div.bbp-template-notice a:hover {
 color: #000;
 }
 div.bbp-template-notice.info {
 border: #cee1ef 1px solid;
 background-color: #f0f8ff;
 }
 div.bbp-template-notice.important {
 border: #e6db55 1px solid;
 background-color: #fffbcc;
 }
 div.bbp-template-notice.error,
 div.bbp-template-notice.warning {
 background-color: #ffebe8;
 border-color: #c00;
 }
 div.bbp-template-notice.error a,
 div.bbp-template-notice.warning a {
 color: #c00;
 }
 div.bbp-template-notice p {
 margin: 0.5em 0 6px 0 !important;
 padding: 2px;
 font-size: 12px;
 line-height: 140%;
 }

8. 从您的主题继承的面包屑和链接颜色

9. 其他造型功能

9.1 提交按钮

正在寻找一种方法来更改 bbpress 、更大、颜色中的提交按钮......?

#bbpress-forums .submit {
    background-color: #a0668d;
    color: #FFFFFF;
    float: center;
    vertical-align: middle;
    border-radius: 10px;
    height: 35px;
    width: 80px;
    font-size: 18px;
}

并更改为您想要的!

10. 从最近的话题小工具和新鲜度中删除头像

widget_display_topics .bbp-author-avatar {
    display: none ! important;
}


.bbp-topic-freshness-author  .bbp-author-avatar {
    display: none ! important;
}

布局和功能 - 您可以使用的示例

布局和功能 - 您可以使用的示例

您可以使用 WordPress 和 bbPress 提供的钩子和过滤器以多种方式自定义和扩展 WordPress 和 bbPress,还可以将一些 CSS 应用到您的论坛或主题。

下面是一些常用的自定义 PHP 和 CSS 代码片段列表,这些代码片段可能对社区用户自定义他们现有的 bbPress 论坛有用。

如果您发现列出的示例似乎不起作用,您想建议另一个有用的示例,或者使用不同/更好的方法来编辑当前的最终功能,然后请进入支持论坛并在论坛请求和反馈

1. 更改论坛列表的显示方式

默认看起来像: 论坛2

改成 : 论坛3

默认情况下,此元素使用 CSS 设置样式。

#bbpress-forums .bbp-forums-list li {
display: inline;
}

将此 CSS 代码片段粘贴到您的子主题的 style.css 并更改 display:inline 为 display:block 将实现此布局。

您也可以改用 PHP 代码片段,并将以下函数放入您的子主题 functions.php 文件中。

//create vertical list subforum layout
function custom_bbp_sub_forum_list() {
  $args['separator'] = '
';
  return $args;
}
 add_filter('bbp_after_list_forums_parse_args', 'custom_bbp_sub_forum_list' );

2. 从论坛列表中删除话题/回复计数

默认 :

论坛c
更改为:
论坛b
您可以更改 loop.single.forum.php,如 https://codex.bbpress.org/step-by-step-guide-to-setting-up-a-bbpress-forum-part-3/ 所示或者

将以下内容放入您的子主题 functions.php 文件中。

function remove_counts() {
    $args['show_topic_count'] = false;
    $args['show_reply_count'] = false;
    $args['count_sep'] = '';
return $args;
}
add_filter('bbp_before_list_forums_parse_args', 'remove_counts' );

3. 向登录小工具添加 「修改个人资料/密码」

虽然您可以单击 bbPress 登录小工具中显示的头像或您的用户名转到您的个人资料,然后从那里单击编辑链接来编辑您的个人资料,但这对您的某些用户来说可能并不那么明显。

安装这个插件 RKK Login Widget,它几乎与默认的 bbPress Login Widget 完全相同。只需将 (RKK) 登录小工具放置到侧边栏之一,您的登录小工具就会与此类似。

论坛

根据需要自定义或编辑插件。

4. 关闭或更改面包屑

要删除论坛中的面包屑,请确保将此功能添加到子主题或功能插件中的 functions.php 文件中。

add_filter( 'bbp_no_breadcrumb', '__return_true' );

要自定义您要显示的特定面包屑链接的布局,请将其添加到您的子主题 function.php 文件中。

function mycustom_breadcrumb_options() {
    // Home - default = true
    $args['include_home']    = false;
    // Forum root - default = true
    $args['include_root']    = false;
    // Current - default = true
    $args['include_current'] = true;


    return $args;
}
add_filter('bbp_before_get_breadcrumb_parse_args', 'mycustom_breadcrumb_options');

5. 移除论坛搜索功能

如果您想删除搜索栏 (有些人喜欢要么排除它,要么把它放在侧边栏中)

默认情况下,搜索表单将显示如下。
论坛

这是之后的结果。
论坛

然后只需更改仪表盘> 设置> 论坛功能中的设置并取消勾选
Allow forumwide search 。

6. bbpress 后加载样式表

介绍:当 WordPress 加载样式表时,它们会先加载所有 theme.css 文件,然后再加载插件 css 。这样插件 css 的优先级高于您的 css,唯一可以覆盖它的方法是使用 !important 。众所周知,这是一种不好的做法。

此外,您可以在您的主题中创建自己的 bbpress.css ,但这意味着需要处理更多文件 (尽管这是最干净的解决方案) 。这是执行此操作的另一种方法:

将此代码粘贴到您的子主题 functions.php 文件中。

/** * load custom bbpress css after bbp default css */ wp_enqueue_style( 'custom-bbpress', get_stylesheet_directory_uri() . '/css/custom-bbpress.css',array('bbp-default'),'1.1','all');

7. 如何从模板中删除所有面包屑并将它们设置在页面顶部?

由 Markic 提交并感谢

简介:我们中的许多人正在使用标题中的现有面包屑系统创建自己的主题。但是 bbPress 有自己的想法在哪里显示面包屑,有时在页面顶部,有时在搜索表单下方,有时在标题下方。这是您可以快速更改的方法。

1. 如果还没有,请在主题的根文件夹中创建 「bbpress.php」 。
2. 添加

<div class="truebreadcrumbs"><?php bbp_breadcrumb(); ?></div>

3. 添加此 CSS:

div.bbp-breadcrumb {
 display: none; /*this will hide all breadcrumbs*/
 }


.truebreadcrumbs div.bbp-breadcrumb {
 display: block; /*this will display breadcrumbs you've created*/
 }

当然,HTML 仍然会生成所有的面包屑,而 css 会隐藏不需要的面包屑,但这是最简单的方法,无需进入模板。

8. 将 「编辑个人资料」 添加到 wordpress 菜单

大多数 bbPress 用户都有侧边栏,并使用小工具来控制登录和个人资料更改。其他人则依赖于用户知道如果点击他们的头像或用户名,那么他们就可以编辑他们的个人资料。

此解决方案允许您将 「编辑个人资料」 添加到您的 wordpress 菜单中——这仅在用户登录时显示为菜单项。

将以下内容添加到您的函数文件中

// Filter wp_nav_menu() to add profile link
add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
function my_nav_menu_profile_link($menu) {
    if (!is_user_logged_in())
        return $menu;
    else
        $current_user = wp_get_current_user();
        $user=$current_user->user_nicename ;
        $profilelink = '<li><a href="/forums/users/' . $user . '/edit">Edit Profile</a></li>';
        $menu = $menu . $profilelink;
        return $menu;


}

9. 将社交媒体联系人添加到 bbPress 个人资料

感谢 nicmare 提供这个方便的提示。

您可以通过将这些添加到 contactmethods 来添加和删除社交媒体联系人。

以下示例函数添加了 twitter 、 facebook 、 google+ 和 youtube,但删除了
目标、 yabber 和 yim 。

function add_extra_contactmethod( $contactmethods ) {
// Add new ones
$contactmethods['twitter'] = 'Twitter';
$contactmethods['facebook'] = 'Facebook';
$contactmethods['googleplus'] = 'Google Plus';
$contactmethods['youtube'] = 'Youtube';


// remove unwanted
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);


return $contactmethods;
}
add_filter('user_contactmethods', 'add_extra_contactmethod');

10. 缩短新鲜度的措辞

此代码缩短新鲜度取出分钟

只需将其放入您的函数文件中

//function to shorten freshness display from say '1 month, 2 weeks' to '1 month'
function short_freshness_time( $output) {
$output = preg_replace( '/, .*[^ago]/', ' ', $output );
return $output;
}
add_filter( 'bbp_get_time_since', 'short_freshness_time' );
add_filter('bp_core_time_since', 'short_freshness_time');

11. 简单地在菜单中添加登录/注销

为用户添加用于登录/注销、注册或恢复其密码的菜单项到您的站点菜单非常有用,尤其是在您决定不使用 bbPress 侧边栏登录小工具的情况下。

将以下 php 代码片段粘贴到您的子主题的 functions.php 文件中。

add_filter( 'wp_nav_menu_items', 'rkk_add_auth_links', 10 , 2 );
function rkk_add_auth_links( $items, $args ) {
 if ( is_user_logged_in() ) {
 $items .= '<li><a href="'. wp_logout_url() .'">Log Out</a></li>';
 }
 elseif ( !is_user_logged_in() ) {
 $items .= '<li><a href="'. site_url('wp-login.php') .'">Log In</a></li>';
 $items .= '<li><a href="'. site_url('wp-login.php?action=register') .'">Register</a></li>';
 $items .= '<li><a href="'. site_url('wp-login.php?action=lostpassword') .'">Lost Password</a></li>';
 }
 return $items;
}

您可以根据自己的喜好自定义此功能。如果您想将某些 url 链接到自定义前端表单而不是默认的 WordPress 登录表单,您可以进行这样的简单更改,例如。

site_url('wp-login.php') 对此 site_url('/login/')

如果您想在注销后重定向到自定义 url 。

wp_logout_url() 把这个改成这个 wp_logout_url( site_url('/logout/') )

或者,如果您想在特定菜单中显示这些菜单项,请在 之后的函数中编辑两个条件 is_user_logged_in() 。

像这样添加到现有的条件中,is_user_logged_in() && $args->theme_location == 'primary-menu'.

上面的代码将在您的功能条件中显示特定菜单中的菜单项,其中主题位置通常是菜单段。要查找此功能的菜单的 slug/主题位置搜索,通常位于主题 functions.php 文件中 register_nav_menu() 。有关了解 register_nav_menu() 函数的更多信息,请参阅 WordPress 指南导航菜单

12. 添加模态 (弹出) 登录

这非常好,说明将其添加到菜单中,并根据需要使用登录/注销。

登录看起来像这样

模态登录3

有不同的风格可供选择,您可以设计自己的风格。

说明可以在这里找到

13. 防止关闭的话题变灰

默认情况下,关闭回复的话题是 「灰色的」 。但通常您只是想停止发帖,最常见的是针对论坛顶部的热门话题。

将此代码添加到主题中的 css 或修改主题 css 文件夹中的 bbpress.css 。

#bbpress-forums .status-closed,
#bbpress-forums .status-closed a {
  color: #aaa !important;
}

14. 添加新的 bbpress 角色

您可以添加新的 bbpress 角色,甚至更改其功能。

在最简单的级别上,您可以添加新角色名称并为其赋予现有能力

这仅对您自己的管理目的很有用,但如果像我一样讨厌 「keymaster」 角色名称,只需创建一个新角色并为其赋予 keymaster 功能。

以下代码创建了三个名为 「name 1」 、 「name 2」 和 「name 3」 的新角色,第一个角色有参与者、第二个主持人和第三个关键管理员。

只需修改代码即可创建具有您需要的任何功能的任意数量的角色

function add_custom_role( $bbp_roles ) {


$bbp_roles['my_custom_role1'] = array(
'name' => 'name 1',
'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
);
$bbp_roles['my_custom_role2'] = array(
'name' => 'name 2',
'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
);
$bbp_roles['my_custom_role3'] = array(
'name' => 'name 3',
'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() ) // the same capabilities as keymaster
);
return $bbp_roles;
}
add_filter( 'bbp_get_dynamic_roles', 'add_custom_role', 1 );

15. 在 bbpress 角色中添加自定义功能

添加自定义功能

16. 论坛页面添加说明

如果您创建一个带有描述的论坛,这只会显示在主索引页面上,而不显示在类别页面或论坛页面上

从:

说明1

到 :

说明2

将以下内容添加到您的函数文件中。

//filter to add description after forums titles on forum index
function rw_singleforum_description() {
  echo '<div class="bbp-forum-content">';
  echo bbp_forum_content();
  echo '</div>';
}
add_action( 'bbp_template_before_single_forum' , 'rw_singleforum_description');

我还添加了这个自定义 CSS 。将此添加到您的子主题的 style.css 文件中,以将其放置在面包屑下。

#bbpress-forums div.bbp-forum-content {
  clear: both !important;
  margin-left: 0 !important;
  padding: 0  !important;
}

17. 将订阅移至右侧

改变
订阅1

到:

订阅2

将以下内容放入您的 style.css

/*styling to move 'Subscribe' to right hand side */
.single-forum .subscription-toggle  {
    float:right !important ;
}

如果您只想将其稍微向右移动,则可以使用此自定义 CSS 。根据您的喜好调整 5px 。

/*styling to move 'Subscribe' to right hand side */
.single-forum .subscription-toggle  {
    padding-left: 5px !important ;
}

18. 防止用户更改他们的显示名称

用户可以在 bbpress 个人资料页面中更改他们的显示名称。一些论坛希望阻止这种情况以阻止用户隐藏他们的身份。

要关闭此功能,您需要编辑默认文件之一并将其保存到您的主题中。

在您的主题中创建一个 bbpress 文件夹。

wp-content/themes/%yourtheme%/bbpress

其中 %yourtheme% 是您的主题名称

然后导航到

wp-content/plugins/bbpress/templates/default/bbpress/form-user-edit.php

并将此文件复制到您在上面创建的 bbpress 文件夹中

bbpress 现在将使用这个而不是默认值。

编辑这个新文件并取出第 33 到 45 行

即取出以下内容

<div>
            <label for="nickname"><?php _e( 'Nickname', 'bbpress' ); ?></label>
            <input type="text" name="nickname" id="nickname" value="<?php bbp_displayed_user_field( 'nickname', 'edit' ); ?>" class="regular-text" tabindex="<?php bbp_tab_index(); ?>" />
        </div>
        <div>
            <label for="display_name"><?php _e( 'Display Name', 'bbpress' ) ?></label>
            <?php bbp_edit_user_display_name(); ?>
        </div>
        <?php do_action( 'bbp_user_edit_after_name' ); ?>

这将删除设置昵称和更改显示名称的功能。

19. 链接到一个随机的单个话题

这是一个非常漂亮的功能,允许用户通过单击链接浏览随机话题。

如果您启用了它,它会挂在主要主题之前,否则它还会创建一个名为 [ntwb-bbpress-random-single-topic] 的简码,允许您将其放入小工具或 WordPress 帖子中。

// bbPress - Random Single Topic
function ntwb_bbpress_random_single_topic() {
    if ( bbp_has_topics( array( 'orderby' => 'rand', 'posts_per_page' => 1 ) ) ) {


        while ( bbp_topics() ) : bbp_the_topic();


            ?>
            <a class="bbp-topic-permalink" href="<?php bbp_topic_permalink(); ?>">Random Topic</a>
            <?php


        endwhile;
    }
}
// Hook into action
add_action('bbp_template_before_lead_topic','ntwb_bbpress_random_single_topic');
// Add it as a shortcode [ntwb-bbpress-random-single-topic]
add_shortcode('ntwb-bbpress-random-single-topic', 'ntwb_bbpress_random_single_topic');

20. 重命名用户角色名称

此功能允许您重命名 bbPress 预定义的用户角色。

只需将 「我的自定义 *** 角色名称」 更改为您喜欢的内容即可。

add_filter( 'bbp_get_dynamic_roles', 'ntwb_bbpress_custom_role_names' );


function ntwb_bbpress_custom_role_names() {
    return array(


        // Keymaster
        bbp_get_keymaster_role() => array(
            'name'         => 'My Custom Keymaster Role Name',
            'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() )
        ),


        // Moderator
        bbp_get_moderator_role() => array(
            'name'         => 'My Custom Moderator Role Name',
            'capabilities' => bbp_get_caps_for_role( bbp_get_moderator_role() )
        ),


        // Participant
        bbp_get_participant_role() => array(
            'name'         => 'My Custom Participant Role Name',
            'capabilities' => bbp_get_caps_for_role( bbp_get_participant_role() )
        ),


        // Spectator
        bbp_get_spectator_role() => array(
            'name'         => 'My Custom Spectator Role Name',
            'capabilities' => bbp_get_caps_for_role( bbp_get_spectator_role() )
        ),


        // Blocked
        bbp_get_blocked_role() => array(
            'name'         => 'My Custom Blocked Role Name',
            'capabilities' => bbp_get_caps_for_role( bbp_get_blocked_role() )
        )
    );
}

21. 在论坛索引后显示 5 个最近的话题

此功能在您的论坛索引后显示最新的 5 个话题。

// Add Recent Topics to BBPress
function recent_bbpress_topics() { { ?>
 <?php
 if ( bbp_has_topics( array( 'author' => 0, 'show_stickies' => false, 'order' => 'DESC', 'post_parent' => 'any', 'posts_per_page' => 5 ) ) )
 bbp_get_template_part( 'bbpress/loop', 'topics' );
 ?>
<?php }}
// Hook into action
add_action('bbp_template_after_forums_index','recent_bbpress_topics');

22. 仅显示 1 个修订日志话题和回复

使用此功能清理您的修订日志。

// Only return one entry for revision log otherwise it gets cluttered
function bbp_trim_revision_log( $r='' ) {
 $arr = array( end( $r ));
 reset( $r );


 return( $arr );
}


add_filter( 'bbp_get_reply_revisions', 'bbp_trim_revision_log', 20, 1 );
add_filter( 'bbp_get_topic_revisions', 'bbp_trim_revision_log', 20, 1 );

23. 显示 bbPress 论坛搜索无处不在

在单个论坛和单个话题上方显示论坛搜索,但不在 bbPress 个人资料上。

//display bbPress search form above sinle topics and forums
function rk_bbp_search_form(){


    if ( bbp_allow_search()) {
        ?>
        <div class="bbp-search-form">


            <?php bbp_get_template_part( 'form', 'search' ); ?>


        </div>
        <?php
    }
}


add_action( 'bbp_template_before_single_forum', 'rk_bbp_search_form' );
add_action( 'bbp_template_before_single_topic', 'rk_bbp_search_form' );

24. 显示 bbPress 主题的状态标签

这些功能中的每一个都将帮助您根据当前主题的状态添加特定的标签或图标。

在每个话题旁边显示粘性标签。

function rk_sticky_topics() {


   if ( bbp_is_topic_sticky() && !bbp_is_topic_closed() )
      echo '<span class="sticky">[Sticky]</span>';
}


add_action( 'bbp_theme_before_topic_title', 'rk_sticky_topics' );

在每个话题旁边显示封闭标签。

function rk_closed_topics() {


   if ( bbp_is_topic_closed() && !bbp_is_topic_sticky() )
      echo '<span class="closed">[Closed]</span>';
}


add_action( 'bbp_theme_before_topic_title', 'rk_closed_topics' );

如果该话题的回复超过 25 条,则显示热标签。
(您可以将 25 更改为任何您想要的)

function rk_hot_topics() {
   $reply_count = bbp_get_topic_reply_count();


   if ( $reply_count > 25 )
      echo '<span class="hot">[Hot]</span>';
}


add_action( 'bbp_theme_before_topic_title', 'rk_hot_topics' );

如果某个话题已关闭且有粘性,则显示 「公告」 标签。

function rk_announcement_topics() {


   if ( bbp_is_topic_sticky() && bbp_is_topic_closed() )
      echo '<span class="announcement">[Announcement]</span>';
}


add_action( 'bbp_theme_before_topic_title', 'rk_announcement_topics' );

创建话题后一小时内显示 「新」 标签。
将偏移量更改为您想要的任何值,但请确保数量以秒为单位。
例如 60*60*1 = 3600 seconds ,这是 1 小时的秒数。

function rk_new_topics() {
$offset = 60*60*1;


   if ( get_post_time() > date('U') - $offset )
      echo '<span class="new">[New]</span>';
}


add_action( 'bbp_theme_before_topic_title', 'rk_new_topics' );

您可以通过在图标字体 HTML 中添加以显示图标来输入图标。

无论您在哪里看到这个

echo '<span class="example">[Example]</span>';

您可以轻松地将其更改为此。
在这里,我使用了 Dashicons 图标字体集中的随机字体。

echo '<span class="example"><span class="dashicons dashicons-admin-post"></span>[Example]</span>';

如果您要使用基本的 HTML 插入图标的方式,您可以使用这个小 CSS 片段来设置它们的样式。

#bbpress-forums li.bbp-topic-title span.dashicons {
  float: left;
  margin-right: 5px;
}

您也可以仅使用 CSS 在话题标题之前或之后插入自定义图标字体。

这是来自该站点的 Dashicons 示例。

此 CSS 代码在置顶帖之前的话题图标之前显示置顶图标。

ul.sticky 和 ​​ul.super-sticky 都是该话题的帖子类,您可以利用和自定义更多帖子类。

#bbpress-forums ul.sticky li.bbp-topic-title a.bbp-topic-permalink:before,
#bbpress-forums ul.super-sticky li.bbp-topic-title a.bbp-topic-permalink:before {
  font: normal 16px/1 'dashicons';
  content: '\f450';
  margin-right: 5px;
  float: left;
  padding-top: 3px;
  color: #bb0;
}

25. 限制用户访问 WordPress 后端

添加此 PHP 代码片段以仅允许只能编辑帖子的用户访问 WordPress 后端。

/**
 * Redirect back to homepage and not allow access to
 * WP backend for Subscribers.
 */
function rkk_redirect_admin(){
    if ( ! current_user_can( 'edit_posts' ) ){
        wp_redirect( site_url() );
        exit;
    }
}
add_action( 'admin_init', 'rkk_redirect_admin' );

您可能还想为参与者禁用 WordPress 工具栏。

/**
 * Disable toolbar on the frontend of your website
 * for subscribers.
 */
function rkk_disable_admin_bar() {
    if( ! current_user_can('edit_posts') )
        add_filter('show_admin_bar', '__return_false');
}
add_action( 'after_setup_theme', 'rkk_disable_admin_bar' );

您也可以安装 WP Admin No Show 来执行此类操作。

26. 创建一个 bbPress 特定的侧边栏

没有插件

注册您的新侧边栏

将此添加到您的子主题 functions.php 文件中

function rkk_widgets_init() {


    register_sidebar( array(
        'name' => __( 'bbPress Sidebar', 'rkk' ),
        'id' => 'bbp-sidebar',
        'description' => __( 'A sidebar that only appears on bbPress pages', 'rkk' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
    }


add_action( 'widgets_init', 'rkk_widgets_init' );

现在在您的子主题中编辑您的 sidebar.php 文件,并选择仅在 bbPress 页面上用 bbPress 侧边栏替换的侧边栏。

此示例使用的是直接来自话题 25 的代码。我们使用条件 is_bbPress 在 bbPress 页面上显示我们想要的侧边栏,并使用!is_bbpress 在 bbPress 页面上不显示我们想要替换的侧边栏。

<?php if ( is_active_sidebar( 'sidebar-1' ) && !is_bbpress() ) : ?>


        <div id="widget-area" class="widget-area" role="complementary">
        <?php dynamic_sidebar( 'sidebar-1' ); ?>
        </div><!-- .widget-area -->


<?php elseif ( is_active_sidebar( 'bbp-sidebar' ) && is_bbpress() ) : ?>


    <div id="secondary" class="widget-area" role="complementary">
    <?php dynamic_sidebar( 'bbp-sidebar' ); ?>
    </div><!-- .widget-area -->


<?php endif; ?>

现在转到仪表盘> 外观> 小工具,现在您将看到一个名为 「bbPress Sidebar」 的新侧边栏。

添加 bbPress 小工具后,它应该看起来像这样。

论坛1

带插件

使用以下任何插件都可以获得特定于 bbPress 的侧边栏。如果您认为您发现的内容不够好,我相信还有更多插件可以执行此类功能。

限制小工具

小工具逻辑

bbPress WP 调整

27. 登录后自定义重定向

要更改用户登录后重定向到的页面,有像 Peter's Login Redirect 这样的 WordPress 插件可以处理重定向,或者您可以使用和自定义此 php 代码片段来实现相同的功能。

下面的 PHP 代码片段会将具有 Admin 用户角色的用户重定向到 WordPress 后端,并将其他任何人重定向到您网站的首页。

/**
* WordPress function for redirecting users on login based on user role
*/
function my_login_redirect( $url, $request, $user ){
if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
if( $user->has_cap( 'administrator' ) ) {
$url = admin_url();
} else {
$url = home_url();
}
}
return $url;
}
add_filter('login_redirect', 'my_login_redirect', 10, 3 );

Step by step guide to setting up a bbPress forum – part 4

Step by step guide to setting up a bbPress forum – part 4

Step by step guide to setting up a bbPress forum – part 4
Codex Home → Step by step guide to setting up a bbPress forum – part 4
Using Actions
Actions and Filters add a powerful way that you can alter how bbPress (and of course WordPress) work.
For the newcomer to WordPress, they are also probably one of the hardest to find information on. This tutorial tries to demystify some of this area, and help newcomers code those changes that will make their site work how they wish.
This section deals with actions.
1. Actions
These are things you can get wordpress to do at a particular point in the code.  So if you want to add something to an area, then an action is probably what you want.  WordPress and bbPress are full of actions points throughout the code.
So lets look at what one looks like, so that you can recognise it

They start with do_action and then each one has a unique action name.
This example above occurs in the bbPress code before the author avatar on a topic or reply
So if you wanted bbPress to say the words 「Posted by」 before the author details are shown in a topic or reply, you』d create some code that would 「hook」 to this action, and that』s what the code will do.
2. Finding the code
So where is the code that we need to look at?
This is of course the first issue with bbPress (as with any other part of WordPress – finding out what file needs to be altered.
All the critical template files (the most likely you』ll want to alter) are held in the folder
Wp-content/plugins/bbpress/tempaltes/default/bbpress
Whilst many of the names give a good clue to what they do, you』ll probably need to delve into a few before you find the one you want.
I plan to make a list of these templates and what they do if/when I work it out!
For the meantime, just look through, and you should find.
3. Where to add actions
In this tutorial, we』ll look the code you need to create, but obviously that code needs to go somewhere.  So where do we put it?
We add actions either to the functions file, or create them as a plugin.
Both methods have uses.
If you like using plugins, then they』re a good way to add changes, and work well if you have a number of sites, as the plugin is easily uploaded to each.  But they do require a bit more knowledge to code.  But google the subject and you』ll find lots of help on creating one.  Then just put the code into it, and upload to your site. If you want others to use your clever code, then a plugin is a definite.
The alternate is to use a functions file.  As you are now at the 「advanced」 stage of using WordPress, you should either be developing your own themes, or using a Childtheme to store the specifics.
If you still just have a main theme from someone else (eg a wordpress default theme, a free theme or a premium theme), then you should create a childtheme.  This simply uses the main theme, and the you can add your tweaks to the childtheme.  Then if the maintheme is updated, you tweaks in the childtheme are not overwritten.  For help on creating a childtheme  google 「child theme wordpress video」 and you』ll find lots of demonstrations.
If someone has built a site for you, it may already be a in a child theme, and again the video』s will let you see whether this is the case.
Within our childtheme, we』ll put a functions.php file, and the actions we set up will go in there.
4. Example of code we can alter
So to start to understand what we need to do, lets have a look at one of bbPress』s templates and see where these are used.
The file we』ll look at is called 「loop-single-reply.php」 and the dafault is held at
wp-content/plugins/bbpress/templates/default/bbpress/loop-single-reply.php
Loop single reply is the template that  is used by bbpress to display a topic and the replies associated with it – eg this page

If we start to look at this file we』ll find at around line 39 the following

'
', 'show_role' => false ) ); ?>

This part of the code displays the author details.
Line 1 – So the first things we see is an action an action for 『bbp_theme_before_reply_author_details』
Line 2 – This is followed by the bbp_reply_author link code which displays the author avatar, name, and role.
Line 3 – tests if the viewer is keymaster
Line 4 – if the viewer is keymaster, you can add some admin things to do here.
Line 5 – if the viewer is keymaster,displays the IP address of the author
Line 6 – is another action we can hook to after it has displayed this
Line 7 – ends the 「if admin..」 statement
5. Adding a simple action
So as stated above, if we hook to 『bbp_theme_before_reply_author_details』, we can add text or data at this point
So lets add 「posted by」 to the start of the author display area.
To do this we』ll simply hook to this action.
In effect we create a bit of code with

a name
what we want to hook it to, and
what we want it to do

The name must be unique, as other plugins, themes or indeed you could have actions that should be carried out at this point.
So this is what I coded :
//this function hooks to BBpress loop-single-reply.php and adds "posted by" before the author details
function robin_w_posted_by ()
 {
echo 'Posted by' ;
 }
add_action ('bbp_theme_before_reply_author_details', 'robin_w_posted_by') ;

So let』s pick this apart
Line 1 – starting 『//』 is a comment line. You may know today what this function does, but in a few months time, you may not, so comments are good to remind you !
Line 2 – This sets up a function and calls it  『robin_w_posted_by』.  Functions within your website must be unique, so adding your name at the start will make it highly unlikely that any WordPress, bbPress or any plugins you have will already have this name.
Line 3 – The actual function code is then contained in the {  } so this opens the function code area.
Line 4 – the function code – in this case it is a simple 『echo』 command
Line 5 – closes the code area
So that creates the function, but now you must hook it to the do_action command in the bbPress code, that is tell WordPress where to implement it.
Line 6 – The final 『add_action』 command tells WordPress to add an action to 『bbp_theme_before_reply_author_details』 called 『robin_w_posted_by』 – the function we』ve just created.  And that』s it!
so

becomes

6. Adding an Action – further example
This example adds a post count to the author area, using the do_action( 『bbp_theme_after_reply_author_details』 ) action hook
//this function hooks to BBpress loop-single-reply.php and adds the post count to the reply display
function robin_w_display_count ()
{
$post_count = bbp_get_user_post_count( bbp_get_reply_author_id( $reply_id )) ;
echo "
Total posts : " ;
echo $post_count ;
echo "" ;
}
add_action ('bbp_theme_after_reply_author_details', 'robin_w_display_count') ;

Very similar to the first example, but here we set a variable called $post_count to the count and then use this to display the count.
The result:

bbPress 条件标签

bbPress 条件标签

参见 WordPress Codex https://codex.wordpress.org/Conditional_Tags

is_bbpress()

/** 档案 *****************************************************************/

bbp_is_forum_archive()

bbp_is_topic_archive()

/** 话题标签 **************************************************************/

bbp_is_topic_tag()

bbp_is_topic_tag_edit()

/** 组件 *************************************************************/

bbp_is_single_forum()

bbp_is_single_topic()

bbp_is_single_reply()

bbp_is_topic_edit()

bbp_is_topic_merge()

bbp_is_topic_split()

bbp_is_reply_edit()

bbp_is_reply_move()

bbp_is_view()

/** 用户 ***********************************************************/

bbp_is_single_user_edit()

bbp_is_single_user()

bbp_is_user_home()

bbp_is_user_home_edit()

bbp_is_topics_created()

bbp_is_replies_created()

bbp_is_favorites()

bbp_is_subscriptions()

/** 搜索 ***********************************************************/

bbp_is_search()

bbp_is_search_results()

bbp_get_user_profile_url

bbp_get_user_profile_url

bbp_get_user_profile_url
Codex Home → bbp_get_user_profile_url
Description
The 『bbp_get_user_profile_url』 function returns the URL of the supplied user』s profile.
How to use it

$user_id – The specified user to return the profile link for.
Sample code example

<a href="ID); ?>">Your Forum profile

Creates a link to the current user』s profile.

审核和黑名单

审核和黑名单

bPress 使用与 WordPress 用于评论的相同的审核和黑名单工具。如果您希望用户的话题和回复受到额外审核,和/或完全禁止发布,您可以使用评论审核和评论黑名单设置,位于:

管理后台 > 设置 > 讨论

当在您的评论审核设置中发现包含单词的话题或回复时,它将被置于待处理状态,而不是立即发布。

当发现某个话题或回复在您的评论黑名单设置中包含某个词时,它会被阻止发布,并且用户会收到一个相对通用的回复,例如:

目前无法创建您的话题。

响应保持通用的原因是,冒犯的用户不会被发现问题,然后他们可以尝试使用空格、符号或其他 Web voo-doo 来规避。

您目前无法修改这些响应的结果,尽管该 bbp_add_error() 功能的核心补丁可以在 bbPress 的未来版本中相对轻松地启用它。

bbp_reply_author_link

bbp_reply_author_link

bbp_reply_author_link
Codex Home → bbp_reply_author_link
Description
The 『bbp_reply_author_link』 function is used to display the current reply author』s user role, avatar and username linked to their profile
Where is it used
Templates: loop-single-reply.php loop-search-reply.php
How to use it
1
$args (optional) – Accepts an array of arguments to configure display ( post_id, link_title, type, size, sep, show_role )
Uses bbp_get_reply_author_link()
Sample code example
1
Displays the linked avatar and username for the current reply author
1 '
', 'show_role' => true ) ); ?>
Displays the current reply author and user role separated by an HTML
element
1 '
', 'show_role' => true, 'size' => 32 ) ); ?>
Displays the current reply author and user role separated by an HTML
element and sets their avatar to a size of 32px wide and 32px tall
1 '
', 'show_role' => false, 'type' => 'avatar' ) ); ?>
Displays only the current reply author user』s avatar without their display name or user role
1 '
', 'show_role' => false, 'type' => 'name' ) ); ?>
Displays only the current reply author user』s display name without their avatar or user role
Related
bbp_author_link()
bbp_topic_author_link()