After reading this I started moving my CPT (namely “evento”) to a new plugin (it was in a Bones-based theme before). The new plugin is basically a fresh WP Boilerplate Plugin folder, where I just renamed all the “plugin-name” occurences to “dogmaweb”, which is the name of my new plugin. Please keep in mind I’m a beginner when it comes to WordPress coding.

I’ve copied three files from the theme to the plugin folder: the first for CPT registration (library/evento-post-type.php), the second for the single post (/single_evento-type.php) and the third for the CPT archive (/archive_evento-type.php). I’ve kept the Bones directory structure for those files in the plugin too.

I then hooked the CPT registration into my plugin class (includes/class-dogmaweb.php) adding the following to its load_dependencies() function:

require_once plugin_dir_path( dirname( __FILE__ ) ) . 'library/evento-post-type.php';

This file registers the new CPT with a call to:

add_action( 'init', 'evento_post_type');

and then it registers two taxonomies too:

register_taxonomy( 'evento_cat', array('evento_type') ...
register_taxonomy( 'evento_tag', array('evento_type') ...

After moving the files from the theme to the plugin, the whole site stopped working. I only get a “500 Internal server error” from Apache. Apache error log shows:

[Tue Dec 01 15:16:29.941885 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP Fatal error:  Call to a member function add_rewrite_tag() on null in /home/lucio/workspace/netbeans/wpsite/wp-includes/rewrite.php on line 54
[Tue Dec 01 15:16:29.941933 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP Stack trace:
[Tue Dec 01 15:16:29.941941 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   1. {main}() /home/lucio/workspace/netbeans/wpsite/index.php:0
[Tue Dec 01 15:16:29.941955 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   2. require() /home/lucio/workspace/netbeans/wpsite/index.php:17
[Tue Dec 01 15:16:29.941960 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   3. require_once() /home/lucio/workspace/netbeans/wpsite/wp-blog-header.php:12
[Tue Dec 01 15:16:29.941964 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   4. require_once() /home/lucio/workspace/netbeans/wpsite/wp-load.php:37
[Tue Dec 01 15:16:29.941968 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   5. require_once() /home/lucio/workspace/netbeans/wpsite/wp-config.php:91
[Tue Dec 01 15:16:29.941972 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   6. include_once() /home/lucio/workspace/netbeans/wpsite/wp-settings.php:215
[Tue Dec 01 15:16:29.941975 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   7. run_dogmaweb() /home/lucio/workspace/netbeans/wpsite/wp-content/plugins/dogmaweb/dogmaweb.php:75
[Tue Dec 01 15:16:29.941980 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   8. Dogmaweb->__construct() /home/lucio/workspace/netbeans/wpsite/wp-content/plugins/dogmaweb/dogmaweb.php:71
[Tue Dec 01 15:16:29.941985 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP   9. Dogmaweb->load_dependencies() /home/lucio/workspace/netbeans/wpsite/wp-content/plugins/dogmaweb/includes/class-dogmaweb.php:74
[Tue Dec 01 15:16:29.941989 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP  10. require_once() /home/lucio/workspace/netbeans/wpsite/wp-content/plugins/dogmaweb/includes/class-dogmaweb.php:122
[Tue Dec 01 15:16:29.941993 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP  11. register_taxonomy() /home/lucio/workspace/netbeans/wpsite/wp-content/plugins/dogmaweb/library/evento-post-type.php:91
[Tue Dec 01 15:16:29.941997 2015] [:error] [pid 11123] [client 127.0.0.1:48572] PHP  12. add_rewrite_tag() /home/lucio/workspace/netbeans/wpsite/wp-includes/taxonomy.php:389

However the same code in the Bones theme works correctly. I understand that WP is trying to call add_rewrite_tag() on a null reference, e.g. $this is null, and that makes me suspect I should hook my “evento” post type taxonomies registration somewhere else. I only don’t know where. Please note that line 91 in my code matches the first taxonomy registration, but I’m pretty sure the same would happen for the second too.

1
1

While I don’t like taking credit for someone else’s answer, I also don’t like leaving a question without answer when the answer does exist and it’s already been given.
I hope Milo won’t mind if I copy the answer from his comment.

The register_taxonomy calls should be hooked to init.

Leave a Reply

Your email address will not be published. Required fields are marked *