阅读后this 我开始移动我的CPT (即“evento”)添加到一个新插件(它位于Bones-基于之前的主题)。新插件基本上是全新的WP Boilerplate Plugin 文件夹中,我刚刚将所有出现的“插件名称”重命名为“dogmaweb”,这是我新插件的名称。请记住,我是WordPress编码的初学者。
我已经将三个文件从主题复制到插件文件夹:第一个用于CPT注册(library/evento post-type.php),第二个用于单篇文章(/single\\u evento-type.php),第三个用于CPT归档文件(/archive\\u evento-type.php)。我也在插件中保留了这些文件的Bones目录结构。
然后,我将CPT注册连接到我的插件类(includes/class dogmaweb.php)中,并将以下内容添加到其load\\u dependencies()函数中:
require_once plugin_dir_path( dirname( __FILE__ ) ) . \'library/evento-post-type.php\';
此文件注册新CPT,并调用:
add_action( \'init\', \'evento_post_type\');
然后它还注册了两种分类法:
register_taxonomy( \'evento_cat\', array(\'evento_type\') ...
register_taxonomy( \'evento_tag\', array(\'evento_type\') ...
将文件从主题移动到插件后,整个站点停止工作。我只收到Apache的“500内部服务器错误”。Apache错误日志显示:
[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
但是,“骨骼”主题中的相同代码可以正常工作。我知道WP试图对null引用调用add\\u rewrite\\u tag(),例如$this为null,这让我怀疑我应该将我的“evento”post-type分类法注册挂接到其他地方。我只是不知道在哪里。请注意,我的代码中的第91行与第一个分类法注册匹配,但我很确定第二个也会发生同样的情况。