0

I open the image picker of wordpress on the front end with this function

function save_image($id){
    var mediaUploader;
    event.preventDefault();
    // If the uploader object has already been created, reopen the dialog
    if (mediaUploader) {
        mediaUploader.open();
        return;
    }
    // Extend the wp.media object
    var popupTitle = 'Choisir Image';
    if($('body').hasClass('en')){
        popupTitle = 'Choose Image';
    }
    mediaUploader = wp.media.frames.file_frame = wp.media({
        title: popupTitle,
        button: {
            text: popupTitle
        },
        multiple: false,
        type: 'image'
    });

    // When a file is selected, grab the URL and set it as the text field's value
    mediaUploader.on('select', function() {
        attachment = mediaUploader.state().get('selection').first().toJSON();
    });
    // Open the uploader dialog
    mediaUploader.open();
}

I'm wondering how can I define it's language? I have a bilingual site with WPML, but the popup is always in english now.

What option could I change get it in french (in the french version of the site)? I think its somewhere in the options of wp.media, but have no idea what to call for.

  • I don't know but to put my 2p, I can confirm that wp.media doesn't have any method that can do that and I really doubt it can give you any solution to that problem. – ClemC Oct 6 '16 at 19:02
0

I am french too, but I must answer in English. You need to use wp_localize_script(), that allow to handle localization in js scripts.

add_action('wp_enqueue_scripts', 'load_script');
function load_script(){
    wp_register_script( 'popup', plugins_url('yourscript/js/yourjs.js'), array( 'jquery' ), false, true );
    wp_localize_script(
        'popup',
        'popUp',
        array(
            'popupTitle'=> __('', 'your-text-domain)
        )
    );
    wp_enqueue_script('popup');
}

In the js file, you will able to retrieve value:

function save_image($id){
    var mediaUploader;
    event.preventDefault();

    // If the uploader object has already been created, reopen the dialog
    if (mediaUploader) {
        mediaUploader.open();
        return;
    }

    // Extend the wp.media object
    var popupTitle = popUp.popupTitle;

    mediaUploader = wp.media.frames.file_frame = wp.media({
        title: popupTitle,
        button: {
            text: popupTitle
        },
        multiple: false,
        type: 'image'
    });

    // When a file is selected, grab the URL and set it as the text field's value
    mediaUploader.on('select', function() {
        attachment = mediaUploader.state().get('selection').first().toJSON();
    });

    // Open the uploader dialog
    mediaUploader.open();
}

https://codex.wordpress.org/Function_Reference/wp_localize_script

You can install the plugin WordPress Media Javascript Guide. It explains with screenshots and examples the must use method of the wp.media object.

Just a snippet to change the button text and the title:

wp_register_script( 'media-bbss-uploader-js', BBSSLIDERS_DIR_URL .'/js/bbss-multiple-media-upload.js', array('jquery') );
wp_localize_script(
    'media-bbss-uploader-js',
    'screenHelp',
    array(
        'title' => __('Select or upload new slides', $this->plugin_text_domain),
        'buttomText' => __('Add slides', $this->plugin_text_domain),
    )
);
wp_enqueue_script( 'media-bbss-uploader-js' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.