-
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
How do I get the clientId of a block? #25195
Comments
@TJBriggs The clientId is passed as a prop to the function edit( { clientId, attributes } ) { I don't think it's present in the I'll add the documentation label to this issue so that we treat this as a request to update the docs, as I think this is something that should be mentioned. |
Since this issue is still open I'd like to add my question.
What method should I use? |
@bobbingwide Take a look at this issue - #17246. I think the discussed react hook is still the best option for static blocks - #17246 (comment) The other option is to use a dynamic block, but granted you may not be able to that if you're using a JS library. |
We should document the entire |
This is an oldie but I stumbled upon it while searching how to have an Id associated to a block. |
If anyone needs it here's an example of how you can persist a block's generated clientId, although it should be noted this can't be used for any dynamic function Edit( { attributes, setAttributes, clientId } ) {
// Set the uniqueId from clientId only if not already set.
useEffect( () => {
if ( ! attributes.uniqueId ) {
setAttributes( { uniqueId: clientId } );
}
}, [ attributes.uniqueId, clientId ] );
// ...
}
// ... in the save function
function save( { attributes } ) {
return <div data-uuid={ attributes.uniqueId }></div>
} |
I was using something similair like this. A good thing to note is, that when you duplicate post-X. The copied results have the same uniqueId. What results in a duplicate IDs (front-end). |
setting an attribute in a useEffect makes the save button light up... which means you have to save the page.. which means the post is considered dirty, and some people use that as an indicator that something has happened. It's probably bad practice to set an attribute and create a condition where the editor always has to be saved. In my mind this should be avoided.... but then, how can we get the client Id in save(js) or render(php)??? |
useEffect only triggers when you initialize/ add the block for the first time. Adding a block makes the save option enabled. This is the code I'm currently using in production and works as expected.
What is isBlockIdReserved?
How does it work?
On edit
Additional notes Settings an default value for the attribute: blockId, makes it work unreliable. And the easiest way for me was the remove the default value (undefined, NULL, false all gave strange attrifact while working with the block). ClientID inside save/render instances?
For render.php it works the same: I hope this helps you a bit with your problem. |
I just had a idea. Maybe this is what is happing for you. Just did a "standalone" test with my code (standalone plugin that adds the blocks to the environment). After reloading the page, with the custom block with blockId. The save option is disabled. After I add Yoast SEO and enable it. The save function always stays enabled ( this is something I noticed while building my plugin ). |
anytime a setAttribute is fired the save button lights up. it depends on the conditionals... in your example the blockId is already set in the saved attributes when the page loads again and so it doesn't light up on editor init. but if you needed to keep that clientID up to date on each editor reload it would fire on each editor reload. Without You even say "although it should be noted this can't be used for any dynamic wp.data functions like getBlocks()". which is the heart of my issue. |
I never said "dynamic wp.data functions like", this was someone else and I quoted his comment and gave a reaction too this statement. Not that it matters much. Also did you try to get the block with the stored blockId, this doens't work because the clientId changes constantly. Every page load new clientIDs are generated for the given blocks. Not sure if this helps a bit, but this is just something that came to my mind. |
I need to save the clientId to some block attributes whenever the editor starts up. I'd like to make it available via context, but context seems to require you to save an attribute and since the clientID does change on every reload that attribute needs to be saved on every reload of the editor. Setting context in a react way doesn't work #50633 . I believe it's not possible, exploring other methods. |
This is correct, the clientID changes every cycle. And storing it for later use is a "work-around". But this value doesn't help you get any block information, it's more for your own use. |
What's the remaining problem here? Can we close this issue? |
I have been scouring the internet for documentation on this. The WordPress documentation focuses on all the methods you can run if you have the clientId, but I can't figure our how to get that ID dynamically during the edit or save portion of my block. I'm trying to use the following code to access all my innerblock data/attributes but I need the clientId first:
wp.data.select('core/block-editor').getBlocks(clientId);
Can anyone point me in the right direction on this?
The text was updated successfully, but these errors were encountered: