我的 PHP 代码需要一些帮助。 我在functions.php 文件中添加了一个PHP 函数,但它不起作用。我确实在我的帐户页面 WordPress 上创建了一个新的自定义选项卡。但我注意到自定义链接与 'my-account' url 绑定。我无法删除此链接
我想删除 http://site-test.local/my-account
从这个绝对链接“http://site-test.local/my-account/https:/mycustomwebsite.com/”
当我单击“新自定义选项卡”菜单链接时,我只想获取“https:/mycustomwebsite.com/”。
function custom_add_my_account_menu_item($items) {
// Add a new menu item with the key 'custom_option'
$items['https://mycustomwebsite.com'] = __('NEW CUSTOM TAB', 'your-text-domain');
$cleanLink = str_replace('http://site-test.local/my-account', '', $items); //I'm trying to remove the site-test.local/my-account, but it doesn't work
$items = $cleanLink;
return $items;
}
add_filter('woocommerce_account_menu_items', 'custom_add_my_account_menu_item');
// Display content for the custom option in the My Account section
function custom_my_account_endpoint_content() {
echo '<p>This is the content for the custom option.</p>';
}
add_action('woocommerce_account_custom_option_endpoint', 'custom_my_account_endpoint_content');
要设置指向自定义“我的帐户”菜单项的自定义链接,您可以使用以下命令:
// Add a custom menu item
add_filter('woocommerce_account_menu_items', 'add_my_account_custom_menu_item');
function add_my_account_custom_menu_item( $menu_items ) {
$menu_items['custom_link'] = __('Custom Link', 'woocommerce');
return $menu_items;
}
// Display the tab content for the 'custom_link' menu item (optional as with an external link it will not be displayed)
add_action('woocommerce_account_custom_option_endpoint', 'custom_my_account_endpoint_content');
function custom_my_account_endpoint_content() {
echo '<p>This is the content for the Custom Link.</p>';
}
// Replace the custom menu item Link
add_action('template_redirect', 'change_my_account_custom_menu_item_link', 10);
function change_my_account_custom_menu_item_link() {
if ( is_user_logged_in() && is_account_page() ) {
$menu_item_key = 'custom_link'; // HERE set the custom menu item key you are using
$custom_link = esc_url('https://mycustomwebsite.com/'); // HERE set your custom link
wc_enqueue_js("$('li.woocommerce-MyAccount-navigation-link--{$menu_item_key} a').attr('href','{$custom_link}');");
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。
如果您想在新窗口中打开链接,请将最后一个函数替换为:
// Replace the custom menu item Link
add_action('template_redirect', 'change_my_account_custom_menu_item_link', 10);
function change_my_account_custom_menu_item_link() {
if ( is_user_logged_in() && is_account_page() ) {
$menu_item_key = 'custom_link'; // HERE set the custom menu item key you are using
$custom_link = esc_url('https://mycustomwebsite.com/'); // HERE set your custom link
wc_enqueue_js("$('li.woocommerce-MyAccount-navigation-link--{$menu_item_key} a').attr('href','{$custom_link}').attr('target','_blank');");
}
}