Skip to main content
changed wording to be more precise
Source Link
SkyShab
  • 1.4k
  • 15
  • 17

To get this value, use the wp.data objectmodule.

const template = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'template' );

Since this can be changed in the document settings, you would likely need to "subscribe" and run a callback whenever this changes.

For example:

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

  constructor() {
    this.template = null;
  }

  init() {

    subscribe( () => {

      const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );

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

    });
  }

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

new PageTemplateSwitcher().init();

To get this value, use the wp.data object.

const template = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'template' );

Since this can be changed in the document settings, you would likely need to "subscribe" and run a callback whenever this changes.

For example:

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

  constructor() {
    this.template = null;
  }

  init() {

    subscribe( () => {

      const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );

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

    });
  }

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

new PageTemplateSwitcher().init();

To get this value, use the wp.data module.

const template = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'template' );

Since this can be changed in the document settings, you would likely need to "subscribe" and run a callback whenever this changes.

For example:

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

  constructor() {
    this.template = null;
  }

  init() {

    subscribe( () => {

      const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );

      if ( newTemplate && 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
SkyShab
  • 1.4k
  • 15
  • 17

To get this value, use the wp.data object.

const template = wp.data.select( 'core/editor' ).getEditedPostAttribute( 'template' );

Since this can be changed in the document settings, you would likely need to "subscribe" and run a callback whenever this changes.

For example:

const { select, subscribe } = wp.data;

class PageTemplateSwitcher {

  constructor() {
    this.template = null;
  }

  init() {

    subscribe( () => {

      const newTemplate = select( 'core/editor' ).getEditedPostAttribute( 'template' );

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

    });
  }

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

new PageTemplateSwitcher().init();