Last active
June 11, 2021 05:56
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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