为什么我的自定义POST类型在插件激活时没有激活?

时间:2020-08-19 作者:Jason Is My Name

我正在构建一个非常基本的插件,我创建的模板效果很好。然而,我无法在激活时在管理面板中生成和显示自定义帖子类型。

我觉得奇怪的是,如果我在构造方法中添加触发器,就会创建post类型并显示在管理面板中。

激活时,我收到回音“插件已激活!”,停用时也是如此。我只是不明白为什么在activate方法中这不起作用。

请有人告诉我如何在激活时触发此帖子类型的创建?

这是我的代码:

<?php
/**
 * @package WebsiteLister
 */
/*
Plugin Name: Website Lister
Plugin URI: https://ME.co.uk/website-lister
Description: Helper enabling the ability to log website URLs into a beautiful slider!
Version: 1.0.0
Author: ME
Author URI: https://ME.co.uk
License: GPLv2 or later
Text Domain: website-lister
*/

/*
Copyright (C) 2020 Jason Dupley

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

defined(\'ABSPATH\') or die(\'Get out of here, sneaky human!\');

if (!class_exists(\'WebsiteLister\')) :

    class WebsiteLister
    {

        public $plugin;

        function __construct() {
            $this->plugin = plugin_basename( __FILE__ );
            //$this->create_post_type();
        }

        // Register Enqueued Scritps
        function register_scripts() {
            add_action(\'admin_enqueue_scripts\', array($this, \'enqueue_admin_scripts\'));
            add_action(\'wp_enqueue_scripts\', array($this, \'enqueue_frontend_scripts\'));
        }

        // Register Admin Area
        function register() {
            add_action(\'admin_menu\', array($this, \'add_admin_pages\'));
            add_filter("plugin_action_links_$this->plugin", array( $this, \'settings_link\'));
        }

        // Activate
        function activate() {
            //echo \'Plug-in Activated!\';

            // Create Custom Post Type
            $this->create_post_type();

            // Flush rewrite rules
            flush_rewrite_rules();
        }

        // Deactivate
        function deactivate() {
            //echo \'Plug-in Deactivated!\';

            // Flush rewrite rules
            flush_rewrite_rules();
        }

        public function settings_link($links) {
            $settings_link = \'<a href="admin.php?page=websites_plugin">Settings</a>\';
            array_push($links, $settings_link);
            return $links;
        }

        // Create Admin Pages
        public function add_admin_pages() {
             add_menu_page( \'Websites Plug-in\', \'Websites Settings\', \'manage_options\', \'websites_plugin\', array($this, \'admin_index\'), \'dashicons-admin-site-alt3\', 100 );
        }

        // Create Admin Index
        public function admin_index() {
            // Require Template
            require_once plugin_dir_path( __FILE__ ) . \'templates/admin.php\'; 
        }

        // Create Custom Post Type
        protected function create_post_type() {
            add_action(\'init\', array($this, \'custom_post_type\'));
        }

        // Register Websites Custom Post Type
        function custom_post_type() {
            register_post_type(\'website\', [\'public\' => true, \'label\' => \'Websites\']);
        }

        // Enqueue Admin Scripts
        function enqueue_admin_scripts() {
            // Styles
            wp_enqueue_style(\'websitelisteradminstylesheet\', plugins_url( \'/css/wl-admin-style.css\', __FILE__ ));
            wp_enqueue_script(\'websitelisteradminscript\', plugins_url( \'/js/wl-admin-script.js\', __FILE__ ));
        }

        // Enqueue Front-end Scripts
        function enqueue_frontend_scripts() {
            // Styles
            wp_enqueue_style(\'websitelisterstylesheet\', plugins_url( \'/css/wl-style.css\', __FILE__ ));
            wp_enqueue_script(\'websitelisterscript\', plugins_url( \'/js/wl-script.css\', __FILE__ ));
        }

    }

    if (class_exists(\'WebsiteLister\')) :

        $websiteLister = new WebsiteLister();
        $websiteLister->register_scripts();
        $websiteLister->register();

    endif;

    // activation
    register_activation_hook( __FILE__, array($websiteLister, \'activate\'));

    // deactivation
    register_deactivation_hook( __FILE__, array($websiteLister, \'deactivate\'));

    // uninstall
    // uninstall.php located within the plug-in root folder handles this process

endif;

1 个回复
SO网友:Tom J Nowell

不是这样的register_activation_hook 作品

register_activation_hook 仅在插件激活时调用一次。然而,CPT需要在每个请求上注册,而不仅仅是在插件被激活时。

这是因为WP没有在数据库中存储CPT的列表,它是在运行时由PHP代码生成的。

因此,请在init 钩自定义分类法也是如此。

至于激活钩子,如果需要,可以使用它来创建自定义数据库表,但激活钩子从一开始就不是超级可靠的。大多数插件都不使用它。