我在网上搜索了很多,但还没有找到答案。 所以我在这里依赖专家们。
我想禁用一些WooCommerce的端点。网上告诉我可以通过woocommerce_account_menu_items钩子取消设置WooCommerce菜单项,如下所示:
add_filter ( 'woocommerce_account_menu_items', 'my_remove_my_account_links' );
function my_remove_my_account_links( $menu_links ){
/**
* Uncomment the appropriate lines to remove specific
* endpoints in the WooCommerce My Account screen.
*/
//unset( $menu_links['dashboard'] ); // Remove Dashboard
//unset( $menu_links['edit-address'] ); // Addresses
//unset( $menu_links['payment-methods'] ); // Remove Payment Methods
//unset( $menu_links['orders'] ); // Remove Orders
//unset( $menu_links['downloads'] ); // Disable Downloads
//unset( $menu_links['edit-account'] ); // Remove Account details tab
//unset( $menu_links['customer-logout'] ); // Remove Logout link
return $menu_links;
}
但是这里的一个大问题是,这只是在前端删除了菜单链接。
我仍然可以通过直接URL访问取消设置的端点。所以当我输入https://example.de/myaccount/[unset-endpoint]时,我仍然可以访问内容。
我找到了一种通过直接URL访问重定向的方法。我使用了位于支付方式模板(/woocommerce/templates/myaccount/payment-methods.php)中的钩子woocommerce_before_account_payment_methods来重定向回仪表板:
function redirect_forbidden_access_account_endpoints(){
wp_redirect(wc_get_account_endpoint_url('dashboard'));
}
add_action('woocommerce_before_account_payment_methods', 'redirect_forbidden_access_account_endpoints');
这个方法非常好用,但只适用于payment-methods端点。我尝试过对原生的downloads端点和自定义端点进行同样的操作,但没有成功。
所以我的问题是:有没有一种可靠的解决方案,将从特定禁用的WooCommerce端点的URL访问重定向到仪表板?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
答案是:是的,有!我的钩子写错了。我现在使用了wp钩子。这合法吗?
function redirect_forbidden_access(){ $current_endpoint = WC()->query->get_current_endpoint(); if($current_endpoint == "payment-methods" || $current_endpoint == "add-payment-method" || $current_endpoint == "edit-payment-method" || $current_endpoint == "[custom-endpoint]") { wp_redirect(wc_get_account_endpoint_url('dashboard')); } } add_action('wp', 'redirect_forbidden_access');这就是解决方法。