修复WooCommerce自定义端点无法显示YITH心愿单内容问题
Let's break down the issues with your code and fix them step by step:
1. Incorrect Action Hook Name
The biggest blocker here is the wrong hook used to render the endpoint content. WooCommerce follows a strict hook format for custom account endpoints: woocommerce_account_{your-endpoint-slug}_endpoint.
Since your endpoint is yith-my-wishlist, the correct hook should be woocommerce_account_yith-my-wishlist_endpoint — not the generic woocommerce_account_special-page_endpoint you used before.
2. Fix the File Path (For Your Copied Template Setup)
Relative file paths in child themes can be unreliable, especially when WordPress is loaded from different contexts. Use WordPress's built-in get_stylesheet_directory() function to get the absolute path to your child theme folder, ensuring the server always finds your copied template.
Update your content rendering function like this:
function my_custom_endpoint_content() { // Get absolute path to the wishlist template in your child theme $wishlist_template = get_stylesheet_directory() . '/yith-woocommerce-wishlist/wishlist-view.php'; // Add a safety check to avoid fatal errors if the file is missing if ( file_exists( $wishlist_template ) ) { include $wishlist_template; } else { echo '<p>' . __( 'Wishlist content could not be loaded.', 'woocommerce' ) . '</p>'; } } // Hook to the correct endpoint action add_action( 'woocommerce_account_yith-my-wishlist_endpoint', 'my_custom_endpoint_content' );
3. Better Alternative: Skip Copying Plugin Files (Recommended)
Copying the entire YITH plugin folder to your child theme creates maintenance headaches — you’ll miss template updates whenever the plugin is updated. Instead, use YITH’s built-in template function to load the official plugin template directly:
function my_custom_endpoint_content() { // First confirm YITH Wishlist is active if ( function_exists( 'YITH_WCWL' ) ) { // Load the original wishlist template from the plugin yith_wcwl_get_template( 'wishlist-view.php' ); } else { echo '<p>' . __( 'YITH WooCommerce Wishlist plugin is not active.', 'woocommerce' ) . '</p>'; } } add_action( 'woocommerce_account_yith-my-wishlist_endpoint', 'my_custom_endpoint_content' );
4. Final Checks to Ensure Everything Works
- Flush Rewrite Rules: Even with your flush function, manually trigger this by going to Settings → Permalinks in your dashboard and clicking "Save Changes" (no edits needed). This registers your new endpoint properly.
- Verify Menu Order: Double-check your
my_custom_my_account_menu_items()function to ensureyith-my-wishlistis uncommented and placed where you want it in the menu.
After applying these fixes, your wishlist content should load correctly in the My Account section.
内容的提问来源于stack exchange,提问作者lisarko8077

