How to set a default option value for a backbone select in wordpress media libra

  • Unknown's avatar

    Wondering if it’s possible to trigger a default option for this backbone dropdown in the wordpress media library. I’ve created a new filter for “years”, and it would be great if I could set the default to be the current year rather than always load “filter.all”.

    New to the world of backbone, and thanks in advance!

    Here’s my code that creates the filter and adds it to the toolbar.

    `
    // Create Years Filter
    var MediaLibraryYearsFilter = wp.media.view.AttachmentFilters.extend({
    id: ‘media-attachment-years-filter’,

    createFilters: function() {
    var filters = {};

    filters.all = {

    text: ‘All Years’,
    props: {
    year: “”,
    },
    priority: 10
    };

    // Get years from 2015->Today
    this.years = function(startYear) {
    var currentYear = new Date().getFullYear(), years = [];
    startYear = startYear || 1980;
    while ( startYear <= currentYear ) {
    years.push(startYear++);
    }
    return years;
    }
    years = this.years(2015).reverse();

    _.each( years || {}, function( value, index ) {
    filters[ index ] = {
    text: value,
    props: {
    year: value
    },
    priority: 20
    };
    });
    this.filters = filters;
    }
    });

    // create ToolBar
    var AttachmentsBrowser = wp.media.view.AttachmentsBrowser;
    wp.media.view.AttachmentsBrowser = wp.media.view.AttachmentsBrowser.extend({

    createToolbar: function() {

    // original toolbar
    AttachmentsBrowser.prototype.createToolbar.call( this );

    this.toolbar.set( ‘MediaLibraryYearsFilter’, new MediaLibraryYearsFilter({
    controller: this.controller,
    model: this.collection.props,
    priority: -80
    }).render() );
    },
    });`

  • Hi there,

    It sounds like you’re using the open source version of WordPress, made by the community on WordPress.org.

    This forum is for the hosting provider, WordPress.com, and the type of customization you’re doing isn’t possible on free WordPress.com sites, so we don’t really have the expertise here to help you with this.

    Please ask the community in the forums at https://wordpress.org/support/forum/wp-advanced/ for advice on this instead.

  • The topic ‘How to set a default option value for a backbone select in wordpress media libra’ is closed to new replies.