Skip to content

secondsky/claude-skills / plugins/wordpress-plugin-core/skills/wordpress-plugin-core

wordpress-plugin-core

Flagged

WordPress plugin development with hooks, security, REST API, custom post types. Use for plugin creation, $wpdb queries, Settings API, or encountering SQL injection, XSS, CSRF, nonce errors.

FileKindSize
SKILL.mdskill md17.9 KB
assets/example-template.txtmarkdown416 B
examples/github-updater.phpscript15.3 KB
references/advanced-topics.mdmarkdown16.6 KB
references/common-hooks.mdmarkdown2.7 KB
references/common-patterns.mdmarkdown13.1 KB
references/error-catalog.mdmarkdown16.3 KB
references/example-reference.mdmarkdown760 B
references/github-auto-updates.mdmarkdown30.3 KB
references/plugin-architectures.mdmarkdown21.0 KB
references/security-checklist.mdmarkdown11.7 KB
scripts/example-script.shexecutablescript302 B
scripts/scaffold-plugin.shexecutablescript9.0 KB
templates/examples/ajax-handler.phpscript8.0 KB
templates/examples/custom-post-type.phpscript9.5 KB
templates/examples/meta-box.phpscript6.3 KB
templates/examples/rest-endpoint.phpscript8.7 KB
templates/examples/settings-page.phpscript7.9 KB
templates/plugin-oop/README.mdmarkdown7.4 KB
templates/plugin-oop/my-oop-plugin.phpscript12.5 KB
templates/plugin-oop/uninstall.phpscript929 B
templates/plugin-oop/views/admin-settings.phpscript2.1 KB
templates/plugin-psr4/README.mdmarkdown9.7 KB
templates/plugin-psr4/composer.jsonconfig706 B
templates/plugin-psr4/my-psr4-plugin.phpscript1.7 KB
templates/plugin-psr4/src/API/BookEndpoints.phpscript5.3 KB
templates/plugin-psr4/src/Admin/Settings.phpscript5.1 KB
templates/plugin-psr4/src/Frontend/Assets.phpscript2.0 KB
templates/plugin-psr4/src/Plugin.phpscript2.4 KB
templates/plugin-psr4/src/PostTypes/Book.phpscript4.9 KB
templates/plugin-psr4/src/Taxonomies/Genre.phpscript1.9 KB
templates/plugin-psr4/uninstall.phpscript929 B
templates/plugin-simple/README.mdmarkdown4.6 KB
templates/plugin-simple/my-simple-plugin.phpscript7.4 KB
templates/plugin-simple/uninstall.phpscript1.3 KB

templates/examples/ajax-handler.php

View on GitHub
<?php
/**
 * Example: AJAX handlers
 *
 * This example shows how to:
 * - Register AJAX handlers for logged-in and non-logged-in users
 * - Verify nonces for CSRF protection
 * - Check user capabilities
 * - Sanitize input and return JSON responses
 * - Enqueue scripts with localized data
 *
 * @package YourPlugin
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Enqueue AJAX script
 */
function yourprefix_enqueue_ajax_script() {
	wp_enqueue_script(
		'yourprefix-ajax',
		plugins_url( 'js/ajax-example.js', __FILE__ ),
		array( 'jquery' ),
		'1.0.0',
		true
	);

	// Localize script with AJAX URL and nonce
	wp_localize_script(
		'yourprefix-ajax',
		'yourprefixAjax',
		array(
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'yourprefix_ajax_nonce' ),
		)
	);
}
add_action( 'wp_enqueue_scripts', 'yourprefix_enqueue_ajax_script' );

/**
 * AJAX handler for logged-in users
 */
function yourprefix_ajax_save_data() {
	// Verify nonce
	check_ajax_referer( 'yourprefix_ajax_nonce', 'nonce' );

	// Check user capability
	if ( ! current_user_can( 'edit_posts' ) ) {
		wp_send_json_error( array(
			'message' => __( 'Permission denied', 'your-plugin' ),
		) );
	}

	// Get and sanitize input
	$name  = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : '';
	$email = isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : '';
	$age   = isset( $_POST['age'] ) ? absint( $_POST['age'] ) : 0;

	// Validate input
	if ( empty( $name ) ) {
		wp_send_json_error( array(
			'message' => __( 'Name is required', 'your-plugin' ),
		) );
	}

	if ( ! is_email( $email ) ) {
		wp_send_json_error( array(
			'message' => __( 'Invalid email address', 'your-plugin' ),
		) );
	}

	// Process data (example: save to database)
	$result = yourprefix_save_user_data( $name, $email, $age );

	if ( is_wp_error( $result ) ) {
		wp_send_json_error( array(
			'message' => $result->get_error_message(),
		) );
	}

	// Return success response
	wp_send_json_success( array(
		'message' => __( 'Data saved successfully', 'your-plugin' ),
		'data'    => array(
			'name'  => $name,
			'email' => $email,
			'age'   => $age,
		),
	) );
}
add_action( 'wp_ajax_yourprefix_save_data', 'yourprefix_ajax_save_data' );

/**
 * AJAX handler for non-logged-in users
 */
function yourprefix_ajax_public_action() {
	// Verify nonce (still required for public AJAX)
	check_ajax_referer( 'yourprefix_ajax_nonce', 'nonce' );

	// Get and sanitize input
	$query = isset( $_POST['query'] ) ? sanitize_text_field( $_POST['query'] ) : '';

	if ( empty( $query ) ) {
		wp_send_json_error( array(
			'message' => __( 'Query is required', 'your-plugin' ),
		) );
	}

	// Process query (example: search posts)
	$results = yourprefix_search_posts( $query );

	wp_send_json_success( array(
		'message' => __( 'Search completed', 'your-plugin' ),
		'results' => $results,
	) );
}
add_action( 'wp_ajax_yourprefix_public_action', 'yourprefix_ajax_public_action' );
add_action( 'wp_ajax_nopriv_yourprefix_public_action', 'yourprefix_ajax_public_action' );

/**
 * AJAX handler for fetching posts
 */
function yourprefix_ajax_load_posts() {
	// Verify nonce
	check_ajax_referer( 'yourprefix_ajax_nonce', 'nonce' );

	// Get parameters
	$page     = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 1;
	$per_page = isset( $_POST['per_page'] ) ? absint( $_POST['per_page'] ) : 10;
	$category = isset( $_POST['category'] ) ? absint( $_POST['category'] ) : 0;

	// Query posts
	$args = array(
		'post_type'      => 'post',
		'posts_per_page' => $per_page,
		'paged'          => $page,
		'post_status'    => 'publish',
	);

	if ( $category > 0 ) {
		$args['cat'] = $category;
	}

	$query = new WP_Query( $args );

	$posts = array();
	if ( $query->have_posts() ) {
		while ( $query->have_posts() ) {
			$query->the_post();
			$posts[] = array(
				'id'      => get_the_ID(),
				'title'   => get_the_title(),
				'excerpt' => get_the_excerpt(),
				'url'     => get_permalink(),
				'date'    => get_the_date(),
			);
		}
		wp_reset_postdata();
	}

	wp_send_json_success( array(
		'posts'       => $posts,
		'total_pages' => $query->max_num_pages,
		'found_posts' => $query->found_posts,
	) );
}
add_action( 'wp_ajax_yourprefix_load_posts', 'yourprefix_ajax_load_posts' );
add_action( 'wp_ajax_nopriv_yourprefix_load_posts', 'yourprefix_ajax_load_posts' );

/**
 * AJAX handler for deleting item
 */
function yourprefix_ajax_delete_item() {
	// Verify nonce
	check_ajax_referer( 'yourprefix_ajax_nonce', 'nonce' );

	// Check user capability
	if ( ! current_user_can( 'delete_posts' ) ) {
		wp_send_json_error( array(
			'message' => __( 'Permission denied', 'your-plugin' ),
		) );
	}

	// Get item ID
	$item_id = isset( $_POST['item_id'] ) ? absint( $_POST['item_id'] ) : 0;

	if ( $item_id === 0 ) {
		wp_send_json_error( array(
			'message' => __( 'Invalid item ID', 'your-plugin' ),
		) );
	}

	// Check if item exists
	$post = get_post( $item_id );
	if ( ! $post ) {
		wp_send_json_error( array(
			'message' => __( 'Item not found', 'your-plugin' ),
		) );
	}

	// Delete item
	$result = wp_trash_post( $item_id );

	if ( ! $result ) {
		wp_send_json_error( array(
			'message' => __( 'Failed to delete item', 'your-plugin' ),
		) );
	}

	wp_send_json_success( array(
		'message' => __( 'Item deleted successfully', 'your-plugin' ),
		'item_id' => $item_id,
	) );
}
add_action( 'wp_ajax_yourprefix_delete_item', 'yourprefix_ajax_delete_item' );

/**
 * AJAX handler for uploading file
 */
function yourprefix_ajax_upload_file() {
	// Verify nonce
	check_ajax_referer( 'yourprefix_ajax_nonce', 'nonce' );

	// Check user capability
	if ( ! current_user_can( 'upload_files' ) ) {
		wp_send_json_error( array(
			'message' => __( 'Permission denied', 'your-plugin' ),
		) );
	}

	// Check if file was uploaded
	if ( empty( $_FILES['file'] ) ) {
		wp_send_json_error( array(
			'message' => __( 'No file uploaded', 'your-plugin' ),
		) );
	}

	// Handle file upload
	require_once ABSPATH . 'wp-admin/includes/file.php';
	require_once ABSPATH . 'wp-admin/includes/media.php';
	require_once ABSPATH . 'wp-admin/includes/image.php';

	$file = $_FILES['file'];

	// Validate file type
	$allowed_types = array( 'image/jpeg', 'image/png', 'image/gif' );
	if ( ! in_array( $file['type'], $allowed_types, true ) ) {
		wp_send_json_error( array(
			'message' => __( 'Invalid file type. Only JPG, PNG, and GIF are allowed.', 'your-plugin' ),
		) );
	}

	// Upload file
	$attachment_id = media_handle_upload( 'file', 0 );

	if ( is_wp_error( $attachment_id ) ) {
		wp_send_json_error( array(
			'message' => $attachment_id->get_error_message(),
		) );
	}

	// Get attachment data
	$attachment_url = wp_get_attachment_url( $attachment_id );

	wp_send_json_success( array(
		'message'        => __( 'File uploaded successfully', 'your-plugin' ),
		'attachment_id'  => $attachment_id,
		'attachment_url' => $attachment_url,
	) );
}
add_action( 'wp_ajax_yourprefix_upload_file', 'yourprefix_ajax_upload_file' );

/**
 * Helper function: Save user data
 *
 * @param string $name  User name.
 * @param string $email User email.
 * @param int    $age   User age.
 * @return bool|WP_Error
 */
function yourprefix_save_user_data( $name, $email, $age ) {
	global $wpdb;

	$table_name = $wpdb->prefix . 'yourprefix_users';

	$result = $wpdb->insert(
		$table_name,
		array(
			'name'  => $name,
			'email' => $email,
			'age'   => $age,
		),
		array( '%s', '%s', '%d' )
	);

	if ( false === $result ) {
		return new WP_Error( 'db_error', __( 'Database error', 'your-plugin' ) );
	}

	return true;
}

/**
 * Helper function: Search posts
 *
 * @param string $query Search query.
 * @return array
 */
function yourprefix_search_posts( $query ) {
	$args = array(
		'post_type'      => 'post',
		's'              => $query,
		'posts_per_page' => 10,
		'post_status'    => 'publish',
	);

	$search_query = new WP_Query( $args );

	$results = array();
	if ( $search_query->have_posts() ) {
		while ( $search_query->have_posts() ) {
			$search_query->the_post();
			$results[] = array(
				'id'    => get_the_ID(),
				'title' => get_the_title(),
				'url'   => get_permalink(),
			);
		}
		wp_reset_postdata();
	}

	return $results;
}
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 467kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 467kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 467kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 193kUpdated Claude CodeCodexVerifiedInstall