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
190
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
220
Other Decks in Programming
See All in Programming
Datadog DBMでなにができる? JDDUG Meetup#7
nealle
0
160
はじめての Go * WASM * OCR
sgash708
1
130
バイセルでの AI を用いた開発の取り組み ~ Devin, Cursor の活用事例・知見共有 ~
umaidashi
0
110
kintone開発を効率化するためにチームで試した施策とその結果を大放出!
oguemon
0
390
CDKを使ったPagerDuty連携インフラのテンプレート化
shibuya_shogo
0
130
楽しく向き合う例外対応
okutsu
0
760
LINE messaging APIを使ってGoogleカレンダーと連携した予約ツールを作ってみた
takumakoike
0
140
Jasprが凄い話
hyshu
0
200
PHPカンファレンス名古屋2025 タスク分解の試行錯誤〜レビュー負荷を下げるために〜
soichi
1
770
もう少しテストを書きたいんじゃ〜 #phpstudy
o0h
PRO
21
4.4k
DRFを少しずつ オニオンアーキテクチャに寄せていく DjangoCongress JP 2025
nealle
2
300
Duke on CRaC with Jakarta EE
ivargrimstad
0
350
Featured
See All Featured
Six Lessons from altMBA
skipperchong
27
3.6k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
Designing on Purpose - Digital PM Summit 2013
jponch
117
7.1k
Into the Great Unknown - MozCon
thekraken
35
1.7k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
590
Scaling GitHub
holman
459
140k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
Done Done
chrislema
182
16k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
129
19k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
11
1.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
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