Skip to content

Instantly share code, notes, and snippets.

@maugelves
Last active June 11, 2021 05:56
Show Gist options
  • Save maugelves/d09e8c009a2034009983ece6ff70fb33 to your computer and use it in GitHub Desktop.
Save maugelves/d09e8c009a2034009983ece6ff70fb33 to your computer and use it in GitHub Desktop.
This functions returns a WordPress page permalink for the current language by its slug.
<?php
/**
* This function returns a page permalink
* for the current website language.
*
* @author Mauricio Gelves <mg@maugelves.com>
* @param $page_slug string WordPress page slug
* @return string|false Page Permalink or false if the page is not found
*/
function pll_get_page_url( $page_slug ) {
// Check parameter
if( empty( $page_slug ) ) return false;
// Get the page
$page = get_page_by_path( $page_slug );
// Check if the page exists
if( empty( $page ) || is_null( $page ) ) return false;
// Get the URL
$page_ID_current_lang = pll_get_post( $page->ID );
// Return the current language permalink
return empty($page_ID_current_lang) ? get_permalink( $page->ID ) : get_permalink( $page_ID_current_lang );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment