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:

  1. add_rewrite_endpoint() - Registers a new URL endpoint to the WordPress rewrite rules.
  2. EP_ROOT | EP_PAGES - Specifies where the endpoint should be added. EP_ROOT adds it to the root of the URL, and EP_PAGES adds it to WordPress pages.
  3. add_action( 'init', 'custom_my_account_endpoint' ); - Hooks the custom_my_account_endpoint() function to the init action, ensuring the endpoint is added during WordPress initialization.

  4. function custom_my_account_endpoint_content() - Defines a function to display content for the custom endpoint.

  5. echo '<p>This is the content for the custom endpoint.</p>'; - Outputs the content for the custom endpoint.
  6. add_action( 'woocommerce_account_custom-endpoint_endpoint', 'custom_my_account_endpoint_content' ); - Hooks the custom_my_account_endpoint_content() function to the woocommerce_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.