Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Extending BuddyPress with BP_Component Class
Search
Renato Alves
September 27, 2016
Programming
0
180
Extending BuddyPress with BP_Component Class
English version of my talk given at WordCamp Rio de Janeiro in 2016.
Renato Alves
September 27, 2016
Tweet
Share
More Decks by Renato Alves
See All by Renato Alves
Estendendo o BuddyPress com a classe BP_Component
espellcaste
0
130
Criando sua Rede Social com o BuddyPress
espellcaste
2
200
Other Decks in Programming
See All in Programming
サーバーゆる勉強会 DBMS の仕組み編
kj455
1
350
Package Traits
ikesyo
2
230
令和7年版 あなたが使ってよいフロントエンド機能とは
mugi_uno
12
6k
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
3
1k
SpringBoot3.4の構造化ログ #kanjava
irof
2
700
Оптимизируем производительность блока Казначейство
lamodatech
0
990
バックエンドのためのアプリ内課金入門 (サブスク編)
qnighy
6
1.1k
Spring gRPC について / About Spring gRPC
mackey0225
0
160
BEエンジニアがFEの業務をできるようになるまでにやったこと
yoshida_ryushin
0
250
CNCF Project の作者が考えている OSS の運営
utam0k
5
610
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
980
2024年のkintone API振り返りと2025年 / kintone API look back in 2024
tasshi
0
160
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.5k
Side Projects
sachag
452
42k
KATA
mclloyd
29
14k
Designing Experiences People Love
moore
139
23k
Fireside Chat
paigeccino
34
3.2k
Music & Morning Musume
bryan
46
6.3k
Designing for humans not robots
tammielis
250
25k
Docker and Python
trallard
43
3.2k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Site-Speed That Sticks
csswizardry
3
300
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
Transcript
Extending BuddyPress with BP_Component Class WordCamp Rio | September 2016
Hi Rio! I'm Renato Alves Recifense/Pernambucano/Nordestino WordPress/BuddyPress/Freelancer Developer Full-stack developer
at Inglês na Rede Saucal WordPress Engineer bbPress and BuddyPress contributor
Our task: Integrate WooCommerce account page inside the BuddyPress user
profile. For this integration, we are gonna use BuddyPress BP_Component class.
BuddyPress?! What is it? :/ BuddyPress is a powerful community
plugin for WordPress that takes your site beyond the blog. With it, you'll be able to create any social network.
Use Cases Some real world examples of sites using BuddyPress
Inglês na Rede Users: +210 - https://inglesnarede.com.br
Tasty Kitchen Users: +230.000 - http://tastykitchen.com/
Tributário Users: +72.000 - https://tributario.com.br
Why integrate both plugins? Examples of WooCommerce and BuddyPress integrations
Sites with subscriptions and products; Sites where BP components are the focus; Avoid confusing the user.
I introduce BP_Component class
BP_Component Class What is it? Why does it exist? How
is it being used inside BuddyPress? How other plugins are using it? Menus; Component files; Cache groups; Post types/taxonomies registration; Database tables; Metadata tables; Permalinks; Rewrite rules.
BuddyPress Follow Downloads: +43.000
Tributário Users: +72.000
Integration Main Points Pages, endpoints and permalinks; Content; Redirection.
class IRB_WC_Account_Component extends BP_Component { public function __construct() { parent::start(
'bp_wc_account', __( 'Apoio', 'irb' ) ); $this->setup_hooks(); } public function setup_hooks() { add_filter( 'woocommerce_get_endpoint_url', array( $this, 'get_url' ), 10, 4 ); add_filter( 'woocommerce_get_myaccount_page_permalink', array( $this, 'account_url' ), 10, 1 ); add_filter( 'woocommerce_is_account_page', array( $this, 'is_account_page' ), 10, 1 ); add_filter( 'woocommerce_locate_template', array( $this, 'disable_account_nav' ), 10, 2 ); } public function disable_account_nav( $template = '', $template_name = '' ) { if ( 'myaccount/navigation.php' === $template_name && bp_is_current_component( 'minha-conta' ) ) { $template = plugin_dir_path( __FILE__ ) . 'dummy-nav.php'; } return $template; } // Continue.... # Plugin Beginning
public function setup_globals( $args = array() ) { $bp =
buddypress(); $bp->active_components[$this->id] = '1'; // Define slug, if needed if ( ! defined( 'BP_WC_ACCOUNT_SLUG' ) ) { define( 'BP_WC_ACCOUNT_SLUG', 'minha-conta' ); } $args = array( 'slug' => BP_WC_ACCOUNT_SLUG, 'has_directory' => false, ); parent::setup_globals( $args ); $this->irb_wc_link = ''; if ( is_user_logged_in() ) { $this->irb_wc_link = trailingslashit( bp_loggedin_user_domain() . $this->slug ); } } # Plugin globals
public function setup_nav( $main_nav = array(), $sub_nav = array() )
{ ... more code above foreach ( wc_get_account_menu_items() as $key_item => $item ) { // if ( 'dashboard' === $key_item || 'customer-logout' === $key_item ) { // continue; // } // Don't add edit-acount page // if ( bp_is_active( 'settings' ) && 'edit-account' === $key_item ) { // continue; // } ... more code below } # Menus
// WooCommerce uses it to redirect user after a form
entry is submited. public function account_url( $url = '' ) { global $wp; if ( ! bp_is_current_component( 'minha-conta' ) && ! isset( $wp->query_vars['customer-logout'] ) && empty( $this->was_redirected ) ) { return $url; } if ( isset( $wp->query_vars['customer-logout'] ) ) { return bp_get_root_domain(); } else { return trailingslashit( bp_loggedin_user_domain() . $this->slug ); } } # Filter URL
public function set_screen() { if ( bp_is_current_action( 'orders' ) &&
'view-order' === bp_action_variable( 0 ) && is_numeric( bp_action_variable( 1 ) ) ) { // WooCommerce usa a variável global $wp global $wp; unset( $wp->query_vars['orders'] ); $wp->query_vars['view-order'] = (int) bp_action_variable( 1 ); } remove_filter( 'the_title', 'wc_page_endpoint_title' ); add_action( 'bp_template_content', array( $this, 'wc_content' ) ); bp_core_load_template( 'members/single/plugins' ); } # Telling BP which screen to use
public function wc_content() { echo apply_filters( 'the_content', get_post_field( 'post_content', wc_get_page_id(
'myaccount' ) ) ); } # Content pre-WooCommerce 2.6
public function wc_content() { global $wp; $vars = $wp->query_vars['name']; if
( $vars == 'billing' ) { return woocommerce_account_edit_address( $vars ); } else if ( $vars == 'view-order' ) { return woocommerce_account_view_order( $wp->query_vars['page'] ); } else if ( $vars == 'minha-conta' ) { return woocommerce_account_content(); } else { do_action( 'woocommerce_account_' . $vars . '_endpoint'); } } # Content pos-WooCommerce 2.6
public function get_url( $url, $endpoint, $value, $permalink ) { if
( ! bp_is_current_component( 'minha-conta' ) ) { return $url; } $my_account_slugs = array_merge( wc_get_account_menu_items(), array( 'view-order' => true ) ); if ( isset( $my_account_slugs[ $endpoint ] ) ) { $slug = $endpoint; if ( 'view-order' === $endpoint ) { $slug = 'orders/' . $slug; } $url = trailingslashit( bp_loggedin_user_domain() . $this->slug ) . $slug; if ( ! empty( $value ) ) { $url = trailingslashit( $url ) . $value; } } return $url; } # Adjusting URLS
function irb_wc_account_component() { buddypress()->irb_wc_account = new IRB_WC_Acount_Component; } add_action( 'irb_wc_account_ready',
'irb_wc_account_component', 20 ); # Creating a class instance # Loading the component private function setup_actions() { if ( class_exists('WooCommerce') ) { require( $this->plugin_dir . 'woo/bp-wp-component.php' ); } # Adding the component do_action( 'irb_wc_account_ready' ); }
# Orders
# View Order
# My Addresses
# Edit Address
# Account Details
The end! Now go create your social network! =D
[email protected]
ralv.es // inglesnarede.com.br Questions?! Renato Alves