woocommerce my account php code wordpress
Here's an example of PHP code that you can use in WordPress/WooCommerce to customize the My Account page:
// Add a new endpoint to My Account page
function custom_my_account_endpoint() {
add_rewrite_endpoint( 'custom-endpoint', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'custom_my_account_endpoint' );
// Add content to the new endpoint
function custom_my_account_endpoint_content() {
echo '<p>This is the content for the custom endpoint.</p>';
}
add_action( 'woocommerce_account_custom-endpoint_endpoint', 'custom_my_account_endpoint_content' );
Explanation:
add_rewrite_endpoint()
- Registers a new URL endpoint to the WordPress rewrite rules.EP_ROOT | EP_PAGES
- Specifies where the endpoint should be added.EP_ROOT
adds it to the root of the URL, andEP_PAGES
adds it to WordPress pages.add_action( 'init', 'custom_my_account_endpoint' );
- Hooks thecustom_my_account_endpoint()
function to theinit
action, ensuring the endpoint is added during WordPress initialization.function custom_my_account_endpoint_content()
- Defines a function to display content for the custom endpoint.echo '<p>This is the content for the custom endpoint.</p>';
- Outputs the content for the custom endpoint.add_action( 'woocommerce_account_custom-endpoint_endpoint', 'custom_my_account_endpoint_content' );
- Hooks thecustom_my_account_endpoint_content()
function to thewoocommerce_account_custom-endpoint_endpoint
action, displaying the content on the My Account page at the specified endpoint.
Remember to replace 'custom-endpoint'
and 'custom_my_account_endpoint_content'
with your desired endpoint slug and function names, respectively.