-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
How to use dynamic data with registerFormatType #18490
Comments
Or maybe there is way to render server side such format. |
@danieliser Thanks! It is like static format insertion, I mean using data which may be changed while editing like post slug or post name. |
You can use normal |
Hi @ellatrix const type = 'test/test';
registerFormatType(type, {
title: 'Slug link',
tagName: 'a',
className: 'slug',
edit ({ isActive, value, onChange }) {
const permalink = useSelect(( select ) => {
return select('core/editor').getPermalink();
});
const onToggle = () => {
onChange(toggleFormat(value, {
type,
attributes: {
'href': '#',
// I'd like to change it on post permalink change
'data-permalink': permalink,
},
}));
};
return <RichTextToolbarButton
icon={ 'editor-code' }
title={ 'Post slug' }
onClick={ onToggle }
isActive={ isActive }
/>;
}
}); I'd like to have attribute |
It is an example with |
@mahnunchik - So I see it 2 ways.
|
Hi @danieliser Yep, server side approach solves the problem. I'm using I think @ellatrix as a core developer and author of many things in More general question: Is is possible to hook changes in |
@mahnunchik Well sure it’s possible, but a Gutenberg only solution could very well still use server side approach. Many blocks already do this such as showing latest 5 posts in a list. So the question isn’t can you do it, it’s should you do it, or more importantly should you spend the time when the most reliable method is much simpler. Also my proposal would be to use the Format API to wrap your text with a tag (not shortcode) along with some string which is easily parsed and replaced later. So your format api might add a span with unique data attribute around the selected text. It could even be JSON. During server side processing you replace that data attribute with the proper dynamically rendered content. The other way is both unreliable and likely to conflict over time if your constantly searching and replacing content across the entire editor that could be formatted by other plugins etc. Just my 2 cents. |
It is server side rendering of
Yep, I'm implementing this approach for now. Unique class name helps to find and replace. But it would be pretty cool to have some of:
|
I don't think it's possible to change an already applied format, only those that are yet to be applied, so lets change this to an enhancement request. It is probably a bit beyond the scope of most formatting use cases though and might be better handled through a plugin that filters post content. |
Hi @talldan @danieliser @ellatrix Yep, I've already wrote about it: Some kind of server side register_format_type with render_callback for formatted part of existing blocks with Rich Text field. #18490 (comment) Use cases I'm working on:
Yep, it is handled by |
I would love a solution for this, but it's low priority for me to work on right now to be honest. Would love to see explorations! |
@mahnunchik - From my experience, the currency converter concept doesn't make sense in this context. Specifically because of caching any real solution would need to be JavaScript based, so the most that needs to happen is a format tags text with something that JS detects and replaces on the fly. A simple css class or data-attribute handles that without dynamic replacement. That said this sounds like it should be one offed honestly. There is no way that running regex across the content of every page will be viable / scalable if this replacement technique were picked up by dozens of plugins on the same site. Instead it would probably be better to do a simple strpos check that some key part of the phrase exists first before running it for matches needlessly. But that likely means doing this on a per plugin/theme basis, as opposed to as some built in mechanism. The best solution is gonna come in the form of JS based rendering at runtime, its the only way to beat the caching setups on most WP hosts these days. |
Hi @danieliser Currency converter is just a sample. For my case it should be static (not JS based) because:
I hope I was able to show the main idea: post processing of |
I'm also currently solving this use case by storing some data in data-attributes and then using a filter on However, I just found that there is no way to filter the content of block templates. So this method breaks for any rich text format that gets used in any template... |
Part of #41236.
How to use dynamic data with format registered by
registerFormatType
?The text was updated successfully, but these errors were encountered: