我正在构建一个非常基本的插件,我创建的模板效果很好。然而,我无法在激活时在管理面板中生成和显示自定义帖子类型。
我觉得奇怪的是,如果我在构造方法中添加触发器,就会创建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;