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
170
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
120
Criando sua Rede Social com o BuddyPress
espellcaste
2
200
Other Decks in Programming
See All in Programming
Macとオーディオ再生 2024/11/02
yusukeito
0
390
Ethereum_.pdf
nekomatu
0
470
Amazon Qを使ってIaCを触ろう!
maruto
0
420
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
140
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
130
CSC509 Lecture 13
javiergs
PRO
0
110
DevTools extensions で 独自の DevTool を開発する | FlutterKaigi 2024
kokiyoshida
0
150
初めてDefinitelyTypedにPRを出した話
syumai
0
440
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
6
1.7k
RubyLSPのマルチバイト文字対応
notfounds
0
120
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
130
デザインパターンで理解するLLMエージェントの作り方 / How to develop an LLM agent using agentic design patterns
rkaga
9
1.5k
Featured
See All Featured
Bash Introduction
62gerente
608
210k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
140
For a Future-Friendly Web
brad_frost
175
9.4k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Unsuck your backbone
ammeep
668
57k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Navigating Team Friction
lara
183
14k
Gamification - CAS2011
davidbonilla
80
5k
Typedesign – Prime Four
hannesfritz
40
2.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Rails Girls Zürich Keynote
gr2m
94
13k
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