• Resolved mocheng

    (@mocheng)


    I’d like to have 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 below:

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

    The sample.js.php file is referred as javascript.

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

    However, it seems __() and _e() does’t work as they are not defined.
    How to make the _e() and __() work in my case?

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • When you call an external php file this way, it’s not passing through WordPress and won’t have any of the typical WordPress functionality. To re-enable __() and _e() you have to include the WP initialization files.

    So add this code to the top of your sample.js.php file:

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

    That should do it.

    Thread Starter mocheng

    (@mocheng)

    Thanks ericmann.
    It works.

    Thread Starter mocheng

    (@mocheng)

    BTW, what is “define(‘WP_USE_THEMES’, false);” for?
    Is it necessary? I remove this line and it still works.

    Not entirely necessary, it’s just setting a constant that’s used elsewhere in WordPress.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to make __() and _e() work in any PHP file?’ is closed to new replies.