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

CRM-19665 WordPress: set canonical URL when using basepage #107

Merged
merged 7 commits into from
Mar 8, 2017
35 changes: 33 additions & 2 deletions includes/civicrm.basepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public function register_hooks() {
return;
}

// In WP 4.6.0+, tell it URL params are part of canonical URL
add_filter( 'get_canonical_url', array($this, 'basepage_canonical_url' ), 999);

// Yoast SEO has separate way of establishing canonical URL
add_filter( 'wpseo_canonical', array($this, 'basepage_canonical_url' ), 999);

Copy link
Member

@kcristiano kcristiano Nov 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in the CRM-19665 I would want to add:
add_filter( 'aioseop_canonical_url', array($this, 'basepage_canonical_url' ), 999);
here,

// regardless of URL, load page template
add_filter( 'template_include', array( $this, 'basepage_template' ), 999 );

Expand Down Expand Up @@ -259,6 +265,33 @@ public function basepage_render() {

}

/**
* Provide the canonical URL for a page accessed through a basepage.
*
* WordPress will default to saying the canonical URL is the URL of the base
* page itself, but we need to indicate that in this case, the whole thing
* matters.
*
* @return string
* The complete URL to the page as it should be accessed.
*/
public function basepage_canonical_url($canonical) {
// It would be better to specify which params are okay to accept as the
// canonical URLs, but this will work for the time being.
if (empty($_GET['page'])
|| empty($_GET['q'])
|| $_GET['page'] != 'CiviCRM') {
return $canonical;
}
$path = $_GET['q'];
unset($_GET['q']);
unset($_GET['page']);
$query = http_build_query($_GET);

// We should, however, build the URL the way that CiviCRM expects it to be
// (rather than through some other funny base page).
return CRM_Utils_System::url($path, $query);
}

/**
* Get CiviCRM base page template.
Expand Down Expand Up @@ -328,5 +361,3 @@ public function basepage_template( $template ) {


} // class CiviCRM_For_WordPress_Basepage ends