跳至正文
OpenAgentSkill
浏览已开放 · 目录记录日期 2026-11-21 · 自动更新和账户服务暂时关闭。
数据与分析

wp-plugin-performance

Performance guidelines for WordPress plugin development: database optimization, object caching, conditional asset loading, efficient hooks, HTTP requests, WP-Cron, AJAX/REST optimization, and common anti-patterns. Based on official WordPress Developer Resources and WP VIP documentation.

fernandotellado · 已有 AI 审核记录

安装与使用

运行命令前先阅读来源与依赖说明。复制命令不会自动安装或执行。

npx skills add fernandotellado/ai-skills --skill wp-plugin-performance

技能说明


name: wp-plugin-performance description: "Performance guidelines for WordPress plugin development: database optimization, object caching, conditional asset loading, efficient hooks, HTTP requests, WP-Cron, AJAX/REST optimization, and common anti-patterns. Based on official WordPress Developer Resources and WP VIP documentation." compatibility: "WordPress 6.0+ / PHP 7.4+. Applies to plugins and custom code." license: GPL-2.0-or-later metadata: author: fernando-tellado version: "1.1"

WordPress plugin performance

When to use

Use this skill when:

  • Developing new WordPress plugins
  • Optimizing existing plugin code for better performance
  • Working with database queries (WP_Query, $wpdb, options)
  • Implementing caching strategies (object cache, transients)
  • Loading assets (scripts, styles) efficiently
  • Creating AJAX handlers or REST API endpoints
  • Scheduling background tasks with WP-Cron
  • Making external HTTP requests from plugins
  • Reviewing code before deployment to high-traffic sites

Core performance principles

The performance mantra

Query only what you need
Cache expensive operations
Load assets conditionally
Avoid work on every request

Key concepts

  1. Bounded queries: Always limit results with posts_per_page or similar
  2. Object caching: Store expensive computations for reuse across requests
  3. Conditional loading: Enqueue scripts/styles only where needed
  4. Context awareness: Check is_admin(), page conditions before heavy operations
  5. Async processing: Move slow tasks to WP-Cron or background processes

Database queries

Efficient database queries are the foundation of plugin performance.

WP_Query optimization

ParameterPurpose
posts_per_pageLimit results (never use -1 in production)
no_found_rowsSkip counting total rows when not paginating
update_post_meta_cacheSet false if not using post meta
update_post_term_cacheSet false if not using taxonomies
fieldsRequest only 'ids' or 'id=>parent' when full objects not needed
cache_resultsKeep true unless intentionally bypassing cache

WP_Query examples

// CORRECT: Optimized query for displaying 10 posts
$query = new WP_Query( array(
    'post_type'              => 'post',
    'posts_per_page'         => 10,
    'no_found_rows'          => true, // Skip SQL_CALC_FOUND_ROWS if not paginating
    'update_post_meta_cache' => false, // Skip if not using meta
    'update_post_term_cache' => false, // Skip if not using terms
) );

// CORRECT: Get only post IDs for a lightweight lookup
$post_ids = get_posts( array(
    'post_type'      => 'product',
    'posts_per_page' => 100,
    'fields'         => 'ids',
    'no_found_rows'  => true,
) );

// WRONG: Unbounded query - will crash on large sites
$all_posts = get_posts( array(
    'post_type'      => 'post',
    'posts_per_page' => -1, // Never do this in production!
) );

When pagination is needed

// CORRECT: With pagination - need found_rows for page links
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;

$query = new WP_Query( array(
    'post_type'      => 'post',
    'posts_per_page' => 10,
    'paged'          => $paged,
    // no_found_rows defaults to false - we need the count
) );

// Display pagination
echo paginate_links( array(
    'total' => $query->max_num_pages,
) );

Avoid query_posts()

// WRONG: Never use query_posts() - breaks main query and pagination
query_posts( 'cat=5' );

// CORRECT: Use pre_get_posts filter to modify main query
add_action( 'pre_get_posts', 'ayudawp_modify_main_query' );
function ayudawp_modify_main_query( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
        $query->set( 'cat', 5 );
    }
}

// CORRECT: Use WP_Query for secondary queries
$custom_query = new WP_Query( array( 'cat' => 5 ) );

Meta queries optimization

Meta queries scan unindexed columns. Use them sparingly.

// WRONG: Complex meta query on every page load
$query = new WP_Query( array(
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key'     => 'color',
            'value'   => 'red',
            'compare' => '=',
        ),
        array(
            'key'     => 'size',
            'value'   => array( 'S', 'M', 'L' ),
            'compare' => 'IN',
        ),
    ),
) );

// CORRECT: Use taxonomy for filterable attributes
register_taxonomy( 'product_color', 'product', array( /* ... */ ) );
register_taxonomy( 'product_size', 'product', array( /* ... */ ) );

$query = new WP_Query( array(
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_color',
            'field'    => 'slug',
            'terms'    => 'red',
        ),
        array(
            'taxonomy' => 'product_size',
            'field'    => 'slug',
            'terms'    => array( 's', 'm', 'l' ),
        ),
    ),
) );

Legitimate meta_query uses

Some lookups are unavoidably keyed by post meta because that is where the data lives (Privacy API exporters/erasers keyed by customer email, WooCommerce order number resolvers that respect plugins like Sequential Order Numbers, etc.). For these, the WordPress.DB.SlowDBQuery.slow_db_query_meta_query warning is informational, not a security issue, and the manual reviewer accepts it with a short justification:

$query = new WP_Query( array(
    'post_type'  => 'ayudawp_withdrawal',
    'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Privacy API exporter contract requires lookup by stored customer email.
        array(
            'key'   => '_ayudawp_email',
            'value' => $email,
        ),
    ),
) );

The justification text matters: state why the meta_query is the right tool here (a contract from another API, a third-party numbering scheme, etc.). A

阅读完整来源 ↗