how to create custom plugin hook in wordpress?
make new folder in plugin after whatever you want your folder name, remember that file name and folder name same .
make folder in this folder wp-content/plugins/
exmaple : wp-content/plugins/viral/viral.php
viral is your plugin folder and viral.php is your plugin file
now add code in viral.php file.
viral.php
<?php
/**
* @package viral
*/
/*
plugin Name: vira-plugin
plugin URI: https://vdcodin.blogspot.com/
Description: this is my custom plugin made by your name other wise details about plugin
Version: 4.1.7
Author: viral
Author URI: https://vdcodin.blogspot.com/
License: GPLv2 or later
text Domain: viral
*/
add_action('admin_menu','viral_menu');
function viral_menu(){
add_menu_page(
'viral', // page title
'viral', // menu title
'manage_options', // by-default admin site
'dashboard', // this is called page slug
'my_function',// callback function which we called
'dashicons-dashboard', // this is our icon
6 // this is our plugin position
);
//we have only one sub menu then why its showing 2?
//lets make this one
add_submenu_page(
'dashboard', // parent slug beause we need submenu of this menu
'first submenu', // page title
'first submenu', // menu title
'manage_options', // by-default admin site
'dashboard', // menu slug
'my_function' //call function which we click on sub menu and open that page
);
add_submenu_page(
'dashboard', // parent slug beause we need submenu of this menu
'second submenu', // page title
'second submenu', // menu title
'manage_options', // by-default admin site
'second-submenu', // menu slug
'second_submenu' //call function which we click on sub menu and open that page
);
add_submenu_page(
'dashboard', // parent slug beause we need submenu of this menu
'third submenu', // page title
'third submenu', // menu title
'manage_options', // by-default admin site
'third-submenu', // menu slug
'third_submenu' //call function which we click on sub menu and open that page
);
}
//directory
define("PLUGIN_DIR_PATH", PLUGIN_DIR_PATH(__FILE__));
function my_function(){
include_once PLUGIN_DIR_PATH . '/include/first.php';
}
function second_submenu(){
include_once PLUGIN_DIR_PATH . '/include/second.php';
}
function third_submenu(){
include_once PLUGIN_DIR_PATH . '/include/third.php';
}
//now we make table and page automatically when we activat our plugin
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
register_activation_hook(__FILE__,'table_activate');
register_activation_hook(__FILE__, 'create_page');
//deactivate hook
//drop table when plugindelete
function table_activate(){
global $wpdb;
$sql = "CREATE TABLE `wp_registration` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(30) NOT NULL,
`last_name` varchar(30) NOT NULL,
`email` varchar(30) NOT NULL,
`password` varchar(15) NOT NULL,
`phone_no` bigint(10) NOT NULL,
`reg_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1";
$wpdb->query($sql);
}
// when we click on activate table made automatically when our plugin activate
//now we make our page too
function create_page()
{
$page = array();
$page['post_title'] = "custom page";
$page['post_content'] = "this is our custom page when you active out plugin this page also created with plugin";
$page['post_statud'] = "publish";
$page['post_slug'] = "custom-page";
$page['post_type'] = "page";// this slug make you choice either page or post which is ?
$post_id = wp_insert_post($page);
add_option("custom_plugin_page", $post_id);
}
No comments:
Post a Comment
If You have any doubt, or want to know about something, please let me know