File size: 10,682 Bytes
407171a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# WordPress Developer Markdown Brain
> Used by AI pipeline to plan, generate, and validate WordPress themes, plugins, REST API integrations.

---

## WordPress REST API

### Base URL
```
https://yoursite.com/wp-json/wp/v2/
```

### Core Endpoints
| Resource | GET | POST | PUT | DELETE |
|----------|-----|------|-----|--------|
| Posts | `/posts` | `/posts` | `/posts/{id}` | `/posts/{id}` |
| Pages | `/pages` | `/pages` | `/pages/{id}` | `/pages/{id}` |
| Media | `/media` | `/media` | `/media/{id}` | `/media/{id}` |
| Users | `/users` | `/users` | `/users/{id}` | `/users/{id}` |
| Categories | `/categories` | `/categories` | β€” | β€” |
| Tags | `/tags` | `/tags` | β€” | β€” |
| Comments | `/comments` | `/comments` | `/comments/{id}` | `/comments/{id}` |
| Menus | `/menus` | β€” | β€” | β€” |

### Authentication
```http
# Application Password (WP 5.6+)
Authorization: Basic base64(username:app_password)

# JWT Plugin
Authorization: Bearer <jwt_token>

# Cookie + Nonce (frontend)
X-WP-Nonce: {nonce}
```

### Query Parameters
```
?per_page=10        # items per page (max 100)
?page=2             # pagination
?search=keyword     # full-text search
?status=publish     # draft | publish | pending | private
?categories=1,2     # filter by category IDs
?tags=3,4           # filter by tag IDs
?author=1           # filter by author ID
?orderby=date       # date | title | id | slug | modified
?order=desc         # asc | desc
?_embed=true        # include embedded resources
?_fields=id,title   # return only specific fields
```

### Create Post (POST /posts)
```json
{
  "title": "My Post Title",
  "content": "<p>HTML content here</p>",
  "excerpt": "Short summary",
  "status": "publish",
  "categories": [1, 2],
  "tags": [3],
  "featured_media": 42,
  "slug": "my-post-title",
  "meta": {
    "custom_field": "value"
  }
}
```

### Response Shape
```json
{
  "id": 1,
  "date": "2024-01-01T00:00:00",
  "slug": "post-slug",
  "status": "publish",
  "title": { "rendered": "Post Title" },
  "content": { "rendered": "<p>...</p>", "protected": false },
  "excerpt": { "rendered": "<p>...</p>" },
  "author": 1,
  "featured_media": 0,
  "categories": [1],
  "tags": [],
  "_links": { "self": [{"href": "..."}] }
}
```

---

## WordPress Theme Structure

```
my-theme/
β”œβ”€β”€ style.css              # Theme header + base styles
β”œβ”€β”€ index.php              # Main template fallback
β”œβ”€β”€ functions.php          # Theme setup, hooks, enqueue scripts
β”œβ”€β”€ header.php             # <head> + nav
β”œβ”€β”€ footer.php             # closing tags + scripts
β”œβ”€β”€ sidebar.php            # widget areas
β”œβ”€β”€ single.php             # single post
β”œβ”€β”€ page.php               # static page
β”œβ”€β”€ archive.php            # archive (category/tag/date)
β”œβ”€β”€ search.php             # search results
β”œβ”€β”€ 404.php                # not found
β”œβ”€β”€ front-page.php         # homepage (if static front page)
β”œβ”€β”€ home.php               # blog index
β”œβ”€β”€ functions/
β”‚   β”œβ”€β”€ enqueue.php        # wp_enqueue_scripts
β”‚   β”œβ”€β”€ setup.php          # add_theme_support
β”‚   β”œβ”€β”€ widgets.php        # register_sidebar
β”‚   └── custom-post-types.php
β”œβ”€β”€ template-parts/
β”‚   β”œβ”€β”€ content.php
β”‚   β”œβ”€β”€ content-single.php
β”‚   └── content-none.php
β”œβ”€β”€ inc/
β”‚   └── customizer.php
β”œβ”€β”€ assets/
β”‚   β”œβ”€β”€ css/
β”‚   β”œβ”€β”€ js/
β”‚   └── images/
└── screenshot.png         # 1200x900 theme preview
```

### style.css Header (Required)
```css
/*
Theme Name: My Theme
Theme URI: https://example.com
Author: Dolor David Prince
Author URI: https://dolor3v.com
Description: A custom WordPress theme
Version: 1.0.0
License: GNU GPL v2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: my-theme
Tags: custom, responsive, blog
*/
```

### functions.php Essential Pattern
```php
<?php
// Theme setup
function mytheme_setup() {
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('html5', ['search-form', 'comment-form', 'gallery']);
    add_theme_support('custom-logo');
    add_theme_support('customize-selective-refresh-widgets');
    
    register_nav_menus([
        'primary' => __('Primary Menu', 'my-theme'),
        'footer'  => __('Footer Menu', 'my-theme'),
    ]);
    
    load_theme_textdomain('my-theme', get_template_directory() . '/languages');
}
add_action('after_setup_theme', 'mytheme_setup');

// Enqueue scripts and styles
function mytheme_scripts() {
    wp_enqueue_style('mytheme-style', get_stylesheet_uri(), [], '1.0.0');
    wp_enqueue_style('mytheme-main', get_template_directory_uri() . '/assets/css/main.css', [], '1.0.0');
    wp_enqueue_script('mytheme-main', get_template_directory_uri() . '/assets/js/main.js', ['jquery'], '1.0.0', true);
    
    // Pass data to JS
    wp_localize_script('mytheme-main', 'mythemeData', [
        'ajaxUrl' => admin_url('admin-ajax.php'),
        'nonce'   => wp_create_nonce('mytheme_nonce'),
        'siteUrl' => get_site_url(),
    ]);
}
add_action('wp_enqueue_scripts', 'mytheme_scripts');

// Register widget areas
function mytheme_widgets_init() {
    register_sidebar([
        'name'          => __('Main Sidebar', 'my-theme'),
        'id'            => 'sidebar-1',
        'description'   => '',
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ]);
}
add_action('widgets_init', 'mytheme_widgets_init');
```

---

## WordPress Plugin Structure

```
my-plugin/
β”œβ”€β”€ my-plugin.php          # Plugin header + bootstrap
β”œβ”€β”€ readme.txt             # WordPress.org readme
β”œβ”€β”€ uninstall.php          # Cleanup on delete
β”œβ”€β”€ includes/
β”‚   β”œβ”€β”€ class-plugin.php   # Main plugin class
β”‚   β”œβ”€β”€ class-admin.php    # Admin functionality
β”‚   β”œβ”€β”€ class-api.php      # REST API extensions
β”‚   └── class-cpt.php      # Custom post types
β”œβ”€β”€ admin/
β”‚   β”œβ”€β”€ css/
β”‚   β”œβ”€β”€ js/
β”‚   └── views/
β”œβ”€β”€ public/
β”‚   β”œβ”€β”€ css/
β”‚   β”œβ”€β”€ js/
β”‚   └── views/
└── languages/
```

### Plugin Header
```php
<?php
/**
 * Plugin Name:       My Plugin
 * Plugin URI:        https://dolor3v.com/my-plugin
 * Description:       Plugin description here.
 * Version:           1.0.0
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * Author:            Dolor David Prince
 * Author URI:        https://dolor3v.com
 * License:           GPL v2 or later
 * Text Domain:       my-plugin
 */

defined('ABSPATH') || exit;

define('MY_PLUGIN_VERSION', '1.0.0');
define('MY_PLUGIN_PATH', plugin_dir_path(__FILE__));
define('MY_PLUGIN_URL', plugin_dir_url(__FILE__));
```

### Custom Post Type
```php
function register_project_cpt() {
    register_post_type('project', [
        'labels' => [
            'name'               => 'Projects',
            'singular_name'      => 'Project',
            'add_new_item'       => 'Add New Project',
            'edit_item'          => 'Edit Project',
            'view_item'          => 'View Project',
            'search_items'       => 'Search Projects',
            'not_found'          => 'No projects found',
        ],
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_rest'       => true,   // Enable REST API
        'rest_base'          => 'projects',
        'supports'           => ['title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'],
        'has_archive'        => true,
        'rewrite'            => ['slug' => 'projects'],
        'menu_icon'          => 'dashicons-portfolio',
    ]);
}
add_action('init', 'register_project_cpt');
```

### Custom REST Endpoint
```php
add_action('rest_api_init', function() {
    register_rest_route('my-plugin/v1', '/data', [
        'methods'             => 'GET',
        'callback'            => 'my_plugin_get_data',
        'permission_callback' => '__return_true',
        'args' => [
            'id' => [
                'validate_callback' => fn($v) => is_numeric($v),
                'sanitize_callback' => 'absint',
            ],
        ],
    ]);
});

function my_plugin_get_data(WP_REST_Request $request) {
    $id = $request->get_param('id');
    // ... logic
    return new WP_REST_Response(['data' => $result], 200);
}
```

### Gutenberg Block (block.json)
```json
{
  "$schema": "https://schemas.wp.org/trunk/block.json",
  "apiVersion": 3,
  "name": "my-plugin/hero",
  "version": "1.0.0",
  "title": "Hero Block",
  "category": "layout",
  "description": "A hero section block",
  "supports": { "html": false, "align": ["wide", "full"] },
  "attributes": {
    "heading": { "type": "string", "default": "Hello World" },
    "subtext": { "type": "string", "default": "" },
    "backgroundColor": { "type": "string", "default": "#000" }
  },
  "editorScript": "file:./build/index.js",
  "editorStyle":  "file:./build/index.css",
  "style":        "file:./build/style-index.css"
}
```

---

## WordPress Hooks Reference

### Action Hooks
```php
add_action('init', $cb);                    // After WP loads
add_action('wp_head', $cb);                 // Inside <head>
add_action('wp_footer', $cb);               // Before </body>
add_action('save_post', $cb, 10, 3);        // Post saved
add_action('wp_enqueue_scripts', $cb);      // Enqueue assets
add_action('admin_init', $cb);              // Admin init
add_action('admin_menu', $cb);              // Register admin pages
add_action('rest_api_init', $cb);           // Register REST routes
add_action('wp_ajax_{action}', $cb);        // AJAX (logged in)
add_action('wp_ajax_nopriv_{action}', $cb); // AJAX (public)
```

### Filter Hooks
```php
add_filter('the_content', $cb);             // Post content
add_filter('the_title', $cb);               // Post title
add_filter('excerpt_length', $cb);          // Excerpt word count
add_filter('wp_nav_menu_items', $cb, 10, 2);// Nav menu items
add_filter('body_class', $cb);              // Body classes
add_filter('upload_mimes', $cb);            // Allowed file types
add_filter('rest_post_query', $cb, 10, 2);  // REST query args
```

---

## WP-CLI Commands
```bash
wp post create --post_title="Title" --post_status=publish
wp post list --post_type=page
wp user create user@example.com --role=editor
wp plugin install woocommerce --activate
wp theme activate my-theme
wp db export backup.sql
wp search-replace 'old-url.com' 'new-url.com'
wp cron event run --due-now
wp cache flush
```