1

I've created a media view template with two input fields (width and height) and I was wondering how to retrieve the values entered in them in a media controller o view.

PHP:

function add_my_media_view_template(){
?>
    <script type="text/html" id="tmpl-my-media-view">
        <label class="setting width">
            <span><?php _e('Width'); ?></span>
            <input type="text" class="alignment" data-setting="width" />
        </label>
        <label class="setting height">
            <span><?php _e('Height'); ?></span>
            <input type="text" class="alignment" data-setting="height" />
        </label>
    </script>
<?php  }
add_action( 'print_media_templates', 'add_my_media_view_template');

JS:

wp.media.view.MyMediaViewSettings = wp.media.view.Settings.extend({
    className: 'my-media-view',
    template:  wp.media.template('my-media-view'),

    initialize: function() {
        ....
    }

});

wp.media.controller.Custom = wp.media.controller.State.extend({

    initialize: function(){
        ....
    },

    // called when the toolbar button is clicked
    customAction: function(){
        // NEED TO GET THE VALUES OF THE WIDTH AND HEIGHT INPUT FIELDS
    }

});

Any help would be appreciated. The media modal is poorly documented and it's difficult to build things using it.

Thanks

1 Answer 1

1

After digging through the media modal code, I came up with the answer to my question:

wp.media.controller.Custom = wp.media.controller.State.extend({

    initialize: function(){
        this.props = new Backbone.Model();
    },

    // called when the toolbar button is clicked
    customAction: function( controller ){
        // get the value of a media view template form field using
        // this.props.get('key'), where 'key' is the value of its 
        // 'data-setting' attribute
        console.log('width: ' + this.props.get('width'));
        console.log('height: ' + this.props.get('height'));
    }

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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