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_register_script doesn't work in wp_enqueue_scripts #7084

Closed
manake opened this issue Jun 1, 2018 · 7 comments
Closed

wp_register_script doesn't work in wp_enqueue_scripts #7084

manake opened this issue Jun 1, 2018 · 7 comments
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Status] Needs More Info Follow-up required in order to be actionable.

Comments

@manake
Copy link

manake commented Jun 1, 2018

This doesn't work:

function slug_gutenberg_scripts() {
	wp_register_script( 'gutenberg-xxx', plugins_url( 'js/gutenberg-xxx.js', __FILE__ ), array( 'wp-blocks', 'wp-element' ) );

	$xxx = array(
		'categories' => get_terms(array(
			'taxonomy' => 'custom_post_type_category',
			'hide_empty' => false,
		))
	);
	wp_localize_script( 'gutenberg-xxx', 'gutenbergXxx', $xxx );
}
add_action( 'wp_enqueue_scripts', 'slug_gutenberg_scripts' );

function slug_gutenberg_blocks() {
	register_block_type( 'slug/xxx', array(
		'script' => 'gutenberg-xxx',
	) );
}
add_action( 'init', 'slug_gutenberg_blocks' );

This doesn't work:

function slug_gutenberg_blocks() {
	wp_register_script( 'gutenberg-xxx', plugins_url( 'js/gutenberg-xxx.js', __FILE__ ), array( 'wp-blocks', 'wp-element' ) );

	$xxx = array(
		'categories' => get_terms(array(
			'taxonomy' => 'custom_post_type_category',
			'hide_empty' => false,
		))
	);
	wp_localize_script( 'gutenberg-xxx', 'gutenbergXxx', $xxx );

	register_block_type( 'slug/xxx', array(
		'script' => 'gutenberg-xxx',
	) );
}
add_action( 'init', 'slug_gutenberg_blocks' );

Reason: var gutenbergXxx = {"categories":{"errors":{"invalid_taxonomy":["Invalid taxonomy."]},"error_data":[]}};

Is the only option to make it work to use add_action( 'init', 'slug_gutenberg_blocks', 11 );?

Expected: Being able to enqueue scripts in regular wp_enqueue_scripts to have access to custom post types etc.

@ocean90
Copy link
Member

ocean90 commented Jun 2, 2018

How are you registering your custom taxonomy? You have to make sure that it's registered before using it. I'm assuming it's the default priority (10) but called after add_action( 'init', 'slug_gutenberg_blocks' ); which is why 11 works.

@ocean90 ocean90 added the [Status] Needs More Info Follow-up required in order to be actionable. label Jun 2, 2018
@manake
Copy link
Author

manake commented Jun 2, 2018

Yes, that's correct.

A plugin has add_action( 'init', 'slug_gutenberg_blocks' ); and a theme which registers a post type has add_action( 'init', 'register_that_post_type' );.

While I can use 11 priority to fix this, shouldn't it be possible to use wp_enqueue_scripts hook to register scripts? This hook happens a lot later after init and it would offer access to whatever is missing in init.

I don't know if this is a bug but it was unexpected by me that wp_enqueue_scripts did not work.

@danielbachhuber
Copy link
Member

@manake Were you able to track a resolution for this down? If not, can you share a fully-functional (not sample) code snippet that reproduces the issue?

@manake
Copy link
Author

manake commented Jun 15, 2018

You already have the full code. You only don't have a register_post_type call but you can register it yourself like this:

  1. What I have in the first post was in a plugin.
  2. I was registering a post type in another plugin (or theme) with 10 priority init hook.
  3. It just happens that when both have 10 priority then there's a chance that the post type won't be available yet and my get_terms will fail (see first post).
  4. Hence I tried to use wp_enqueue_scripts to definitely have post types available. But that didn't work.
  5. So I was forced to use add_action( 'init', 'slug_gutenberg_blocks', 11 ); to solve this.

Again: I don't know if this is a bug. If I can't use wp_enqueue_scripts to register my Gutenberg scripts then I consider this to be a bug for now.

The documentation uses init hook for wp_register_script. Are you requiring register_block_type scripts to be in init?

https://wordpress.org/gutenberg/handbook/blocks/writing-your-first-block-type/

@designsimply designsimply added [Type] Help Request Help with setup, implementation, or "How do I?" questions. Needs Technical Feedback Needs testing from a developer perspective. and removed [Status] Needs More Info Follow-up required in order to be actionable. labels Sep 13, 2018
@tbleckert
Copy link

tbleckert commented Oct 25, 2018

Isn't the correct way to use enqueue_block_assets and some (or all) of the dependencies [ 'wp-i18n', 'wp-edit-post', 'wp-element', 'wp-editor', 'wp-components', 'wp-data', 'wp-plugins', 'wp-edit-post', 'wp-api' ]?

@designsimply designsimply added [Status] Needs More Info Follow-up required in order to be actionable. and removed [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Jan 25, 2019
@designsimply
Copy link
Member

@manake since it has been a few months since this issue was started, may I ask if you're still having trouble and if the code examples are still good? If yes, I will see what I can do to help test and will get additional help with that if I need it. Thanks so much!

@youknowriad
Copy link
Contributor

Closing as stale, let's reopen if there's are reproducible steps/actionable items.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Status] Needs More Info Follow-up required in order to be actionable.
Projects
None yet
Development

No branches or pull requests

6 participants