Created
August 18, 2017 20:40
-
-
Save wpmudev-sls/32e0d53a4a7be7762ae36281cd3171f7 to your computer and use it in GitHub Desktop.
[WordPress] - Replace gettext on CPT pages
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 | |
/* | |
Plugin Name: Replace gettext on CPT pages | |
Plugin URI: https://premium.wpmudev.org/ | |
Description: Replace gettext on CPT pages | |
Author: Panos Lyrakis @ WPMUDEV | |
Author URI: https://premium.wpmudev.org/ | |
License: GPLv2 or later | |
*/ | |
//TAGS: WordPress, gettext | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Replace_Gettext_On_CPT' ) ) { | |
class WPMUDEV_Replace_Gettext_On_CPT { | |
private static $_instance = null; | |
public static $cpt = 'YOUR CPT'; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Replace_Gettext_On_CPT(); | |
} | |
return self::$_instance; | |
} | |
public function __construct() { | |
add_filter('gettext', array($this, 'replace_gettext'), 20, 3); | |
add_filter('gettext_with_context', array($this, 'replace_gettext_with_context'), 20, 4); | |
} | |
public function replacements(){ | |
$replacments = array( | |
'default' => array( | |
'Categories' => 'Replacement for Categories', | |
'Post Comment' => 'Replacement for Post Comment', | |
'Leave a Reply' => 'Replacement for Leave a Reply' | |
), | |
'twentyseventeen' => array( | |
'Next Post' => 'Replacement for Next Post', | |
'Next' => 'Replacement for Next', | |
'Previous' => 'Replacement for Previous' | |
) | |
); | |
return apply_filters( 'WPMUDEV_Replace_Gettext_On_CPT/replacements', $replacments ); | |
} | |
public function replace_gettext( $transtext, $normtext, $domain ){ | |
global $post; | |
if( ! $post instanceof WP_Post || $post->post_type != self::$cpt ){ | |
return $transtext; | |
} | |
$replacements = $this->replacements(); | |
$replacement = isset( $replacements[ $domain ][ $normtext ] ) ? $replacements[ $domain ][ $normtext ] : $transtext; | |
return $replacement; | |
} | |
public function replace_gettext_with_context( $translations, $text, $context, $domain ){ | |
return $this->replace_gettext( $translations, $text, $domain ); | |
} | |
} | |
add_action( 'plugins_loaded', function(){ | |
$GLOBALS['WPMUDEV_Replace_Gettext_On_CPT'] = WPMUDEV_Replace_Gettext_On_CPT::get_instance(); | |
}, 9999 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment