7

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.

migrated from stackoverflow.com Dec 2 '15 at 20:34

This question came from our site for professional and enthusiast programmers.

  • 4
    are your register_taxonomy calls also inside the evento_post_type function? taxonomy registration should be hooked to init. – Milo Dec 2 '15 at 20:44
  • @Milo that was the problem. If you turn it into an answer I can accept it, thanks. – Lucio Crusca Dec 3 '15 at 7:20
19

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.