0

i want to fetch all my wordpress plugin then get all gettext functions parameters

Example

 die(_e('Error')) ;
 die( '<b>'._e('Error').'</b>' ) ;
 _e('Error')

function for wordpress gettext i used

_e , __

and how to bypass similar functions

use_e , get_email , select__


I created this function by regex but it's not very useful

$list_words = array();

function po_fetch_code($dir , $preg = "/_\(('|\")(.*?)('|\")\)/"){
     if( is_dir($dir) ){
           $list = scandir($dir) ;
           foreach($list as $l){
              if($l == '.' || $l == '..'){
                // Do Nothing
              }else{
                po_fetch_code($dir.'/'.$l , $preg) ;
              }

           }
     }else{
          $ext = strrchr($dir , '.') ;
          //if($ext == '.php' || $ext == '.html' || $ext == 'html'){
               //$cont = file_get_contents($dir) ;
               $list = file($dir) ;
               global $list_words ;
               foreach($list as $key => $l){
                  preg_match_all( $preg , $l , $matches );
                  if(!empty($matches[2])){
                        foreach($matches[2] as $k=>$m){
                                 //$m = str_replace(array( "\'",'\\' , '"' , "'",'\'' ), '' ,$m) ;
                                 //$m = str_replace('\'s', 's' ,$m) ;
                                 if(!isset($list_words[$m])){
                                     $list_words[$m]['msgid'] = array($m) ;
                                     $list_words[$m]['msgstr'] = array($m) ;
                                     $list_words[$m]['reference'] = array() ;
                                  }
                                  $list_words[$m]['reference'][] = $dir.':'.$key ;
                        }
                  }
               }

         // }
     }
}

$dir = 'themes/mytheme/' ;
po_fetch_code($dir , "/__\(('|\")(.*?)('|\")\)/" ) ;
po_fetch_code($dir , "/_e\(('|\")(.*?)('|\")\)/" ) ;

Is there's any way to enhance my function ?? Thanks

5
  • Are you saying you want to search your code base for anything with _e(? Commented Nov 7, 2015 at 19:09
  • Yes , and also other gettext function __ Commented Nov 7, 2015 at 19:10
  • Do you have access to command line/shell interface? Commented Nov 7, 2015 at 19:11
  • No , I want it with php code , host disabled all command functions Commented Nov 7, 2015 at 19:14
  • 1
    Well that will be a pain. You're going to need PHP to open all your files in that case and then run a regex over the contents. I'd try to see if you can get your host to allow you to use grep or execute a command for you.. Commented Nov 7, 2015 at 19:15

1 Answer 1

2

If your goal is to generate a translation catalog of your theme, you should use the xgettext command line tool. There is really no sense in trying to parse PHP files manually.

## first, collect all the theme’s PHP files into one temporary file
find -name "*.php" wp-content/themes/yourtheme > /tmp/themefiles.txt

# create catalog from translatable strings in your theme’s files
xgettext --from-code=utf-8 --keyword=__ --keyword=_e --keyword="_n:1,2" --keyword="_x:2c,1" --language=PHP -j -o wp-content/themes/yourtheme/yourtheme.pot -f /tmp/themefiles.txt

If you don’t have access to a Linux shell on your webserver, you can set up a Linux VM with a simple distro like Ubuntu or Mint and do the xgettext stuff there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.