bbp_setup_current_user 被錯誤調用

bbp_setup_current_user 被錯誤調用

當該調試通知被觸發 WP_DEBUG 常量定義為 true*不是*的 bbPress 其他試圖訪問一些關於當前用戶和插件之前 $wp->init() 和/或 init 動作已經取消 wp-settings.php 。

該通知是為了提醒您 bbPress 被強制進入可能導致其他(可能嚴重的)訪問控制問題的配置,WordPress 沒有鉤子可以讓 bbPress 正確規避或恢復到安全操作條件。

不幸的是,很難從 bbPress 中確切地確定是什麼其他插件導致了這種情況發生,因此 bbp_setup_current_user 出現在日誌中。

此外,沒有任何 wp_set_current_user 操作;該 wp_set_current_user() 功能是在 WordPress 2.0.3 中引入的,set_current_user 為了向後兼容,該操作仍然存在。

也可以看看:

  • #WP24169 關於定製器也做錯了
  • #WP23106 關於添加一個動作以允許插件可靠地連接和修復功能
  • 這個 WordPress 載入順序問題導致了許多 bbPress Trac 票證

上次修改 2021.12.26

bbp_setup_current_user

bbp_setup_current_user

該 bbp_setup_current_user() 函數與 WordPress 的 set_current_user 操作相關聯。在內部,它只調用 bbp_setup_current_user 動作,bbPress 使用該動作向希望掛鉤 bbPress 用戶功能的第三方插件宣布自己。

這有點黑客技巧,因為插件依賴項在 WordPress 中不存在,但它仍然是定位 bbPress 並專門為其創建插件的絕佳方式。

上次修改 2021.12.26

bbp_topic_author_link

bbp_topic_author_link

bbp_topic_author_link
Codex Home → bbp_topic_author_link
Description
The 『bbp_topic_author_link』 function is used to display the current topic author』s user role, avatar and username linked to their profile
Where is it used
Templates: content-single-topic-lead.php
How to use it

$args (optional) – Accepts an array of arguments to configure display ( post_id, link_title, type, size, sep, show_role )
Uses bbp_get_topic_author_link()
Sample code example

Displays the linked avatar and username for the current topic author

‘, ‘show_role’ => true ) ); ?>
Displays the current topic author and user role separated by an HTML
element

‘, ‘show_role’ => true, ‘size’ => 32 ) ); ?>
Displays the current topic author and user role separated by an HTML
element and sets their avatar to a size of 32px wide and 32px tall

‘, ‘show_role’ => false, ‘type’ => ‘avatar’ ) ); ?>
Displays only the current topic author user』s avatar without their display name or user role

‘, ‘show_role’ => false, ‘type’ => ‘name’ ) ); ?>
Displays only the current topic author user』s display name without their avatar or user role
Related
bbp_author_link()
bbp_reply_author_link()
bbp_show_lead_topic()

上次修改 2021.12.25

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()

上次修改 2021.12.25

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.

上次修改 2021.12.25

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()

上次修改 2021.12.26

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

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

您可以使用 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. 添加

&lt;div class="truebreadcrumbs"&gt;&lt;?php bbp_breadcrumb(); ?&gt;&lt;/div&gt;

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-&gt;user_nicename ;
        $profilelink = '&lt;li&gt;&lt;a href="/forums/users/' . $user . '/edit"&gt;Edit Profile&lt;/a&gt;&lt;/li&gt;';
        $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 .= '&lt;li&gt;&lt;a href="'. wp_logout_url() .'"&gt;Log Out&lt;/a&gt;&lt;/li&gt;';
 }
 elseif ( !is_user_logged_in() ) {
 $items .= '&lt;li&gt;&lt;a href="'. site_url('wp-login.php') .'"&gt;Log In&lt;/a&gt;&lt;/li&gt;';
 $items .= '&lt;li&gt;&lt;a href="'. site_url('wp-login.php?action=register') .'"&gt;Register&lt;/a&gt;&lt;/li&gt;';
 $items .= '&lt;li&gt;&lt;a href="'. site_url('wp-login.php?action=lostpassword') .'"&gt;Lost Password&lt;/a&gt;&lt;/li&gt;';
 }
 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' =&gt; 'name 1',
'capabilities' =&gt; bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
);
$bbp_roles['my_custom_role2'] = array(
'name' =&gt; 'name 2',
'capabilities' =&gt; bbp_get_caps_for_role( bbp_get_participant_role() ) // the same capabilities as participants
);
$bbp_roles['my_custom_role3'] = array(
'name' =&gt; 'name 3',
'capabilities' =&gt; 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 '&lt;div class="bbp-forum-content"&gt;';
  echo bbp_forum_content();
  echo '&lt;/div&gt;';
}
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 行

即取出以下內容

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

這將刪除設置昵稱和更改顯示名稱的功能。

19. 鏈接到一個隨機的單個話題

這是一個非常漂亮的功能,允許用戶通過單擊鏈接瀏覽隨機話題。

如果您啟用了它,它會掛在主要主題之前,否則它還會創建一個名為 [ntwb-bbpress-random-single-topic] 的簡碼,允許您將其放入小工具或 WordPress 帖子中。

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


        while ( bbp_topics() ) : bbp_the_topic();


            ?&gt;
            &lt;a class="bbp-topic-permalink" href="&lt;?php bbp_topic_permalink(); ?&gt;"&gt;Random Topic&lt;/a&gt;
            &lt;?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() =&gt; array(
            'name'         =&gt; 'My Custom Keymaster Role Name',
            'capabilities' =&gt; bbp_get_caps_for_role( bbp_get_keymaster_role() )
        ),


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


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


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


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

21. 在論壇索引後顯示 5 個最近的話題

此功能在您的論壇索引後顯示最新的 5 個話題。

// Add Recent Topics to BBPress
function recent_bbpress_topics() { { ?&gt;
 &lt;?php
 if ( bbp_has_topics( array( 'author' =&gt; 0, 'show_stickies' =&gt; false, 'order' =&gt; 'DESC', 'post_parent' =&gt; 'any', 'posts_per_page' =&gt; 5 ) ) )
 bbp_get_template_part( 'bbpress/loop', 'topics' );
 ?&gt;
&lt;?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()) {
        ?&gt;
        &lt;div class="bbp-search-form"&gt;


            &lt;?php bbp_get_template_part( 'form', 'search' ); ?&gt;


        &lt;/div&gt;
        &lt;?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() &amp;&amp; !bbp_is_topic_closed() )
      echo '&lt;span class="sticky"&gt;[Sticky]&lt;/span&gt;';
}


add_action( 'bbp_theme_before_topic_title', 'rk_sticky_topics' );

在每個話題旁邊顯示封閉標籤。

function rk_closed_topics() {


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


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 &gt; 25 )
      echo '&lt;span class="hot"&gt;[Hot]&lt;/span&gt;';
}


add_action( 'bbp_theme_before_topic_title', 'rk_hot_topics' );

如果某個話題已關閉且有粘性,則顯示 「公告」 標籤。

function rk_announcement_topics() {


   if ( bbp_is_topic_sticky() &amp;&amp; bbp_is_topic_closed() )
      echo '&lt;span class="announcement"&gt;[Announcement]&lt;/span&gt;';
}


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() &gt; date('U') - $offset )
      echo '&lt;span class="new"&gt;[New]&lt;/span&gt;';
}


add_action( 'bbp_theme_before_topic_title', 'rk_new_topics' );

您可以通過在圖標字體 HTML 中添加以顯示圖標來輸入圖標。

無論您在哪裡看到這個

echo '&lt;span class="example"&gt;[Example]&lt;/span&gt;';

您可以輕鬆地將其更改為此。
在這裡,我使用了 Dashicons 圖標字體集中的隨機字體。

echo '&lt;span class="example"&gt;&lt;span class="dashicons dashicons-admin-post"&gt;&lt;/span&gt;[Example]&lt;/span&gt;';

如果您要使用基本的 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' =&gt; __( 'bbPress Sidebar', 'rkk' ),
        'id' =&gt; 'bbp-sidebar',
        'description' =&gt; __( 'A sidebar that only appears on bbPress pages', 'rkk' ),
        'before_widget' =&gt; '&lt;aside id="%1$s" class="widget %2$s"&gt;',
        'after_widget' =&gt; '&lt;/aside&gt;',
        'before_title' =&gt; '&lt;h3 class="widget-title"&gt;',
        'after_title' =&gt; '&lt;/h3&gt;',
    ) );
    }


add_action( 'widgets_init', 'rkk_widgets_init' );

現在在您的子主題中編輯您的 sidebar.php 文件,並選擇僅在 bbPress 頁面上用 bbPress 側邊欄替換的側邊欄。

此示例使用的是直接來自話題 25 的代碼。我們使用條件 is_bbPress 在 bbPress 頁面上顯示我們想要的側邊欄,並使用!is_bbpress 在 bbPress 頁面上不顯示我們想要替換的側邊欄。

&lt;?php if ( is_active_sidebar( 'sidebar-1' ) &amp;&amp; !is_bbpress() ) : ?&gt;


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


&lt;?php elseif ( is_active_sidebar( 'bbp-sidebar' ) &amp;&amp; is_bbpress() ) : ?&gt;


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


&lt;?php endif; ?&gt;

現在轉到儀錶盤> 外觀> 小工具,現在您將看到一個名為 「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 &amp;&amp; is_object( $user ) &amp;&amp; is_a( $user, 'WP_User' ) ) {
if( $user-&gt;has_cap( 'administrator' ) ) {
$url = admin_url();
} else {
$url = home_url();
}
}
return $url;
}
add_filter('login_redirect', 'my_login_redirect', 10, 3 );

上次修改 2021.12.26

啟用可視化編輯器

啟用可視化編輯器

在 bbPress 2.3.1 中,WordPress 的可視化編輯器默認為關閉。要重新打開它,請在插件或主題的 functions.php 文件中嘗試以下代碼片段:

function bbp_enable_visual_editor( $args = array() ) {
    $args['tinymce'] = true;
    return $args;
}
add_filter( 'bbp_after_get_the_content_parse_args', 'bbp_enable_visual_editor' );

您還可以通過在函數中添加更多參數來自定義可視化編輯器。

使用下面的此功能僅顯示可視化編輯器,而根本不顯示 html 編輯器。

function bbp_enable_visual_editor( $args = array() ) {
    $args['tinymce'] = true;
    $args['quicktags'] = false;
    return $args;
}
add_filter( 'bbp_after_get_the_content_parse_args', 'bbp_enable_visual_editor' );

如果您使用像 TinyMCE Advanced 這樣的插件,您還可以顯示其他媒體按鈕,如表情按鈕以插入表情。

使用下面的這個函數代替添加新的參數,以便能夠添加新的媒體,如表情按鈕。

function bbp_enable_visual_editor( $args = array() ) {
    $args['tinymce'] = true;
    $args['teeny'] = false;
    return $args;
}
add_filter( 'bbp_after_get_the_content_parse_args', 'bbp_enable_visual_editor' );

請注意,如果您在可視化編輯器中禁用 teeny 模式並通過 TinyMCE Advanced 插件允許其他媒體按鈕,您可能需要添加到此功能並將其放入您的子主題 functions.php 文件中,以允許您的用戶使用一些按鈕,如表格按鈕。

在某些情況下,粘貼到可視化編輯器中的文本會帶來不需要的樣式和 HTML 標記。您可以使用另一個函數來強制清除粘貼的文本。這將刪除諸如雜散 HTML 之類的內容,但保留粗體和斜體等基礎知識。

function bbp_tinymce_paste_plain_text( $plugins = array() ) {
    $plugins[] = 'paste';
    return $plugins;
}
add_filter( 'bbp_get_tiny_mce_plugins', 'bbp_tinymce_paste_plain_text' );

 

上次修改 2021.12.26