|
1 | 1 | # Wordpress Snippets for Sublime Text 3 |
2 | 2 | Simple snippets for Wordpress elements, such as meta boxes, pages, subpages, post types, taxonomies, etc... |
3 | | -##wp_create_plugin |
4 | | -Snippet for creating wrapper for plugins |
5 | | -``` |
6 | | -/* Plugin Name: New plugin |
7 | | - * Description: Description |
8 | | - * Version: 1.0 |
9 | | - * Author: Me |
10 | | - */ |
11 | 3 |
|
12 | | -if (!class_exists('MyPluginClass')) { |
13 | | - class MyPluginClass { |
14 | | - public function __construct() { |
15 | | - |
16 | | - } |
17 | | - } |
| 4 | +# How to use |
| 5 | +In PHP scope write `wp_` and find snippet you want to use. Each snippet is class-based, so it cannot be used outside of class (e.g. in functions.php). |
18 | 6 |
|
19 | | - new MyPluginClass(); |
20 | | -} |
21 | | -``` |
22 | | -##wp_add_menu_page |
23 | | -Snippet for creating menu |
24 | | -``` |
25 | | -// Put this into your __construct function |
26 | | -add_action('admin_menu', array($this, 'add_menu_slug_menu_page')); |
27 | | -
|
28 | | -public function add_menu_slug_menu_page() { |
29 | | - add_menu_page('Page title', 'Menu title', 'manage_options', 'menu_slug', array($this, 'menu_slug_menu_callback'), 'dashicons-icon', 0); |
30 | | -} |
31 | | -
|
32 | | -public function menu_slug_menu_callback() { |
33 | | - // Content goes here |
34 | | - |
35 | | -} |
36 | | -``` |
37 | | -##wp_add_submenu_page |
38 | | -Snippet for creating submenu |
39 | | -``` |
40 | | -// Put this into your __construct function |
41 | | -add_action('admin_menu', array($this, 'add_submenu_slug_submenu_page')); |
42 | | -
|
43 | | -public function add_submenu_slug_submenu_page() { |
44 | | - add_submenu_page('parent_slug', 'Page title', 'Menu title', 'manage_options', 'submenu_slug', array($this, 'submenu_slug_menu_callback')); |
45 | | -} |
46 | | -
|
47 | | -public function submenu_slug_submenu_callback() { |
48 | | - // Content goes here |
49 | | - |
50 | | -} |
51 | | -``` |
52 | | -##wp_add_menu_with_submenu |
53 | | -Snippet for creating menu with one submenu |
54 | | -``` |
55 | | -// Put this into your __construct function |
56 | | -add_action('admin_menu', array($this, 'add_menu_slug_menu_with_submenu_page')); |
57 | | -
|
58 | | -public function add_menu_slug_menu_with_submenu_page() { |
59 | | - add_menu_page('Page title', 'Menu title', 'manage_options', 'menu_slug', array($this, 'menu_slug_menu_callback'), 'dashicons-icon', 0); |
60 | | - add_submenu_page('menu_slug', 'Page title', 'Menu title', 'manage_options', 'menu_slug', array($this, 'menu_slug_menu_callback')); |
61 | | - add_submenu_page('menu_slug', 'Page title', 'Submenu title', 'manage_options', 'submenu_slug', array($this, 'menu_slug_submenu_callback')); |
62 | | -} |
63 | | -
|
64 | | -public function menu_slug_menu_callback() { |
65 | | - // Menu content goes here |
66 | | - |
67 | | -} |
68 | | -
|
69 | | -public function menu_slug_submenu_callback() { |
70 | | - // Submenu content goes here |
71 | | -} |
72 | | -``` |
73 | | -##wp_add_meta_box |
74 | | -Snippet for creating metabox with callback and save function |
75 | | -``` |
76 | | -// Put this into your __construct function |
77 | | -add_action('add_meta_boxes', array($this, 'add_mymetaboxname_metabox')); |
78 | | -add_action('save_post_post_type_slug', array($this, 'save_mymetaboxname_metabox_data')); |
79 | | -
|
80 | | -public function add_mymetaboxname_metabox { |
81 | | - add_meta_box('mymetaboxname', 'Title', array($this, 'mymetaboxname_metabox_callback'), 'post_type_slug', 'normal', 'low'); |
82 | | -} |
83 | | -
|
84 | | -public function mymetaboxname_metabox_callback($post) { ?> |
85 | | - <label>Enter your first name:</label> |
86 | | - <input type="text" name="mymetaboxname[first_name]" /> |
87 | | - <label>Enter your last name:</label> |
88 | | - <input type="text" name="mymetaboxname[last_name]" /> |
89 | | -<?php } |
90 | | -
|
91 | | -public function save_mymetaboxname_metabox_data($post_id) { |
92 | | - if (!isset($_POST['mymetaboxname'])) { |
93 | | - return $post_id; |
94 | | - } |
95 | | -
|
96 | | - foreach ($_POST['mymetaboxname'] as $key => $field) { |
97 | | - update_post_meta($post_id, $key, $field); |
98 | | - } |
99 | | -} |
100 | | -``` |
| 7 | +# List of snippets |
| 8 | +- wp_plugin_wrapper - class-based wrapper for plugin development |
| 9 | +- wp_meta_box - adds meta box for post type with callback |
| 10 | +- wp_menu_page - adds menu in admin dashboard with callback |
| 11 | +- wp_submenu_page - adds submenu in admin dashboard with callback |
| 12 | +- wp_enqueue_scripts - adds action and one line of script |
| 13 | +- wp_enqueue_script - adds one line of script |
0 commit comments