Last active
June 3, 2025 15:30
-
-
Save wpscholar/215695a02a495194d7f7f8da2f46d519 to your computer and use it in GitHub Desktop.
Integrates the Random Post on Refresh plugin with Polylang to restrict queries to the current language.
This file contains hidden or 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 | |
/** | |
* Plugin Name: Random Post on Refresh – Polylang Integration | |
* Plugin URI: https://gist.github.com/wpscholar/215695a02a495194d7f7f8da2f46d519 | |
* Description: Integrates the Random Post on Refresh plugin with Polylang to restrict queries to the current language if none is explicitly provided. | |
* Version: 1.0 | |
* Author: Micah Wood | |
* Author URI: https://wpscholar.com | |
* License: GPLv2 | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html | |
*/ | |
add_filter( 'random_post_on_refresh_query_args', function( $query_args ) { | |
// Skip if the lang attribute is already explicitly set | |
if ( ! empty( $query_args['lang'] ) ) { | |
return $query_args; | |
} | |
// Only apply if Polylang is active | |
if ( function_exists( 'pll_current_language' ) ) { | |
$current_lang = pll_current_language(); // Returns slug like 'en', 'fr', etc. | |
if ( $current_lang ) { | |
$query_args['lang'] = $current_lang; | |
} | |
} | |
return $query_args; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment