Register custom post type from plugin. WordPress example open

Register custom post type from plugin. WordPress example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

After creating your plugin, if its functionality requires custom post types.You can create them immediately after activating your plugin. Use this example of implementing PHP classes for your needs:

<?php
/*
Plugin Name: My Plugin
Plugin URI: https://mypluginsite.com/
Description: One of best plugins in the planet
Version: 1.0
Author: John
Author URI: https://facebook.com/john
Licence: GPLv2 or later
Text Domain: myplugin
Domain Path: /lang
 */

if (!defined('ABSPATH')) {
    die;
}
 
 
 
class myPlugin
{
    public function custom_post_type(){
        register_post_type('cars',
        array(
            'public' => true,  
            'has_archive' => true,
            'rewrite' => array('slug' => 'cars'),
            'label' => 'Cars',
            'supports' => array('title', 'editor', 'thumbnail'),
        ));
    }
 
    public function register(){
        add_action('init', [$this, 'custom_post_type']);
    }
 

 
    public static function activation(){
 
        flush_rewrite_rules(); //rewrite site links
    }
    public static function deactivation(){
 
        flush_rewrite_rules(); //rewrite site links
    }
}
 

if (class_exists('MyPlugin')) {
    $myPlugin = new myPlugin();
    $myPlugin->register();
}
 
register_activation_hook(__FILE__, array($myPlugin, 'activation'));
register_deactivation_hook(__FILE__, array($myPlugin, 'deactivation'));


If you want to delete your custom entries after removing the plugin, create a unistall.php file in the plugin folder and add the code below

$cars = get_posts(array('post_type' => 'cars', 'numberposts' => -1, ));
foreach ($cars as $car) {
    wp_delete_post( $cars->ID, true )
}

It checks if the constant ABSPATH is defined. If it’s not defined, the script is terminated with die;. ABSPATH is a constant in WordPress that represents the server’s filesystem path to the WordPress directory.

It defines a class named myPlugin which contains several methods.

  • custom_post_type: This method registers a custom post type called “cars.” It specifies various options for this post type, such as making it public, enabling archiving, defining a URL slug, and specifying support for title, editor, and thumbnail.
  • register: This method adds an action hook to WordPress’s ‘init‘ event, which, when triggered, calls the custom_post_type method. This is used to register the custom post type during WordPress initialization.
  • activation: A static method that is used as a callback for the register_activation_hook function. It flushes the rewrite rules, which is necessary when creating custom post types to ensure proper URL rewriting.
  • deactivation: Similar to activation, but for deactivation. It also flushes the rewrite rules.

The code then checks if the class ‘MyPlugin‘ (with a capital ‘M’) exists. This check is not necessary since the class name is ‘myPlugin’ (with a lowercase ‘m’).

If the class ‘MyPlugin‘ exists, it creates an instance of the myPlugin class, registers the custom post type using the register method, and hooks the activation and deactivation methods to the respective WordPress activation and deactivation hooks.

0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation