Оценок пока нет Добвление административной части плагина WordPress

Представим, что требуется написать плагин и у него подразумевается создание административной части и будем считать, что у административной части есть главное меню и подменюшки в левой боковой части.

<?php
/**
 * Plugin Name: WSofter | My Plugin Name
 * Plugin URI: https://wsofter.com/projects
 * Description: Description of My Plugin Name for WordPress
 * Version: 1.0.0
 * Author: David Amirkhanov
 * Author URI: https://wsofter.com
 */

//Регистрируем функцию меню
add_action('admin_menu', 'plugin_setup_menu');
 
function plugin_setup_menu(){
	//Иконка к меню
	$icon_base64 = 'PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PCFET0NUWVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkIj48c3ZnIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIHZpZXdCb3g9IjAgMCAyMTM0IDIxMzQiIHZlcnNpb249IjEuMSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSIgeG1sbnM6c2VyaWY9Imh0dHA6Ly93d3cuc2VyaWYuY29tLyIgc3R5bGU9ImZpbGwtcnVsZTpldmVub2RkO2NsaXAtcnVsZTpldmVub2RkO3N0cm9rZS1saW5lY2FwOnJvdW5kO3N0cm9rZS1saW5lam9pbjpyb3VuZDtzdHJva2UtbWl0ZXJsaW1pdDoxLjU7Ij48Zz48cmVjdCB4PSIyODIuNTc2IiB5PSI4OTAuODg2IiB3aWR0aD0iNzc1Ljc1OCIgaGVpZ2h0PSI3NzguNzgxIiBzdHlsZT0iZmlsbDojZmZmO2ZpbGwtb3BhY2l0eTowO3N0cm9rZTojMDAxZGZmO3N0cm9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kiIHN0eWxlPSJmaWxsOm5vbmU7c3Ryb2tlOiMwMDFkZmY7c3Ryb2tlLXdpZHRoOjY2LjY3cHg7Ii8+PHJlY3QgeD0iMTc3OC42MSIgeT0iMTgxMy43IiB3aWR0aD0iMTQyLjQxMiIgaGVpZ2h0PSIxNDEuNDc5IiBzdHlsZT0iZmlsbDpub25lO3N0cm9rZTojMDAxZGZmO3N0cm9rZS13aWR0aDo2Ni42N3B4OyIvPjwvZz48L3N2Zz4=';

	//Формируем иконку
	$icon_data_uri = 'data:image/svg+xml;base64,' . $icon_base64;

	$page_title = 'End Points';
	$menu_title = 'Custom API';
	$icon_data_uri;
	$position   = '20';
	$capability = 'manage_options';
	$menu_slug  = 'main_page';
	$function   = 'get_page_main';

  //Добавляем главное меню
	add_menu_page(
		$page_title,
		$menu_title,
		$capability,
		$menu_slug,
		$function,
		$icon_data_uri,
		$position
	);
  //Добавляем подменю
    add_submenu_page(
        'main_page',
        'Settings', //page title
        'Settings', //menu title
        'edit_themes', //capability,
        'settings',//menu slug
        'get_page_settings' //callback function
    );
}
//Функция главного меню
function get_page_main(){
    echo "<h1>Main!</h1>";
}
//Функция подменю
function get_page_settings(){
    echo "<h1>Settings!</h1>";
}

Пожалуйста, оцените материал

WebSofter

Web - технологии