-
-
Save 5ally/1c6a8f564f93de29e54c8d3b61c7727e to your computer and use it in GitHub Desktop.
See https://wordpress.stackexchange.com/q/341950 for details.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Custom Endpoint | |
* Plugin URI: https://wordpress.stackexchange.com/q/341950 | |
* Description: Sample plugin which adds a custom WooCommerce (v3.6.4) endpoint named "Company Profile". =) | |
* Version: 1.0 | |
* Author: Sally CJ | |
* Author URI: https://wordpress.stackexchange.com/users/137402/sally-cj | |
* Text Domain: my-plugin | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
// Add the company-profile endpoint. | |
add_filter( 'woocommerce_get_query_vars', function( $query_vars ){ | |
$query_vars[] = 'company-profile'; | |
return $query_vars; | |
} ); | |
// Filter/change the endpoint title. | |
add_filter( 'the_title', function( $title ){ | |
if ( in_the_loop() ) { | |
global $wp; | |
if ( is_main_query() && is_page() && | |
isset( $wp->query_vars[ 'company-profile' ] ) ) { | |
$title = 'Company Profile'; | |
} | |
} | |
return $title; | |
}, 11 ); | |
// Add "Company Profile" to the "My Account" menu. | |
add_filter( 'woocommerce_account_menu_items', function( $items ){ | |
$items['company-profile'] = 'Company Profile'; | |
return $items; | |
} ); | |
// Render the content for the "My Account" -> "Company Profile" section. | |
add_action( 'woocommerce_account_company-profile_endpoint', function(){ | |
$user_id = get_current_user_id(); | |
?> | |
<form method="post" action=""> | |
<h3>Edit company profile</h3> | |
<div class="company-profile-fields"> | |
<div> | |
<?php woocommerce_form_field( 'company_id', array( | |
'type' => 'text', | |
'label' => 'Company ID', | |
), get_user_meta( $user_id, '_company_id', true ) ); ?> | |
</div> | |
<p> | |
<button type="submit" class="button">Save company profile</button> | |
<?php wp_nonce_field( 'edit_company_profile', 'edit-company-profile-nonce' ); ?> | |
<input type="hidden" name="action" value="edit_company_profile" /> | |
</p> | |
</div> | |
</form> | |
<?php | |
} ); | |
// Save the company profile data. | |
add_action( 'template_redirect', function(){ | |
if ( empty( $_POST['edit-company-profile-nonce'] ) ) { | |
return; | |
} | |
$nonce_value = wc_get_var( $_POST['edit-company-profile-nonce'] ); | |
if ( ! wp_verify_nonce( $nonce_value, 'edit_company_profile' ) ) { | |
return; | |
} | |
if ( empty( $_POST['action'] ) || 'edit_company_profile' !== $_POST['action'] ) { | |
return; | |
} | |
wc_nocache_headers(); | |
$user_id = get_current_user_id(); | |
$company_id = wc_clean( wp_unslash( $_POST[ 'company_id' ] ) ); | |
update_user_meta( $user_id, '_company_id', $company_id ); | |
wc_add_notice( 'Company profile data saved successfully.' ); // Just remove this if not needed.. | |
// These redirects both worked for me - no "headers already sent" error. | |
// wp_safe_redirect( wc_get_endpoint_url( 'company-profile', '', wc_get_page_permalink( 'myaccount' ) ) ); | |
wp_safe_redirect( wc_get_page_permalink( 'myaccount' ) ); // Go back to the My Account page. | |
exit; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment