CPT_Core icon indicating copy to clipboard operation
CPT_Core copied to clipboard

Auto-loader like CMB2

Open JayWood opened this issue 8 years ago • 8 comments

Would love to see an auto-loader like CMB2 that loads the latest version always.

JayWood avatar Jun 19 '17 15:06 JayWood

This is probably all you need: http://jtsternberg.github.io/wp-lib-loader/

Let me know if you have questions.

jtsternberg avatar Jun 19 '17 15:06 jtsternberg

Basically, you'd replace the current contents of the CPT_Core.php file with the contents of the loader (which you can generate at that site), and then move the current CPT_Core.php file to lib folder (or anywhere really). This ensures all current code loading that file will still work, but the lib loader will kick in.

jtsternberg avatar Jun 19 '17 15:06 jtsternberg

@jtsternberg from left field 😄 we were looking into this today and got side-tracked.

JayWood avatar Jun 20 '17 02:06 JayWood

Hi @jtsternberg I'm trying to include WP Lib Loader in Taxonomy_Core and facing a problem with functions visibility. Here's the code I have so far: https://github.com/WebDevStudios/Taxonomy_Core/commit/179a122c3856f78a2ea9170a6e58232b93ba5418 so when I load Taxonomy_Core with this:

require_once 'Taxonomy_Core/Taxonomy_Core.php';

$genres = register_via_taxonomy_core( array(
	__( 'Genre', 'your-text-domain' ), // Singular
	__( 'Genres', 'your-text-domain' ), // Plural
	'genre' // Registered name
) );`

both auto-loader and library seems to be loaded, though register_via_taxonomy_core() call fails with Fatal error: Uncaught Error: Call to undefined function register_via_taxonomy_core() I'm not sure why it happens and if I missed something. Could you please take a look?

Thanks, Pavel

PavelK27 avatar Jun 21 '17 20:06 PavelK27

Your require_once needs to happen outside of any hook. Do you have it that way?

jtsternberg avatar Jun 21 '17 21:06 jtsternberg

This should work:

require_once 'Taxonomy_Core/Taxonomy_Core.php';

function myprefix_register_taxonomies() {
	$genres = register_via_taxonomy_core( array(
		__( 'Genre', 'your-text-domain' ), // Singular
		__( 'Genres', 'your-text-domain' ), // Plural
		'genre' // Registered name
	) );
}

add_action( 'taxonomy_core_load', 'myprefix_register_taxonomies', TAXONOMY_CORE_LOADED + 1 );

jtsternberg avatar Jun 21 '17 23:06 jtsternberg

@jtsternberg yes, that's helped. Thanks a lot! :+1: @JayWood with this change we'll need to update all the documentation for the library and flag it as important, because library initiation method had changed.

PavelK27 avatar Jun 22 '17 13:06 PavelK27

@JayWood I have added the same update to CPT Core library, though I can't think of any usage examples inside of a class for Readme file. We probably will have to change library's construct to allow class initiation on its load? Otherwise there's no way for classes to extend it after its files were incuded in PHP.

PavelK27 avatar Jun 22 '17 18:06 PavelK27