Heading defaults
-
This one is probably down to me but I’ll ask anyway… when I’m creating or editing a blog entry, I like sections to have headings – but every time – the default heading is heading 2 in default colour… is there any way to tell WP to default to H3 and maybe one of the colours over in the right panel – I use green.
The page I need help with: [log in to see the link]
-
Hi @scargill
To set default heading to h3 and color to green there is not any setting at wp-admin side, you have to use custom code as per below which I have implemented in child theme.
Update it according to your requirement in parent or child theme.
// Please add below code in “functions.php” file of the theme, then create “editor.js” file and “editor-style.css” on theme root folder./***************************************************************************************/
/*************************************** functions.php **********************************/
/****************************************************************************************/
// Editor style code - set h3 element color to green (Create "editor-style.css" in theme root folder)
function mytheme_add_editor_styles() {
add_theme_support('editor-styles'); // Enables editor-style
add_editor_style('editor-style.css'); // Path of theme root
}
add_action('after_setup_theme', 'mytheme_add_editor_styles');
// Set default element to h3 (Create "editor.js" in theme root folder) (get_stylesheet_directory_uri() for child theme, use get_template_directory_uri() for parent or main theme)
function mytheme_editor_assets() {
wp_enqueue_script(
'mytheme-editor-js',
get_stylesheet_directory_uri() . '/editor.js',
array('wp-blocks', 'wp-dom-ready', 'wp-edit-post'),
filemtime(get_stylesheet_directory_uri() . '/editor.js')
);
}
add_action('enqueue_block_editor_assets', 'mytheme_editor_assets');
/*************************************************************************************/
/*************************************** editor.js ***********************************/
/*************************************************************************************/
wp.domReady(function () {
const { getBlockType, unregisterBlockType, registerBlockType } = wp.blocks;
const headingBlock = getBlockType('core/heading');
if (headingBlock) {
unregisterBlockType('core/heading');
registerBlockType('core/heading', {
...headingBlock,
attributes: {
...headingBlock.attributes,
level: {
type: 'number',
default: 3, // π Set default heading level to H3
},
},
});
}
});
/*************************************************************************************/
/************************************* editor-style.css *******************************/
/*************************************************************************************/
/* This targets headings inside the Gutenberg editor */
.editor-styles-wrapper h3 {
color: #2e8b57; /* Green */
}
/* Optionally: frontend headings too */
h3 {
color: #2e8b57;
}Understand all that thanks – not sure how to create those files you mention – can’t seen to do it in WP admin – in CPANEL ? Oh I tried that – some issue with formatting your code – is the forum adding in some special characters??
-
This reply was modified 11 months ago by
scargill.
Hi @scargill
First check activated theme by visiting “Appearance” tab on wp-admin side (http://yoursite.com/wp-admin/themes.php)
Then by using file manager at cPanel, you can create those files as per previous message in wp-content/themes/your_activated_theme directory.
No, foram not adding any special characters.Truly sorry to waste your time – but in creating editor.js and pasting in code from the forum… I’m seeing…
wp.domReady(function () {
const { getBlockType, unregisterBlockType, registerBlockType } = wp.blocks;const headingBlock = getBlockType('core/heading'); if (headingBlock) { unregisterBlockType('core/heading'); registerBlockType('core/heading', { ...headingBlock, attributes: { ...headingBlock.attributes, level: { type: 'number', default: 3, // π Set default heading level to H3 }, }, }); }});
and the editor is griping from line 12 (…headingBlock) on..
Oh I can’t show you and image of what I see (standad Win 11 PC, Chrome)as this dumb forum editor won’t let me paste in images…. expected } to match { from line 11 and instead saw ‘…’. Expected ) and instead saw headingBlock etc etc and theres a finger after default: 3, //
Possibly email [email removed by moderator] the text of that code?
-
This reply was modified 10 months, 4 weeks ago by
scargill.
-
This reply was modified 10 months, 4 weeks ago by
Steven Stern (sterndata).
have any error message?
Hi @scargill ,
Try below one for editor.jswp.domReady(function () {
const { getBlockType, unregisterBlockType, registerBlockType } = wp.blocks;
const headingBlock = getBlockType('core/heading');
if (headingBlock) {
unregisterBlockType('core/heading');
registerBlockType('core/heading', {
...headingBlock,
attributes: {
...headingBlock.attributes,
level: {
type: 'number',
default: 3,
},
},
});
}
});Let me know if you face any issue and possible to send screenshots of the issue.
Thanks.Hi @scargill
I have checked in blog-kit theme and made some changes, please create/update file and update/add below code in your theme’s file. Please check below image link,// editor.js
wp.domReady(function () {
const { getBlockType, unregisterBlockType, registerBlockType } = wp.blocks;
const headingBlock = getBlockType('core/heading');
if (headingBlock) {
unregisterBlockType('core/heading');
registerBlockType('core/heading', {
...headingBlock,
attributes: {
...headingBlock.attributes,
level: {
type: 'number',
default: 3, // Set default heading level to H3
},
textColor: {
type: 'string',
default: 'green', // Set default color to green
},
style: {
type: 'object',
default: {
color: {
text: '#00d084' // Alternative way to set default color
}
}
}
},
});
}
});// editor-style.css
/* This targets headings inside the Gutenberg editor */
.editor-styles-wrapper h3 {
color: #00d084 /* Green */
}
/* Optionally: frontend headings too */
h3 {
color: #00d084;
}// theme.json
{
"version": 2,
"settings": {
"color": {
"palette": [
{
"name": "Green",
"slug": "green",
"color": "#00d084"
}
]
}
},
"styles": {
"blocks": {
"core/heading": {
"typography": {
"fontSize": "var(--wp--preset--font-size--medium)"
},
"color": {
"text": "var(--wp--preset--color--green)"
}
}
}
}
}// functions.php
// Editor style code - set h3 element color to green (Create "editor-style.css" in theme root folder)
function mytheme_add_editor_styles() {
add_theme_support('editor-styles'); // Enables editor-style
add_editor_style('editor-style.css'); // Path of theme root
}
add_action('after_setup_theme', 'mytheme_add_editor_styles');
// Set default element to h3 (Create "editor.js" in theme root folder) (get_template_directory_uri() for child theme, use get_template_directory_uri() for parent or main theme)
function mytheme_editor_assets() {
wp_enqueue_script(
'mytheme-editor-js',
get_template_directory_uri() . '/editor.js',
array('wp-blocks', 'wp-dom-ready', 'wp-edit-post'),
filemtime(get_template_directory_uri() . '/editor.js')
);
}
add_action('enqueue_block_editor_assets', 'mytheme_editor_assets');Thanks ut no matter what I try – those 3 periods before headingBlock cause the editor to throw up errors…
That or the comma…
...headingBlock,I simply can’t find a ay to save the above without errors. I’m in cpanel file namager – got the empty editor.js file – edit…..
-
This reply was modified 10 months, 4 weeks ago by
scargill.
Hi @scargill
I got it that you’re getting warnings in cPanel’s file manager due to the spread operator (…) in the JavaScript code. This is likely because cPanel’s file manager might be using an older JavaScript parser that doesn’t support the spread operator syntax. Let me help you modify the code to be more compatible.
I’ve modified the code to replace the spread operator (…) with Object.assign(), which is a more widely supported method for object merging. The functionality remains exactly the same, but now it should work without warnings in cPanel’s file manager.
// editor.js
wp.domReady(function () {
const { getBlockType, unregisterBlockType, registerBlockType } = wp.blocks;
const headingBlock = getBlockType('core/heading');
if (headingBlock) {
unregisterBlockType('core/heading');
registerBlockType('core/heading', Object.assign({}, headingBlock, {
attributes: Object.assign({}, headingBlock.attributes, {
level: {
type: 'number',
default: 3, // Set default heading level to H3
},
textColor: {
type: 'string',
default: 'green', // Set default color to green
},
style: {
type: 'object',
default: {
color: {
text: '#00d084' // Alternative way to set default color
}
}
}
})
}));
}
});Replace above code in editor.js file.
ThanksHi @scargill ,
Your issue resolved? -
This reply was modified 11 months ago by
The topic ‘Heading defaults’ is closed to new replies.