Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp_nav_menu suffix in Custom links to remote websites #7329

Closed
stouch opened this issue Nov 8, 2022 · 5 comments · Fixed by #7330
Closed

wp_nav_menu suffix in Custom links to remote websites #7329

stouch opened this issue Nov 8, 2022 · 5 comments · Fixed by #7330
Labels
Bug Something isn't working Changelogged Whether the issue/PR has been added to release notes.
Milestone

Comments

@stouch
Copy link

stouch commented Nov 8, 2022

Bug Description

This plugin adds a suffix /amp to my custom links to remote websites in my footer menu (wp_nav_menu).

My blog is under a subpath (/blog) so I was able to manually remove this suffix (and keep it only for my blog links) with :

		if(strstr($parsed_url['path'], 'blog')){
			$parsed_url['path'] .= user_trailingslashit( amp_get_slug(), 'amp' );
		}

in amp/src/PairedUrl.php.

Expected Behaviour

The suffix must no be added to custom links in wp menus.

Screenshots

No response

PHP Version

7.3

Plugin Version

latest

AMP plugin template mode

Transitional

WordPress Version

No response

Site Health

No response

Gutenberg Version

No response

OS(s) Affected

No response

Browser(s) Affected

No response

Device(s) Affected

No response

Acceptance Criteria

No response

Implementation Brief

No response

QA Testing Instructions

No response

Demo

No response

Changelog Entry

No response

@stouch stouch added the Bug Something isn't working label Nov 8, 2022
@westonruter
Copy link
Member

The plugin should only be adding the AMP endpoint to links on the same domain as the WP install. Are you saying it is adding the link to other parts of your site which are not part of the WordPress subdirectory install? What does home_url() return for you?

The logic for determining whether a given link should get the AMP endpoint is as follows:

/**
* Determine whether a URL is for the frontend.
*
* @param string $url URL.
* @return bool Whether it is a frontend URL.
*/
public function is_frontend_url( $url ) {
$parsed_url = wp_parse_url( $url );
if ( ! empty( $parsed_url['scheme'] ) && ! in_array( strtolower( $parsed_url['scheme'] ), [ 'http', 'https' ], true ) ) {
return false;
}
// Skip adding query var to links on other URLs.
if ( ! empty( $parsed_url['host'] ) && $this->home_host !== $parsed_url['host'] ) {
return false;
}
// Skip adding query var to PHP files (e.g. wp-login.php).
if ( ! empty( $parsed_url['path'] ) && preg_match( '/\.php$/', $parsed_url['path'] ) ) {
return false;
}
// Skip adding query var to feed URLs.
if ( ! empty( $parsed_url['path'] ) && preg_match( ':/feed/(\w+/)?$:', $parsed_url['path'] ) ) {
return false;
}
// Skip adding query var to the admin.
if ( ! empty( $parsed_url['path'] ) && false !== strpos( $parsed_url['path'], $this->admin_path ) ) {
return false;
}
// Skip adding query var to content links (e.g. images).
if ( ! empty( $parsed_url['path'] ) && false !== strpos( $parsed_url['path'], $this->content_path ) ) {
return false;
}
return true;
}

It seems it is not taking into account the path of the home_url and is only considering the host.

Nevertheless, there is a filter you should be able to use to prevent this from happening in the immediate term. Something like this should do it:

add_filter( 
  'amp_to_amp_linking_element_excluded', 
  static function( $excluded, $url ) {
    if ( ! str_contains( $url, '/blog' ) ) {
      $excluded = true;
    }
    return $excluded;
  }, 
  10, 
  2
);

@stouch
Copy link
Author

stouch commented Nov 8, 2022

Thanks.
Actually, We have a website under ourdomain.com/app and we have our blog under /blog. And we have some wp_nav_menu custom links that point to ourdomain.com/app/xxx etc.
I already had an issue with the fact that this plugin does not handle well the case when wp is hosted under a subdirectory :
#6399

Thank you for your add_filter solution (I did not tested it yet)

@westonruter westonruter added this to the v2.3.1 milestone Nov 8, 2022
@westonruter
Copy link
Member

@thelovekesh Would you update the logic in is_frontend_url() to verify that the provided $url has the same path prefix as the home_url()? We an add a home_path member variable where we are currently storing the home_host:

$this->home_host = wp_parse_url( home_url(), PHP_URL_HOST );

$parsed_home     = wp_parse_url( home_url() );
$this->home_host = $parsed_home['host'];
$this->home_path = $parsed_home['path'];

@westonruter
Copy link
Member

@stouch The fix has been merged, and there is a pre-release production build you can test: #7330 (comment)

@westonruter westonruter modified the milestones: v2.3.1, v2.4 Jan 26, 2023
@pavanpatil1
Copy link

pavanpatil1 commented Feb 6, 2023

QA Passed ✅

Cross-checked the issue, Fix is working as expected. Now, the suffix(/amp) for the custom link is not visible.

Update
Steps to test:

  1. On the multisite network, Create two sub-directory site named /app, /blogs and enable the AMP plugin on any one of these sites and enable the transitional mode.
  2. Now add the custom link from the non-amp site to the AMP site.
  3. Once done, validate the Custom link from the AMP site. The result should be as follows:

Before:
image

After Fix:
image

@westonruter westonruter added the Changelogged Whether the issue/PR has been added to release notes. label Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Changelogged Whether the issue/PR has been added to release notes.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants