Skip to main content
deleted 1 character in body
Source Link
Lovor
  • 986
  • 9
  • 16

I slightly modified yourSkyShab's code, because it fired template change event on page load and it didn't fire when template was changed to default (since getEditedPostAttribute( 'template' ) is then '', which equals to null whanwhen testing for condition in your codetemplate change)

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

    constructor() {
        this.template = null;
    }

    init() {

        subscribe( () => {

            const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
            if (newTemplate !== undefined && this.template === null) {
                this.template = newTemplate;
            }

            if ( newTemplate !== undefined && newTemplate !== this.template ) {
                this.template = newTemplate;
                this.changeTemplate();
            }

        });
    }

    changeTemplate() {
        // do your stuff here
        console.log('template changed to ${this.template}');
    }
}

new PageTemplateSwitcher().init();

I slightly modified your code, because it fired template change event on page load and it didn't fire when template was changed to default (since getEditedPostAttribute( 'template' ) is then '', which equals to null whan testing for condition in your code

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

    constructor() {
        this.template = null;
    }

    init() {

        subscribe( () => {

            const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
            if (newTemplate !== undefined && this.template === null) {
                this.template = newTemplate;
            }

            if ( newTemplate !== undefined && newTemplate !== this.template ) {
                this.template = newTemplate;
                this.changeTemplate();
            }

        });
    }

    changeTemplate() {
        // do your stuff here
        console.log('template changed to ${this.template}');
    }
}

new PageTemplateSwitcher().init();

I slightly modified SkyShab's code, because it fired template change event on page load and it didn't fire when template was changed to default (since getEditedPostAttribute( 'template' ) is then '', which equals to null when testing for template change)

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

    constructor() {
        this.template = null;
    }

    init() {

        subscribe( () => {

            const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
            if (newTemplate !== undefined && this.template === null) {
                this.template = newTemplate;
            }

            if ( newTemplate !== undefined && newTemplate !== this.template ) {
                this.template = newTemplate;
                this.changeTemplate();
            }

        });
    }

    changeTemplate() {
        // do your stuff here
        console.log('template changed to ${this.template}');
    }
}

new PageTemplateSwitcher().init();
added 120 characters in body
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

I slightly modified your code, because it fired template change event on page load and it didn't fire when template was changed to default (since getEditedPostAttribute( 'template' ) is then '', which equals to null whan testing for condition in your code

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {


class PageTemplateSwitcher {

    constructor() {
        this.template = null;
    }

    init() {

        subscribe( () => {

            const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
            if (newTemplate !== undefined && this.template === null) {
                this.template = newTemplate;
            }

            if ( newTemplate !== undefined && newTemplate !== this.template ) {
                this.template = newTemplate;
                this.changeTemplate();
            }

        });
    }

    changeTemplate() {
        // do your stuff here
        console.log('template changed to ${this.template}');
    }
}

new PageTemplateSwitcher().init();

}

new PageTemplateSwitcher().init();

I slightly modified your code, because it fired template change event on page load and it didn't fire when template was changed to default (since getEditedPostAttribute( 'template' ) is then '', which equals to null whan testing for condition in your code

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

constructor() {
    this.template = null;
}

init() {

    subscribe( () => {

        const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
        if (newTemplate !== undefined && this.template === null) {
            this.template = newTemplate;
        }

        if ( newTemplate !== undefined && newTemplate !== this.template ) {
            this.template = newTemplate;
            this.changeTemplate();
        }

    });
}

changeTemplate() {
    // do your stuff here
    console.log('template changed to ${this.template}');
}

}

new PageTemplateSwitcher().init();

I slightly modified your code, because it fired template change event on page load and it didn't fire when template was changed to default (since getEditedPostAttribute( 'template' ) is then '', which equals to null whan testing for condition in your code

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

    constructor() {
        this.template = null;
    }

    init() {

        subscribe( () => {

            const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
            if (newTemplate !== undefined && this.template === null) {
                this.template = newTemplate;
            }

            if ( newTemplate !== undefined && newTemplate !== this.template ) {
                this.template = newTemplate;
                this.changeTemplate();
            }

        });
    }

    changeTemplate() {
        // do your stuff here
        console.log('template changed to ${this.template}');
    }
}

new PageTemplateSwitcher().init();
Source Link
Lovor
  • 986
  • 9
  • 16

I slightly modified your code, because it fired template change event on page load and it didn't fire when template was changed to default (since getEditedPostAttribute( 'template' ) is then '', which equals to null whan testing for condition in your code

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

constructor() {
    this.template = null;
}

init() {

    subscribe( () => {

        const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );
        if (newTemplate !== undefined && this.template === null) {
            this.template = newTemplate;
        }

        if ( newTemplate !== undefined && newTemplate !== this.template ) {
            this.template = newTemplate;
            this.changeTemplate();
        }

    });
}

changeTemplate() {
    // do your stuff here
    console.log('template changed to ${this.template}');
}

}

new PageTemplateSwitcher().init();