Main menu

loco_file_mod_allowed_context

Filter the context used to override file modification permission

If your site has DISALLOW_FILE_MODS define as true, then a file_mod_allowed filter would be required to allow the WordPress updater to install translation files. The following code will do that, using only WordPress core functions:

add_filter('file_mod_allowed', function($default,$context){
    if( 'download_language_pack' === $context ){
        return true;
    }
    return $default;
},10,2);

Loco Translate observes this security model for all file modifications, and uses the "download_language_pack" context by default. Hence the above code will allow you to save and compile translation files via the editor even when DISALLOW_FILE_MODS would prevent it.

The purpose of loco_file_mod_allowed_context is simply to modify the context value that gets passed to wp_is_file_mod_allowed, as follows:

/* define a context value to allow translation file mods */
add_filter('loco_file_mod_allowed_context', function(){
    return 'my_file_mod_context';
} );

/* let our file context always allow modification */
add_filter('file_mod_allowed', function($default,$context){
    if( 'my_file_mod_context' === $context ){
        return true;
    }
    return $default;
},10,2);

Note that this allows all file modifications instigated by Loco Translate, and returns true in all cases where the $context parameter is of the expected value. For extra security you might want to check the current user's permissions with current_user_can.

Last updated by