Skip to content
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

wp.data.subscribe generates Uncaught TypeError after WP v5.0 #12702

Closed
Oxibug opened this issue Dec 8, 2018 · 6 comments
Closed

wp.data.subscribe generates Uncaught TypeError after WP v5.0 #12702

Oxibug opened this issue Dec 8, 2018 · 6 comments
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@Oxibug
Copy link

Oxibug commented Dec 8, 2018

Describe the bug
I'm using wp.data.subscribe function to listen on changes made on PostFormat select element, but after upgrade to WordPress v5.0 and the Gutenberg plugin became inactive this code generates {Uncaught TypeError: Cannot read property 'subscribe' of undefined}

The Code like that:

wp.data.subscribe(function () {
 currentFormat = wp.data.select('core/editor').getEditedPostAttribute('format');
});
@youknowriad
Copy link
Contributor

It looks like you need to add wp-data and wp-editor to your dependencies here.
So anytime you use a wp.* or a given data store like core/editor here, you need to make sure to add the corresponding scripts as dependencies when registering your custom script. This ensures your script loads after these variables are available.

@youknowriad youknowriad added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Dec 8, 2018
@youknowriad
Copy link
Contributor

I'm closing for now, let me know if it doesn't fix the issue for you. Thanks

@Oxibug
Copy link
Author

Oxibug commented Dec 8, 2018

Where's the documentation for that? and Do you mean deps while including script with wp_enqueue_script?? I've tried it but the same error

Thanks bro

@youknowriad
Copy link
Contributor

In this page https://wordpress.org/gutenberg/handbook/designers-developers/developers/tutorials/block-tutorial/writing-your-first-block-type/#enqueuing-block-scripts it's mentioned at the end of the section.

And yes, add the dependencies to your wp_enqueue_script or wp_register_script call.

@Oxibug
Copy link
Author

Oxibug commented Dec 8, 2018

My bad bro, It works like Charm, I just need to include wp-data and wp-editor in deps array, But how should any developer know that??

Thanks bro

@Oxibug
Copy link
Author

Oxibug commented Dec 8, 2018

Actually I was using wp-blocks but I've removed it after it causes an issue for scripts in front-end, The stylesheet didn't included then I modified all codes in the back-end too.

But now it works in back-end but not in the front-end, But it was working when the plugin active in both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

2 participants