-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add page-template/post-template value to body class #8162
Comments
Related: #7810 (I don't think they're adding it) I did it manually by rendering this component into an invisible div I added on Gutenberg page: class PageTemplateUpdater extends React.Component {
componentDidUpdate(prevProps) {
if(prevProps.template) {
$('body').removeClass('page-template-' + prevProps.template.replace('.php', ''));
}
if(this.props.template) {
$('body').addClass('page-template-' + this.props.template.replace('.php', ''));
}
}
render() {
return null;
}
}
const withPageTemplate = wp.compose.createHigherOrderComponent(
wp.data.withSelect(
select => {
const {
getEditedPostAttribute,
} = select('core/editor');
return {
template: getEditedPostAttribute('template'),
};
}
),
'withPageTemplate'
);
export default withPageTemplate(PageTemplateUpdater); |
Closing as a duplicate of #7810. Thank you so much for the code example @websevendev! |
@websevendev: I am importing your code in the editor JavaScript file that is enqueued for Gutenberg. |
@strarsis As I said, I render this component manually. The aforementioned code is in, for example, import PageTemplateWatcher from './PageTemplateWatcher'; and render it: document.addEventListener('DOMContentLoaded', () => {
ReactDOM.render(<PageTemplateWatcher />, document.getElementById('my-root'));
}); And the root is added by PHP, but you could dynamically create it as well: add_action('admin_footer', function() {
?>
<div id="my-root"></div>
<?php
}); |
@websevendev: Thanks! Eslint complains about missing props validation.
Edit: Right, I have to add the classes in theme using the /**
* Admin body template class
*/
function admin_body_template_class( $str_classes ) {
global $post;
$template_slug = get_page_template_slug( $post );
$classes = explode(' ', $str_classes);
$classes[] = $template_slug;
$new_str_classes = join(' ', $classes);
return $new_str_classes;
}
add_filter( 'admin_body_class', 'admin_body_template_class' ); IMHO it would be great if this code is added to Gutenberg core or at least |
Based on the code here, I made a little tutorial (sage9 based theme): https://discourse.roots.io/t/gutenberg-body-class-elements-by-template/15310 |
When switching page or post template, it would be good to also add this value to the body class on the admin screen.
I'm working on a theme, and I want to modify the editor layout (widths) and styles for the block content (typo), according to the theme.
The text was updated successfully, but these errors were encountered: