How to get __ () and _e () to work in any php wordpress file?

I want wordpress to do I18N for my javascript. My plan is to have javascript code in php file. For example, one sample.js.php file as shown below:

function foo()
{
   alert(<?php _e('do something'); ?>);
}

      

The sample.js.php file is called javascript.

<script type='text/javascript'>url-to-myplugin/sample.js.php</script>

      

However, it seems that __ () and _e () do not work as they are undefined. How do I make _e () and __ () work in my case?

thank


I found the answer. Below code will do the job.

<?php
    define('WP_USE_THEMES', false);
    require('./wp-blog-header.php');
?>

      

+2


source to share


2 answers


Just include the WP config file in your sample.js.php script:



<?php require_once 'your-path-to/wp-config.php'; ?>

function foo()
{
   alert(<?php _e('do something'); ?>);
}

      

+1


source


Have you installed gettext extenssion?



-1


source







All Articles