I am building a very basic plug-in, the template I have created works beautifully. However I am unable to get the custom post type to be built and displayed in the admin panel on activation.
The thing that is weird to me, if I add the trigger in a construct method, the post type is created and displayed in the admin panel.
On activation I receive the echo 'Plug-in Activated!', likewise for deactivation. I just cant figure out why this wouldn't work within the activate method.
Please can someone direct me as to how I can trigger the creation of this post type on activation?
Here is my code:
<?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;
init
, on every page load.