Use page Title in Gutenberg custom banner block
I have created a custom banner image block for Gutenberg, which works great, but
I want to know if it is possible to use the page title as the current banner text
placeholder until it has been edited?

My Edit function is
return [
el('div', {className:'header-banner'},
el(
element.Fragment,
null,
controls,
el( "div",{
className: 'banner-image',
style: { backgroundImage: 'url('+attributes.mediaURL+')' }
},
attributes.title || isSelected ? el(RichText, {
key: 'editable',
tagName: "h1",
className: "banner-title",
//Can i add the page title in here if it is avaiable??
//placeholder: i18n.__('Write title…'),
value: attributes.title,
onChange: function onChange(value) {
return props.setAttributes({ title: value });
},
inlineToolbar: true
}) : null
)
)
)//header-banner
];
Thanks :)
javascript wordpress wordpress-gutenberg
add a comment |
I have created a custom banner image block for Gutenberg, which works great, but
I want to know if it is possible to use the page title as the current banner text
placeholder until it has been edited?

My Edit function is
return [
el('div', {className:'header-banner'},
el(
element.Fragment,
null,
controls,
el( "div",{
className: 'banner-image',
style: { backgroundImage: 'url('+attributes.mediaURL+')' }
},
attributes.title || isSelected ? el(RichText, {
key: 'editable',
tagName: "h1",
className: "banner-title",
//Can i add the page title in here if it is avaiable??
//placeholder: i18n.__('Write title…'),
value: attributes.title,
onChange: function onChange(value) {
return props.setAttributes({ title: value });
},
inlineToolbar: true
}) : null
)
)
)//header-banner
];
Thanks :)
javascript wordpress wordpress-gutenberg
add a comment |
I have created a custom banner image block for Gutenberg, which works great, but
I want to know if it is possible to use the page title as the current banner text
placeholder until it has been edited?

My Edit function is
return [
el('div', {className:'header-banner'},
el(
element.Fragment,
null,
controls,
el( "div",{
className: 'banner-image',
style: { backgroundImage: 'url('+attributes.mediaURL+')' }
},
attributes.title || isSelected ? el(RichText, {
key: 'editable',
tagName: "h1",
className: "banner-title",
//Can i add the page title in here if it is avaiable??
//placeholder: i18n.__('Write title…'),
value: attributes.title,
onChange: function onChange(value) {
return props.setAttributes({ title: value });
},
inlineToolbar: true
}) : null
)
)
)//header-banner
];
Thanks :)
javascript wordpress wordpress-gutenberg
I have created a custom banner image block for Gutenberg, which works great, but
I want to know if it is possible to use the page title as the current banner text
placeholder until it has been edited?

My Edit function is
return [
el('div', {className:'header-banner'},
el(
element.Fragment,
null,
controls,
el( "div",{
className: 'banner-image',
style: { backgroundImage: 'url('+attributes.mediaURL+')' }
},
attributes.title || isSelected ? el(RichText, {
key: 'editable',
tagName: "h1",
className: "banner-title",
//Can i add the page title in here if it is avaiable??
//placeholder: i18n.__('Write title…'),
value: attributes.title,
onChange: function onChange(value) {
return props.setAttributes({ title: value });
},
inlineToolbar: true
}) : null
)
)
)//header-banner
];
Thanks :)
javascript wordpress wordpress-gutenberg
javascript wordpress wordpress-gutenberg
asked Aug 3 '18 at 13:57
Jim-miraidev
1,0151414
1,0151414
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Gutenberg stores the current editor state using wp.data, which is an abstraction over Redux. To get the title (or 100+ other values), we need to query the core/editor data store.
Once we know where to look, getting the title is simple:
const { select } = wp.data;
const title = select("core/editor").getDocumentTitle();
That works, but it's not responsive. When the post title changes, title won't reflect the new value. That's kind of a let down.
To reflect changes to the editor title, we need to listen for changes to the core/editor data store. There are a few ways to do this.
One solution is to define a simple change handler function and subscribe it to data store updates:
const { select } = wp.data;
function logTitle() {
const title = select("core/editor").getDocumentTitle();
console.log("Editor Title:", title);
}
subscribe(logTitle);
That will fire when any wp.data store value is updated -- which happens a lot.
What seems to be the Gutenberg-sanctioned way of including data-store values is to use a higher-order component to include the value directly:
const GetTitle = props => <div>{props.title}</div>;
const selectTitle = withSelect(select => ({
title: select("core/editor").getDocumentTitle()
}));
const PostTitle = selectTitle(GetTitle);
Then in the block's output, include a <PostTitle /> jsx tag. That's a lot cleaner than nested callbacks or another change handler.
Higher-order components can be difficult to follow. The short explanation is that they wrap a existing component, generate some data, then return a copy of the component with the new data passed as props. This separates logic from presentation and helps with maintainability.
GetTitle is simple enough, it's just a small component that takes in a props object with a title key and spits out some html.
withSelect is a function constructor or decorator. It accepts a function argument, and returns a new function which expects a component. Normally the returned function is invoked immediately (sort of an IIFE) but I stored it in the selectTitle variable for clarity. The new function generates an object containing the title, this object will be passed as props to any components passed to withSelect. Through some magic this will be called whenever the data store is updated.
In the end, PostTitle contains the function result of selectTitle which is a component pre-populated with the the generated props. This component can then be placed into our markup using a <PostTitle /> tag. Whenever the editor data-store is updated, the higher-level component will reflect the new data.
Thank you very much for this, it works great. Really good explanation as well :)
– Jim-miraidev
Aug 13 '18 at 8:26
Can you then use PostTitle to update an attribute? as i can get it to work in the editor, but it doesn't seem to update the frontend content
– Jim-miraidev
Aug 16 '18 at 8:26
thegetDocumentTitleselector was deprecated and has been removed. github.com/WordPress/gutenberg/pull/9623
– joemaller
Oct 1 '18 at 19:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51674293%2fuse-page-title-in-gutenberg-custom-banner-block%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Gutenberg stores the current editor state using wp.data, which is an abstraction over Redux. To get the title (or 100+ other values), we need to query the core/editor data store.
Once we know where to look, getting the title is simple:
const { select } = wp.data;
const title = select("core/editor").getDocumentTitle();
That works, but it's not responsive. When the post title changes, title won't reflect the new value. That's kind of a let down.
To reflect changes to the editor title, we need to listen for changes to the core/editor data store. There are a few ways to do this.
One solution is to define a simple change handler function and subscribe it to data store updates:
const { select } = wp.data;
function logTitle() {
const title = select("core/editor").getDocumentTitle();
console.log("Editor Title:", title);
}
subscribe(logTitle);
That will fire when any wp.data store value is updated -- which happens a lot.
What seems to be the Gutenberg-sanctioned way of including data-store values is to use a higher-order component to include the value directly:
const GetTitle = props => <div>{props.title}</div>;
const selectTitle = withSelect(select => ({
title: select("core/editor").getDocumentTitle()
}));
const PostTitle = selectTitle(GetTitle);
Then in the block's output, include a <PostTitle /> jsx tag. That's a lot cleaner than nested callbacks or another change handler.
Higher-order components can be difficult to follow. The short explanation is that they wrap a existing component, generate some data, then return a copy of the component with the new data passed as props. This separates logic from presentation and helps with maintainability.
GetTitle is simple enough, it's just a small component that takes in a props object with a title key and spits out some html.
withSelect is a function constructor or decorator. It accepts a function argument, and returns a new function which expects a component. Normally the returned function is invoked immediately (sort of an IIFE) but I stored it in the selectTitle variable for clarity. The new function generates an object containing the title, this object will be passed as props to any components passed to withSelect. Through some magic this will be called whenever the data store is updated.
In the end, PostTitle contains the function result of selectTitle which is a component pre-populated with the the generated props. This component can then be placed into our markup using a <PostTitle /> tag. Whenever the editor data-store is updated, the higher-level component will reflect the new data.
Thank you very much for this, it works great. Really good explanation as well :)
– Jim-miraidev
Aug 13 '18 at 8:26
Can you then use PostTitle to update an attribute? as i can get it to work in the editor, but it doesn't seem to update the frontend content
– Jim-miraidev
Aug 16 '18 at 8:26
thegetDocumentTitleselector was deprecated and has been removed. github.com/WordPress/gutenberg/pull/9623
– joemaller
Oct 1 '18 at 19:59
add a comment |
Gutenberg stores the current editor state using wp.data, which is an abstraction over Redux. To get the title (or 100+ other values), we need to query the core/editor data store.
Once we know where to look, getting the title is simple:
const { select } = wp.data;
const title = select("core/editor").getDocumentTitle();
That works, but it's not responsive. When the post title changes, title won't reflect the new value. That's kind of a let down.
To reflect changes to the editor title, we need to listen for changes to the core/editor data store. There are a few ways to do this.
One solution is to define a simple change handler function and subscribe it to data store updates:
const { select } = wp.data;
function logTitle() {
const title = select("core/editor").getDocumentTitle();
console.log("Editor Title:", title);
}
subscribe(logTitle);
That will fire when any wp.data store value is updated -- which happens a lot.
What seems to be the Gutenberg-sanctioned way of including data-store values is to use a higher-order component to include the value directly:
const GetTitle = props => <div>{props.title}</div>;
const selectTitle = withSelect(select => ({
title: select("core/editor").getDocumentTitle()
}));
const PostTitle = selectTitle(GetTitle);
Then in the block's output, include a <PostTitle /> jsx tag. That's a lot cleaner than nested callbacks or another change handler.
Higher-order components can be difficult to follow. The short explanation is that they wrap a existing component, generate some data, then return a copy of the component with the new data passed as props. This separates logic from presentation and helps with maintainability.
GetTitle is simple enough, it's just a small component that takes in a props object with a title key and spits out some html.
withSelect is a function constructor or decorator. It accepts a function argument, and returns a new function which expects a component. Normally the returned function is invoked immediately (sort of an IIFE) but I stored it in the selectTitle variable for clarity. The new function generates an object containing the title, this object will be passed as props to any components passed to withSelect. Through some magic this will be called whenever the data store is updated.
In the end, PostTitle contains the function result of selectTitle which is a component pre-populated with the the generated props. This component can then be placed into our markup using a <PostTitle /> tag. Whenever the editor data-store is updated, the higher-level component will reflect the new data.
Thank you very much for this, it works great. Really good explanation as well :)
– Jim-miraidev
Aug 13 '18 at 8:26
Can you then use PostTitle to update an attribute? as i can get it to work in the editor, but it doesn't seem to update the frontend content
– Jim-miraidev
Aug 16 '18 at 8:26
thegetDocumentTitleselector was deprecated and has been removed. github.com/WordPress/gutenberg/pull/9623
– joemaller
Oct 1 '18 at 19:59
add a comment |
Gutenberg stores the current editor state using wp.data, which is an abstraction over Redux. To get the title (or 100+ other values), we need to query the core/editor data store.
Once we know where to look, getting the title is simple:
const { select } = wp.data;
const title = select("core/editor").getDocumentTitle();
That works, but it's not responsive. When the post title changes, title won't reflect the new value. That's kind of a let down.
To reflect changes to the editor title, we need to listen for changes to the core/editor data store. There are a few ways to do this.
One solution is to define a simple change handler function and subscribe it to data store updates:
const { select } = wp.data;
function logTitle() {
const title = select("core/editor").getDocumentTitle();
console.log("Editor Title:", title);
}
subscribe(logTitle);
That will fire when any wp.data store value is updated -- which happens a lot.
What seems to be the Gutenberg-sanctioned way of including data-store values is to use a higher-order component to include the value directly:
const GetTitle = props => <div>{props.title}</div>;
const selectTitle = withSelect(select => ({
title: select("core/editor").getDocumentTitle()
}));
const PostTitle = selectTitle(GetTitle);
Then in the block's output, include a <PostTitle /> jsx tag. That's a lot cleaner than nested callbacks or another change handler.
Higher-order components can be difficult to follow. The short explanation is that they wrap a existing component, generate some data, then return a copy of the component with the new data passed as props. This separates logic from presentation and helps with maintainability.
GetTitle is simple enough, it's just a small component that takes in a props object with a title key and spits out some html.
withSelect is a function constructor or decorator. It accepts a function argument, and returns a new function which expects a component. Normally the returned function is invoked immediately (sort of an IIFE) but I stored it in the selectTitle variable for clarity. The new function generates an object containing the title, this object will be passed as props to any components passed to withSelect. Through some magic this will be called whenever the data store is updated.
In the end, PostTitle contains the function result of selectTitle which is a component pre-populated with the the generated props. This component can then be placed into our markup using a <PostTitle /> tag. Whenever the editor data-store is updated, the higher-level component will reflect the new data.
Gutenberg stores the current editor state using wp.data, which is an abstraction over Redux. To get the title (or 100+ other values), we need to query the core/editor data store.
Once we know where to look, getting the title is simple:
const { select } = wp.data;
const title = select("core/editor").getDocumentTitle();
That works, but it's not responsive. When the post title changes, title won't reflect the new value. That's kind of a let down.
To reflect changes to the editor title, we need to listen for changes to the core/editor data store. There are a few ways to do this.
One solution is to define a simple change handler function and subscribe it to data store updates:
const { select } = wp.data;
function logTitle() {
const title = select("core/editor").getDocumentTitle();
console.log("Editor Title:", title);
}
subscribe(logTitle);
That will fire when any wp.data store value is updated -- which happens a lot.
What seems to be the Gutenberg-sanctioned way of including data-store values is to use a higher-order component to include the value directly:
const GetTitle = props => <div>{props.title}</div>;
const selectTitle = withSelect(select => ({
title: select("core/editor").getDocumentTitle()
}));
const PostTitle = selectTitle(GetTitle);
Then in the block's output, include a <PostTitle /> jsx tag. That's a lot cleaner than nested callbacks or another change handler.
Higher-order components can be difficult to follow. The short explanation is that they wrap a existing component, generate some data, then return a copy of the component with the new data passed as props. This separates logic from presentation and helps with maintainability.
GetTitle is simple enough, it's just a small component that takes in a props object with a title key and spits out some html.
withSelect is a function constructor or decorator. It accepts a function argument, and returns a new function which expects a component. Normally the returned function is invoked immediately (sort of an IIFE) but I stored it in the selectTitle variable for clarity. The new function generates an object containing the title, this object will be passed as props to any components passed to withSelect. Through some magic this will be called whenever the data store is updated.
In the end, PostTitle contains the function result of selectTitle which is a component pre-populated with the the generated props. This component can then be placed into our markup using a <PostTitle /> tag. Whenever the editor data-store is updated, the higher-level component will reflect the new data.
answered Aug 10 '18 at 18:20
joemaller
11.3k55267
11.3k55267
Thank you very much for this, it works great. Really good explanation as well :)
– Jim-miraidev
Aug 13 '18 at 8:26
Can you then use PostTitle to update an attribute? as i can get it to work in the editor, but it doesn't seem to update the frontend content
– Jim-miraidev
Aug 16 '18 at 8:26
thegetDocumentTitleselector was deprecated and has been removed. github.com/WordPress/gutenberg/pull/9623
– joemaller
Oct 1 '18 at 19:59
add a comment |
Thank you very much for this, it works great. Really good explanation as well :)
– Jim-miraidev
Aug 13 '18 at 8:26
Can you then use PostTitle to update an attribute? as i can get it to work in the editor, but it doesn't seem to update the frontend content
– Jim-miraidev
Aug 16 '18 at 8:26
thegetDocumentTitleselector was deprecated and has been removed. github.com/WordPress/gutenberg/pull/9623
– joemaller
Oct 1 '18 at 19:59
Thank you very much for this, it works great. Really good explanation as well :)
– Jim-miraidev
Aug 13 '18 at 8:26
Thank you very much for this, it works great. Really good explanation as well :)
– Jim-miraidev
Aug 13 '18 at 8:26
Can you then use PostTitle to update an attribute? as i can get it to work in the editor, but it doesn't seem to update the frontend content
– Jim-miraidev
Aug 16 '18 at 8:26
Can you then use PostTitle to update an attribute? as i can get it to work in the editor, but it doesn't seem to update the frontend content
– Jim-miraidev
Aug 16 '18 at 8:26
the
getDocumentTitle selector was deprecated and has been removed. github.com/WordPress/gutenberg/pull/9623– joemaller
Oct 1 '18 at 19:59
the
getDocumentTitle selector was deprecated and has been removed. github.com/WordPress/gutenberg/pull/9623– joemaller
Oct 1 '18 at 19:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51674293%2fuse-page-title-in-gutenberg-custom-banner-block%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown