Skip to content

generoi/wp-vertex

Repository files navigation

WP Vertex

A WordPress plugin for indexing content to Google Cloud Vertex AI Discovery Engine (formerly Enterprise Search).

Prerequisites

1. Install Google Cloud SDK

# macOS (using Homebrew)
brew install --cask google-cloud-sdk

# Or download directly
curl https://sdk.cloud.google.com | bash
exec -l $SHELL

2. Google Cloud Setup

# Initialize gcloud
gcloud init

# Set your project
gcloud config set project YOUR_PROJECT_ID

# Enable required APIs
gcloud services enable discoveryengine.googleapis.com
gcloud services enable aiplatform.googleapis.com

3. Create Discovery Engine Data Store

Note: The gcloud CLI doesn't currently support Discovery Engine commands, so we'll use the API directly.

# Create the data store using the Discovery Engine API
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-H "X-Goog-User-Project: YOUR_PROJECT_ID" \
"https://discoveryengine.googleapis.com/v1alpha/projects/YOUR_PROJECT_ID/locations/global/collections/default_collection/dataStores?dataStoreId=wordpress-store" \
-d '{
  "displayName": "WordPress Data Store",
  "industryVertical": "GENERIC",
  "solutionTypes": ["SOLUTION_TYPE_SEARCH"]
}'

Important: Replace YOUR_PROJECT_ID with your actual Google Cloud project ID.

Configuration

Environment Variables

Create a .env file in your project root:

# Google Cloud Configuration
VERTEX_PROJECT_ID='your-project-id-here'
VERTEX_LOCATION='global'
VERTEX_DATASTORE_ID='wordpress-store'
VERTEX_BRANCH_ID='0'
VERTEX_COLLECTION_ID='default_collection'

# Google Authentication
GOOGLE_APPLICATION_CREDENTIALS='/path/to/your/service-account-key.json'

Service Account Setup

# Create service account (suggested name)
gcloud iam service-accounts create vertex-indexer \
  --display-name="Vertex AI Indexer"

# Grant necessary roles
gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \
  --member="serviceAccount:vertex-indexer@YOUR_PROJECT_ID.iam.gserviceaccount.com" \
  --role="roles/discoveryengine.admin"

# Create and download key
gcloud iam service-accounts keys create service-account-key.json \
  --iam-account=vertex-indexer@YOUR_PROJECT_ID.iam.gserviceaccount.com

Usage

WP-CLI Commands

Indexing Content

# Index all posts
wp vertex post indexAll

# Index specific post types
wp vertex post indexAll --post-types=recipe,product

# Index with specific post status
wp vertex post indexAll --post-status=publish,private

# Force re-indexing (bypass hash checks)
wp vertex post indexAll --force

# Index all terms
wp vertex term indexAll

# Index specific taxonomies
wp vertex term indexAll --taxonomies=category,cuisine,difficulty

# Index all media
wp vertex media indexAll

# Index specific MIME types
wp vertex media indexAll --mime-types=image/jpeg,image/png

Purging Content

# Purge all posts
wp vertex post purge

# Purge specific post types
wp vertex post purge --post-types=recipe,product

# Purge with specific post status
wp vertex post purge --post-status=draft,trash

# Purge all terms
wp vertex term purge

# Purge specific taxonomies
wp vertex term purge --taxonomies=category,cuisine

# Purge all media
wp vertex media purge

# Purge specific MIME types
wp vertex media purge --mime-types=application/pdf

# Global purge (all content types)
wp vertex purge

# Global purge specific content types
wp vertex purge --content-types=post,term

# Dry run (see what would be purged)
wp vertex purge --dry-run
wp vertex post purge --dry-run

Status and Information

# Check indexing status
wp vertex post status
wp vertex term status
wp vertex media status

# Get counts
wp vertex post count
wp vertex term count
wp vertex media count

# Test Vertex AI connection
wp vertex test

Posts

# Index single post
wp vertex post index 123

# Index multiple posts
wp vertex post indexBatch 123,456,789

# Index all posts
wp vertex post indexAll

# Index specific post types (suggested for recipe sites)
wp vertex post indexAll --postTypes=post,recipe,product,food_product

# Remove post from index
wp vertex post remove 123

# Get statistics
wp vertex post stats

Terms

# Index single term
wp vertex term index 456

# Index multiple terms
wp vertex term indexBatch 1,2,3,4,5

# Index all terms
wp vertex term indexAll

# Index specific taxonomies (suggested for recipe sites)
wp vertex term indexAll --taxonomies=category,cuisine,difficulty,ingredient,season

# Remove term from index
wp vertex term remove 456

# Get statistics
wp vertex term stats

Media

# Index single media item
wp vertex media index 789

# Index multiple media items
wp vertex media indexBatch 100,200,300

# Index all media
wp vertex media indexAll

# Index specific MIME types (suggested for recipe sites)
wp vertex media indexAll --mimeTypes=application/pdf,image/jpeg,image/png

# Remove media from index
wp vertex media remove 789

# Get statistics
wp vertex media stats

Configuration & Customization

Enable/Disable Content Types

Control which content types are indexed:

// Disable post indexing
add_filter('wp-vertex/post/enabled', '__return_false');

// Disable term indexing  
add_filter('wp-vertex/term/enabled', '__return_false');

// Disable media indexing
add_filter('wp-vertex/media/enabled', '__return_false');

Customize Indexing Queries

Modify the query arguments used for indexing:

// Customize post indexing query
add_filter('wp-vertex/post/query', function($query) {
    $query['post_type'] = ['recipe'];
    return $query;
});

// Customize term indexing query
add_filter('wp-vertex/term/query', function($query) {
    $query['hide_empty'] = false;
    return $query;
});

// Customize media indexing query
add_filter('wp-vertex/media/query', function($query) {
    $query['post_mime_type'] = ['image/jpeg', 'image/png'];
    return $query;
});

Document Modification

Modify documents before they're sent to Vertex AI:

add_filter('wp-vertex/structData', function (StructData $struct, Document $document) {
    if ($struct->objectType === 'post' && $struct->objectSubtype === 'recipe') {
        /** @var WP_Post $post */
        $post = $document->entity;
        $composer = new ContentRecipe();

        $instructions = $composer->getInstructions($post);
        $struct->additionalFields = [
           'instructions' => wp_strip_all_tags($instructions),
        ];
    }
    return $struct;
}, 10, 2);

About

WIP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published