admin管理员组文章数量:1325236
How to detect the value of selected page template in the edit page? I have used jQuery to select the page attribute select box but it is not working when we hide the editor sidebar; Because it removes the markup of that settings. I think it keeps data somewhere as we are able to bring back the sidebar without any change.
My code:
$('.editor-page-attributes__template select').val()
Is there any way to get the value when the sidebar is closed? Like any JavaScript variable in the page that updates when we change page template.
Note: I mean getting the value and use it in JavaScript without refreshing or updating the editor.
How to detect the value of selected page template in the edit page? I have used jQuery to select the page attribute select box but it is not working when we hide the editor sidebar; Because it removes the markup of that settings. I think it keeps data somewhere as we are able to bring back the sidebar without any change.
My code:
$('.editor-page-attributes__template select').val()
Is there any way to get the value when the sidebar is closed? Like any JavaScript variable in the page that updates when we change page template.
Note: I mean getting the value and use it in JavaScript without refreshing or updating the editor.
Share Improve this question asked May 30, 2019 at 8:28 HectorHector 6821 gold badge7 silver badges18 bronze badges2 Answers
Reset to default 5I 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();
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();
本文标签: javascriptHow to get value of selected page template in Gutenberg editor
版权声明:本文标题:javascript - How to get value of selected page template in Gutenberg editor? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742167063a2426022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论