@mytechtoday/augment-extensions 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/augment-extensions/domain-rules/wordpress/README.md +163 -0
- package/augment-extensions/domain-rules/wordpress/module.json +32 -0
- package/augment-extensions/domain-rules/wordpress/rules/coding-standards.md +617 -0
- package/augment-extensions/domain-rules/wordpress/rules/directory-structure.md +270 -0
- package/augment-extensions/domain-rules/wordpress/rules/file-patterns.md +423 -0
- package/augment-extensions/domain-rules/wordpress/rules/gutenberg-blocks.md +493 -0
- package/augment-extensions/domain-rules/wordpress/rules/performance.md +568 -0
- package/augment-extensions/domain-rules/wordpress/rules/plugin-development.md +510 -0
- package/augment-extensions/domain-rules/wordpress/rules/project-detection.md +251 -0
- package/augment-extensions/domain-rules/wordpress/rules/rest-api.md +501 -0
- package/augment-extensions/domain-rules/wordpress/rules/security.md +564 -0
- package/augment-extensions/domain-rules/wordpress/rules/theme-development.md +388 -0
- package/augment-extensions/domain-rules/wordpress/rules/woocommerce.md +441 -0
- package/augment-extensions/domain-rules/wordpress-plugin/README.md +139 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/ajax-plugin.md +1599 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/custom-post-type-plugin.md +1727 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/gutenberg-block-plugin.md +428 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/gutenberg-block.md +422 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/mvc-plugin.md +1623 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/object-oriented-plugin.md +1343 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/rest-endpoint.md +734 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/settings-page-plugin.md +1350 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/simple-procedural-plugin.md +503 -0
- package/augment-extensions/domain-rules/wordpress-plugin/examples/singleton-plugin.md +971 -0
- package/augment-extensions/domain-rules/wordpress-plugin/module.json +53 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/activation-hooks.md +770 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/admin-interface.md +874 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/ajax-handlers.md +629 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/asset-management.md +559 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/context-providers.md +709 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/cron-jobs.md +736 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/database-management.md +1057 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/documentation-standards.md +463 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/frontend-functionality.md +478 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/gutenberg-blocks.md +818 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/internationalization.md +416 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/migration.md +667 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/performance-optimization.md +878 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/plugin-architecture.md +693 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/plugin-structure.md +352 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/rest-api.md +818 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/scaffolding-workflow.md +624 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/security-best-practices.md +866 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/testing-patterns.md +1165 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/testing.md +414 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/vscode-integration.md +751 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/woocommerce-integration.md +949 -0
- package/augment-extensions/domain-rules/wordpress-plugin/rules/wordpress-org-submission.md +458 -0
- package/augment-extensions/examples/gutenberg-block-plugin/README.md +101 -0
- package/augment-extensions/examples/gutenberg-block-plugin/examples/testimonial-block.md +428 -0
- package/augment-extensions/examples/gutenberg-block-plugin/module.json +40 -0
- package/augment-extensions/examples/rest-api-plugin/README.md +98 -0
- package/augment-extensions/examples/rest-api-plugin/examples/task-manager-api.md +1299 -0
- package/augment-extensions/examples/rest-api-plugin/module.json +40 -0
- package/augment-extensions/examples/woocommerce-extension/README.md +98 -0
- package/augment-extensions/examples/woocommerce-extension/examples/product-customizer.md +763 -0
- package/augment-extensions/examples/woocommerce-extension/module.json +40 -0
- package/augment-extensions/workflows/wordpress-plugin/README.md +232 -0
- package/augment-extensions/workflows/wordpress-plugin/ai-prompts.md +839 -0
- package/augment-extensions/workflows/wordpress-plugin/bead-decomposition-patterns.md +854 -0
- package/augment-extensions/workflows/wordpress-plugin/examples/complete-plugin-example.md +540 -0
- package/augment-extensions/workflows/wordpress-plugin/examples/custom-post-type-example.md +1083 -0
- package/augment-extensions/workflows/wordpress-plugin/examples/feature-addition-workflow.md +669 -0
- package/augment-extensions/workflows/wordpress-plugin/examples/plugin-creation-workflow.md +597 -0
- package/augment-extensions/workflows/wordpress-plugin/examples/secure-form-handler-example.md +925 -0
- package/augment-extensions/workflows/wordpress-plugin/examples/security-audit-workflow.md +752 -0
- package/augment-extensions/workflows/wordpress-plugin/examples/wordpress-org-submission-workflow.md +773 -0
- package/augment-extensions/workflows/wordpress-plugin/module.json +49 -0
- package/augment-extensions/workflows/wordpress-plugin/rules/best-practices.md +942 -0
- package/augment-extensions/workflows/wordpress-plugin/rules/development-workflow.md +702 -0
- package/augment-extensions/workflows/wordpress-plugin/rules/submission-workflow.md +728 -0
- package/augment-extensions/workflows/wordpress-plugin/rules/testing-workflow.md +775 -0
- package/cli/dist/cli.js +5 -1
- package/cli/dist/cli.js.map +1 -1
- package/cli/dist/commands/show.d.ts.map +1 -1
- package/cli/dist/commands/show.js +41 -0
- package/cli/dist/commands/show.js.map +1 -1
- package/modules.md +52 -0
- package/package.json +1 -1
|
@@ -0,0 +1,568 @@
|
|
|
1
|
+
# WordPress Performance Optimization
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
This document provides performance optimization techniques for WordPress themes and plugins, covering caching, database queries, asset optimization, and more.
|
|
6
|
+
|
|
7
|
+
## Database Query Optimization
|
|
8
|
+
|
|
9
|
+
### Avoid Unnecessary Queries
|
|
10
|
+
|
|
11
|
+
```php
|
|
12
|
+
// ❌ BAD: Query in loop
|
|
13
|
+
foreach ( $post_ids as $post_id ) {
|
|
14
|
+
$post = get_post( $post_id ); // Separate query for each post
|
|
15
|
+
echo $post->post_title;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// ✅ GOOD: Single query
|
|
19
|
+
$posts = get_posts( array(
|
|
20
|
+
'post__in' => $post_ids,
|
|
21
|
+
'posts_per_page' => -1,
|
|
22
|
+
) );
|
|
23
|
+
foreach ( $posts as $post ) {
|
|
24
|
+
echo $post->post_title;
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Use WP_Query Efficiently
|
|
29
|
+
|
|
30
|
+
```php
|
|
31
|
+
// ✅ GOOD: Limit fields to what you need
|
|
32
|
+
$query = new WP_Query( array(
|
|
33
|
+
'post_type' => 'post',
|
|
34
|
+
'posts_per_page' => 10,
|
|
35
|
+
'fields' => 'ids', // Only get IDs
|
|
36
|
+
) );
|
|
37
|
+
|
|
38
|
+
// ✅ GOOD: Disable unnecessary features
|
|
39
|
+
$query = new WP_Query( array(
|
|
40
|
+
'post_type' => 'post',
|
|
41
|
+
'posts_per_page' => 10,
|
|
42
|
+
'no_found_rows' => true, // Disable pagination count
|
|
43
|
+
'update_post_meta_cache' => false, // Don't cache post meta
|
|
44
|
+
'update_post_term_cache' => false, // Don't cache terms
|
|
45
|
+
) );
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Optimize Meta Queries
|
|
49
|
+
|
|
50
|
+
```php
|
|
51
|
+
// ❌ BAD: Multiple meta queries
|
|
52
|
+
$query = new WP_Query( array(
|
|
53
|
+
'meta_query' => array(
|
|
54
|
+
array(
|
|
55
|
+
'key' => 'color',
|
|
56
|
+
'value' => 'blue',
|
|
57
|
+
),
|
|
58
|
+
array(
|
|
59
|
+
'key' => 'size',
|
|
60
|
+
'value' => 'large',
|
|
61
|
+
),
|
|
62
|
+
),
|
|
63
|
+
) );
|
|
64
|
+
|
|
65
|
+
// ✅ GOOD: Use custom table or taxonomy instead
|
|
66
|
+
// Create custom taxonomy for frequently queried meta
|
|
67
|
+
register_taxonomy( 'product_color', 'product' );
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Use Transients for Expensive Queries
|
|
71
|
+
|
|
72
|
+
```php
|
|
73
|
+
/**
|
|
74
|
+
* Get popular posts with caching
|
|
75
|
+
*/
|
|
76
|
+
function get_popular_posts() {
|
|
77
|
+
// Try to get cached results
|
|
78
|
+
$popular_posts = get_transient( 'popular_posts' );
|
|
79
|
+
|
|
80
|
+
if ( false === $popular_posts ) {
|
|
81
|
+
// Expensive query
|
|
82
|
+
$popular_posts = new WP_Query( array(
|
|
83
|
+
'post_type' => 'post',
|
|
84
|
+
'posts_per_page' => 10,
|
|
85
|
+
'meta_key' => 'views',
|
|
86
|
+
'orderby' => 'meta_value_num',
|
|
87
|
+
'order' => 'DESC',
|
|
88
|
+
) );
|
|
89
|
+
|
|
90
|
+
// Cache for 1 hour
|
|
91
|
+
set_transient( 'popular_posts', $popular_posts, HOUR_IN_SECONDS );
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return $popular_posts;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Clear cache when post is updated
|
|
99
|
+
*/
|
|
100
|
+
function clear_popular_posts_cache( $post_id ) {
|
|
101
|
+
delete_transient( 'popular_posts' );
|
|
102
|
+
}
|
|
103
|
+
add_action( 'save_post', 'clear_popular_posts_cache' );
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
## Object Caching
|
|
107
|
+
|
|
108
|
+
### Use WordPress Object Cache
|
|
109
|
+
|
|
110
|
+
```php
|
|
111
|
+
/**
|
|
112
|
+
* Get user data with caching
|
|
113
|
+
*/
|
|
114
|
+
function get_user_data( $user_id ) {
|
|
115
|
+
$cache_key = 'user_data_' . $user_id;
|
|
116
|
+
$user_data = wp_cache_get( $cache_key );
|
|
117
|
+
|
|
118
|
+
if ( false === $user_data ) {
|
|
119
|
+
// Expensive operation
|
|
120
|
+
$user_data = array(
|
|
121
|
+
'name' => get_user_meta( $user_id, 'name', true ),
|
|
122
|
+
'email' => get_user_meta( $user_id, 'email', true ),
|
|
123
|
+
'posts' => count_user_posts( $user_id ),
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
// Cache for 5 minutes
|
|
127
|
+
wp_cache_set( $cache_key, $user_data, '', 300 );
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return $user_data;
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Cache Groups
|
|
135
|
+
|
|
136
|
+
```php
|
|
137
|
+
// Set cache with group
|
|
138
|
+
wp_cache_set( 'key', $data, 'my_plugin', 3600 );
|
|
139
|
+
|
|
140
|
+
// Get cache from group
|
|
141
|
+
$data = wp_cache_get( 'key', 'my_plugin' );
|
|
142
|
+
|
|
143
|
+
// Delete cache group
|
|
144
|
+
wp_cache_delete( 'key', 'my_plugin' );
|
|
145
|
+
|
|
146
|
+
// Flush entire group
|
|
147
|
+
wp_cache_flush_group( 'my_plugin' );
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Asset Optimization
|
|
151
|
+
|
|
152
|
+
### Enqueue Scripts Efficiently
|
|
153
|
+
|
|
154
|
+
```php
|
|
155
|
+
/**
|
|
156
|
+
* Enqueue scripts only where needed
|
|
157
|
+
*/
|
|
158
|
+
function my_theme_enqueue_scripts() {
|
|
159
|
+
// Only load on single posts
|
|
160
|
+
if ( is_single() ) {
|
|
161
|
+
wp_enqueue_script(
|
|
162
|
+
'my-single-post-script',
|
|
163
|
+
get_template_directory_uri() . '/js/single-post.js',
|
|
164
|
+
array( 'jquery' ),
|
|
165
|
+
'1.0.0',
|
|
166
|
+
true // Load in footer
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Only load on specific page template
|
|
171
|
+
if ( is_page_template( 'template-contact.php' ) ) {
|
|
172
|
+
wp_enqueue_script(
|
|
173
|
+
'contact-form-script',
|
|
174
|
+
get_template_directory_uri() . '/js/contact-form.js',
|
|
175
|
+
array(),
|
|
176
|
+
'1.0.0',
|
|
177
|
+
true
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_scripts' );
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Defer and Async Scripts
|
|
185
|
+
|
|
186
|
+
```php
|
|
187
|
+
/**
|
|
188
|
+
* Add defer/async to scripts
|
|
189
|
+
*/
|
|
190
|
+
function add_defer_async_to_scripts( $tag, $handle ) {
|
|
191
|
+
// Scripts to defer
|
|
192
|
+
$defer_scripts = array( 'my-analytics-script' );
|
|
193
|
+
|
|
194
|
+
// Scripts to async
|
|
195
|
+
$async_scripts = array( 'my-social-script' );
|
|
196
|
+
|
|
197
|
+
if ( in_array( $handle, $defer_scripts, true ) ) {
|
|
198
|
+
return str_replace( ' src', ' defer src', $tag );
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if ( in_array( $handle, $async_scripts, true ) ) {
|
|
202
|
+
return str_replace( ' src', ' async src', $tag );
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return $tag;
|
|
206
|
+
}
|
|
207
|
+
add_filter( 'script_loader_tag', 'add_defer_async_to_scripts', 10, 2 );
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Minify and Combine Assets
|
|
211
|
+
|
|
212
|
+
```php
|
|
213
|
+
/**
|
|
214
|
+
* Enqueue minified assets in production
|
|
215
|
+
*/
|
|
216
|
+
function my_theme_enqueue_assets() {
|
|
217
|
+
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
|
|
218
|
+
|
|
219
|
+
wp_enqueue_style(
|
|
220
|
+
'my-theme-style',
|
|
221
|
+
get_template_directory_uri() . "/css/style{$suffix}.css",
|
|
222
|
+
array(),
|
|
223
|
+
'1.0.0'
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
wp_enqueue_script(
|
|
227
|
+
'my-theme-script',
|
|
228
|
+
get_template_directory_uri() . "/js/main{$suffix}.js",
|
|
229
|
+
array( 'jquery' ),
|
|
230
|
+
'1.0.0',
|
|
231
|
+
true
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Image Optimization
|
|
238
|
+
|
|
239
|
+
### Lazy Loading
|
|
240
|
+
|
|
241
|
+
```php
|
|
242
|
+
/**
|
|
243
|
+
* Add lazy loading to images
|
|
244
|
+
*/
|
|
245
|
+
function add_lazy_loading_to_images( $content ) {
|
|
246
|
+
// WordPress 5.5+ has native lazy loading
|
|
247
|
+
// This is for older versions or custom implementation
|
|
248
|
+
|
|
249
|
+
if ( is_admin() ) {
|
|
250
|
+
return $content;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
$content = preg_replace(
|
|
254
|
+
'/<img(.*?)src=/i',
|
|
255
|
+
'<img$1loading="lazy" src=',
|
|
256
|
+
$content
|
|
257
|
+
);
|
|
258
|
+
|
|
259
|
+
return $content;
|
|
260
|
+
}
|
|
261
|
+
add_filter( 'the_content', 'add_lazy_loading_to_images' );
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### Responsive Images
|
|
265
|
+
|
|
266
|
+
```php
|
|
267
|
+
/**
|
|
268
|
+
* Add custom image sizes
|
|
269
|
+
*/
|
|
270
|
+
add_image_size( 'thumbnail-small', 150, 150, true );
|
|
271
|
+
add_image_size( 'thumbnail-medium', 300, 300, true );
|
|
272
|
+
add_image_size( 'featured-large', 1200, 600, true );
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Use responsive images
|
|
276
|
+
*/
|
|
277
|
+
function display_responsive_image( $attachment_id ) {
|
|
278
|
+
echo wp_get_attachment_image(
|
|
279
|
+
$attachment_id,
|
|
280
|
+
'featured-large',
|
|
281
|
+
false,
|
|
282
|
+
array(
|
|
283
|
+
'sizes' => '(max-width: 600px) 100vw, (max-width: 1200px) 50vw, 1200px',
|
|
284
|
+
)
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### WebP Support
|
|
290
|
+
|
|
291
|
+
```php
|
|
292
|
+
/**
|
|
293
|
+
* Add WebP support
|
|
294
|
+
*/
|
|
295
|
+
function add_webp_mime_type( $mimes ) {
|
|
296
|
+
$mimes['webp'] = 'image/webp';
|
|
297
|
+
return $mimes;
|
|
298
|
+
}
|
|
299
|
+
add_filter( 'mime_types', 'add_webp_mime_type' );
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## HTTP Requests
|
|
303
|
+
|
|
304
|
+
### Reduce External Requests
|
|
305
|
+
|
|
306
|
+
```php
|
|
307
|
+
/**
|
|
308
|
+
* Remove unnecessary scripts
|
|
309
|
+
*/
|
|
310
|
+
function remove_unnecessary_scripts() {
|
|
311
|
+
// Remove emoji scripts
|
|
312
|
+
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
|
|
313
|
+
remove_action( 'wp_print_styles', 'print_emoji_styles' );
|
|
314
|
+
|
|
315
|
+
// Remove embed script
|
|
316
|
+
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
|
|
317
|
+
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
|
|
318
|
+
}
|
|
319
|
+
add_action( 'init', 'remove_unnecessary_scripts' );
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### Batch API Requests
|
|
323
|
+
|
|
324
|
+
```php
|
|
325
|
+
/**
|
|
326
|
+
* Batch multiple API requests
|
|
327
|
+
*/
|
|
328
|
+
function batch_api_requests( $endpoints ) {
|
|
329
|
+
$responses = array();
|
|
330
|
+
|
|
331
|
+
// Use WordPress HTTP API with concurrent requests
|
|
332
|
+
$requests = array();
|
|
333
|
+
foreach ( $endpoints as $key => $url ) {
|
|
334
|
+
$requests[ $key ] = array(
|
|
335
|
+
'url' => $url,
|
|
336
|
+
'type' => 'GET',
|
|
337
|
+
'timeout' => 10,
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Process requests (use plugin like WP HTTP Parallel for true parallel)
|
|
342
|
+
foreach ( $requests as $key => $request ) {
|
|
343
|
+
$response = wp_remote_get( $request['url'], array(
|
|
344
|
+
'timeout' => $request['timeout'],
|
|
345
|
+
) );
|
|
346
|
+
|
|
347
|
+
if ( ! is_wp_error( $response ) ) {
|
|
348
|
+
$responses[ $key ] = wp_remote_retrieve_body( $response );
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return $responses;
|
|
353
|
+
}
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
## Autoloading
|
|
357
|
+
|
|
358
|
+
### Optimize Autoloaded Options
|
|
359
|
+
|
|
360
|
+
```php
|
|
361
|
+
/**
|
|
362
|
+
* Don't autoload large options
|
|
363
|
+
*/
|
|
364
|
+
update_option( 'my_large_option', $data, false ); // false = don't autoload
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Check autoloaded data size
|
|
368
|
+
*/
|
|
369
|
+
function check_autoloaded_options() {
|
|
370
|
+
global $wpdb;
|
|
371
|
+
|
|
372
|
+
$autoloaded_options = $wpdb->get_results(
|
|
373
|
+
"SELECT option_name, LENGTH(option_value) as option_size
|
|
374
|
+
FROM {$wpdb->options}
|
|
375
|
+
WHERE autoload = 'yes'
|
|
376
|
+
ORDER BY option_size DESC
|
|
377
|
+
LIMIT 20"
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
foreach ( $autoloaded_options as $option ) {
|
|
381
|
+
echo $option->option_name . ': ' . size_format( $option->option_size ) . "\n";
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
## Cron Jobs
|
|
387
|
+
|
|
388
|
+
### Optimize Scheduled Tasks
|
|
389
|
+
|
|
390
|
+
```php
|
|
391
|
+
/**
|
|
392
|
+
* Schedule efficient cron job
|
|
393
|
+
*/
|
|
394
|
+
function schedule_cleanup_task() {
|
|
395
|
+
if ( ! wp_next_scheduled( 'my_cleanup_task' ) ) {
|
|
396
|
+
wp_schedule_event( time(), 'daily', 'my_cleanup_task' );
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
add_action( 'wp', 'schedule_cleanup_task' );
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Cleanup task
|
|
403
|
+
*/
|
|
404
|
+
function my_cleanup_task() {
|
|
405
|
+
// Limit processing
|
|
406
|
+
global $wpdb;
|
|
407
|
+
|
|
408
|
+
$wpdb->query(
|
|
409
|
+
$wpdb->prepare(
|
|
410
|
+
"DELETE FROM {$wpdb->postmeta}
|
|
411
|
+
WHERE meta_key = %s
|
|
412
|
+
AND meta_value < %s
|
|
413
|
+
LIMIT 100", // Process in batches
|
|
414
|
+
'_temp_data',
|
|
415
|
+
time() - WEEK_IN_SECONDS
|
|
416
|
+
)
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
add_action( 'my_cleanup_task', 'my_cleanup_task' );
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
## Pagination
|
|
423
|
+
|
|
424
|
+
### Efficient Pagination
|
|
425
|
+
|
|
426
|
+
```php
|
|
427
|
+
/**
|
|
428
|
+
* Use pagination instead of loading all posts
|
|
429
|
+
*/
|
|
430
|
+
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
|
|
431
|
+
|
|
432
|
+
$query = new WP_Query( array(
|
|
433
|
+
'post_type' => 'post',
|
|
434
|
+
'posts_per_page' => 20,
|
|
435
|
+
'paged' => $paged,
|
|
436
|
+
) );
|
|
437
|
+
|
|
438
|
+
// Display pagination
|
|
439
|
+
the_posts_pagination( array(
|
|
440
|
+
'mid_size' => 2,
|
|
441
|
+
'prev_text' => __( 'Previous', 'my-theme' ),
|
|
442
|
+
'next_text' => __( 'Next', 'my-theme' ),
|
|
443
|
+
) );
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
## Database Tables
|
|
447
|
+
|
|
448
|
+
### Use Indexes
|
|
449
|
+
|
|
450
|
+
```php
|
|
451
|
+
/**
|
|
452
|
+
* Create custom table with indexes
|
|
453
|
+
*/
|
|
454
|
+
function create_custom_table() {
|
|
455
|
+
global $wpdb;
|
|
456
|
+
|
|
457
|
+
$table_name = $wpdb->prefix . 'my_table';
|
|
458
|
+
$charset_collate = $wpdb->get_charset_collate();
|
|
459
|
+
|
|
460
|
+
$sql = "CREATE TABLE $table_name (
|
|
461
|
+
id bigint(20) NOT NULL AUTO_INCREMENT,
|
|
462
|
+
user_id bigint(20) NOT NULL,
|
|
463
|
+
data text NOT NULL,
|
|
464
|
+
created_at datetime DEFAULT CURRENT_TIMESTAMP,
|
|
465
|
+
PRIMARY KEY (id),
|
|
466
|
+
KEY user_id (user_id),
|
|
467
|
+
KEY created_at (created_at)
|
|
468
|
+
) $charset_collate;";
|
|
469
|
+
|
|
470
|
+
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
|
|
471
|
+
dbDelta( $sql );
|
|
472
|
+
}
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
## Monitoring and Profiling
|
|
476
|
+
|
|
477
|
+
### Query Monitor Plugin
|
|
478
|
+
|
|
479
|
+
```php
|
|
480
|
+
/**
|
|
481
|
+
* Add custom timing
|
|
482
|
+
*/
|
|
483
|
+
do_action( 'qm/start', 'my_expensive_operation' );
|
|
484
|
+
|
|
485
|
+
// Expensive operation here
|
|
486
|
+
my_expensive_function();
|
|
487
|
+
|
|
488
|
+
do_action( 'qm/stop', 'my_expensive_operation' );
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
### Debug Queries
|
|
492
|
+
|
|
493
|
+
```php
|
|
494
|
+
/**
|
|
495
|
+
* Enable query debugging (development only)
|
|
496
|
+
*/
|
|
497
|
+
define( 'SAVEQUERIES', true );
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Display queries
|
|
501
|
+
*/
|
|
502
|
+
function display_queries() {
|
|
503
|
+
if ( current_user_can( 'manage_options' ) && defined( 'SAVEQUERIES' ) && SAVEQUERIES ) {
|
|
504
|
+
global $wpdb;
|
|
505
|
+
echo '<pre>';
|
|
506
|
+
print_r( $wpdb->queries );
|
|
507
|
+
echo '</pre>';
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
add_action( 'wp_footer', 'display_queries' );
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
## Best Practices
|
|
514
|
+
|
|
515
|
+
### DO
|
|
516
|
+
|
|
517
|
+
✅ Cache expensive operations
|
|
518
|
+
✅ Optimize database queries
|
|
519
|
+
✅ Load scripts only where needed
|
|
520
|
+
✅ Use lazy loading for images
|
|
521
|
+
✅ Minify and combine assets
|
|
522
|
+
✅ Use CDN for static assets
|
|
523
|
+
✅ Enable GZIP compression
|
|
524
|
+
✅ Optimize autoloaded data
|
|
525
|
+
✅ Use pagination for large datasets
|
|
526
|
+
✅ Monitor performance regularly
|
|
527
|
+
|
|
528
|
+
### DON'T
|
|
529
|
+
|
|
530
|
+
❌ Query in loops
|
|
531
|
+
❌ Load all posts without pagination
|
|
532
|
+
❌ Autoload large options
|
|
533
|
+
❌ Ignore caching opportunities
|
|
534
|
+
❌ Load unnecessary scripts
|
|
535
|
+
❌ Use unoptimized images
|
|
536
|
+
❌ Make excessive HTTP requests
|
|
537
|
+
❌ Skip database indexes
|
|
538
|
+
❌ Ignore slow queries
|
|
539
|
+
❌ Forget to clean up transients
|
|
540
|
+
|
|
541
|
+
## Performance Checklist
|
|
542
|
+
|
|
543
|
+
### Theme/Plugin Development
|
|
544
|
+
|
|
545
|
+
- [ ] Optimize database queries
|
|
546
|
+
- [ ] Implement caching (transients, object cache)
|
|
547
|
+
- [ ] Enqueue scripts conditionally
|
|
548
|
+
- [ ] Minify CSS and JavaScript
|
|
549
|
+
- [ ] Optimize images
|
|
550
|
+
- [ ] Use lazy loading
|
|
551
|
+
- [ ] Reduce HTTP requests
|
|
552
|
+
- [ ] Optimize autoloaded data
|
|
553
|
+
- [ ] Use pagination
|
|
554
|
+
- [ ] Profile and monitor performance
|
|
555
|
+
|
|
556
|
+
### Server Configuration
|
|
557
|
+
|
|
558
|
+
- [ ] Enable caching (Redis, Memcached)
|
|
559
|
+
- [ ] Use PHP 8.0+
|
|
560
|
+
- [ ] Enable OPcache
|
|
561
|
+
- [ ] Configure GZIP compression
|
|
562
|
+
- [ ] Use CDN
|
|
563
|
+
- [ ] Optimize database
|
|
564
|
+
- [ ] Enable HTTP/2
|
|
565
|
+
- [ ] Use SSL/TLS
|
|
566
|
+
- [ ] Configure caching headers
|
|
567
|
+
- [ ] Monitor server resources
|
|
568
|
+
|