0

I have been trying to get a grasp of WordPress Theme Development. I have some understanding of JavaScript and PHP. When I was trying to figure out how to use the media uploader and I found several articles using lines of code like below:

mediaUploader = wp.media.frames.file_frame = wp.media({
        title: 'Choose a Profile Picture',
        button: {
            text: 'Choose Picture'
        },
        multiple: false
    });

It does not make sense to me to use "mediaUploader = wp.media.frames.file_frame = wp.media({});". Why not just use mediaUploader = wp.media({})? I appreciate any help I can get on this because I really want to understand it.

1 Answer 1

2

In JavaScript (and PHP, too), you can save a few lines of code by assigning multiple variables at once. In this example, the function wp.media() is called and immediately assigned to wp.media.frames.file_frame. The author wanted to use the same value for mediaUploader so they added it in the same assignment statement.

Another way to look at it is

// Option 1: single function call, multiple assignments    
wp.media.frames.file_frame = wp.media({...});
mediaUploader              = wp.media.frames.file_frame;

// Option 2: multiple function calls, multiple assignments
wp.media.frames.file_frame = wp.media({...});
mediaUploader              = wp.media({...});

This can be useful if you're assigning a value to a global object but want to manipulate the value locally without affecting the global. In this case, the author can assign the value to wp.media.frames.file_frame so it's available there, and use mediaUploader locally inside a function without affecting the value of the other scoped variable.

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.