Created
January 10, 2025 07:57
-
-
Save eduard-un/fe78514064c233a75da25ed94d140b42 to your computer and use it in GitHub Desktop.
Populate WordPress with 50 posts and projects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define categories for posts | |
CATEGORIES=("Nature" "People" "Buildings" "Blog" "Technology" "Science" "History" "Art" "Food" "Travel" "Sports" "Entertainment") | |
# Define project categories | |
PROJECT_CATEGORIES=("Architecture" "Photography" "Design" "Technology" "Art" "Sports" "Travel" "Entertainment" "Science" "History" "Food" "Buildings") | |
# Define tags for posts and projects | |
POST_TAGS=("Adventure" "Travel" "Food" "Lifestyle" "Technology" "Fitness" "Education" "Health" "Business" "Entertainment") | |
PROJECT_TAGS=("Design" "Development" "Marketing" "Branding" "Photography" "Art" "Animation" "Illustration" "Architecture" "Strategy") | |
# Function to create categories for posts | |
create_categories() { | |
for CATEGORY in "${CATEGORIES[@]}"; do | |
wp term create category "$CATEGORY" --slug="$CATEGORY" | |
done | |
} | |
# Function to create project categories (project_category taxonomy) | |
create_project_categories() { | |
for PROJECT_CATEGORY in "${PROJECT_CATEGORIES[@]}"; do | |
wp term create project_category "$PROJECT_CATEGORY" --slug="$PROJECT_CATEGORY" | |
done | |
} | |
# Function to rename Uncategorized to News | |
rename_uncategorized() { | |
wp term update category 1 --name="News" | |
} | |
# Function to create post tags | |
create_post_tags() { | |
for TAG in "${POST_TAGS[@]}"; do | |
wp term create post_tag "$TAG" | |
done | |
} | |
# Function to create project tags (project_tag taxonomy) | |
create_project_tags() { | |
for TAG in "${PROJECT_TAGS[@]}"; do | |
wp term create project_tag "$TAG" | |
done | |
} | |
# Function to assign 2 random tags to a post | |
assign_random_post_tags() { | |
local TAGS=() | |
while [ ${#TAGS[@]} -lt 2 ]; do | |
RANDOM_INDEX=$((RANDOM % 10)) | |
TAG="${POST_TAGS[$RANDOM_INDEX]}" | |
if [[ ! " ${TAGS[@]} " =~ " ${TAG} " ]]; then | |
TAGS+=("$TAG") | |
fi | |
done | |
wp post term set "$1" post_tag "${TAGS[@]}" | |
} | |
# Function to assign 2 random project tags | |
assign_random_project_tags() { | |
local TAGS=() | |
while [ ${#TAGS[@]} -lt 2 ]; do | |
RANDOM_INDEX=$((RANDOM % 10)) | |
TAG="${PROJECT_TAGS[$RANDOM_INDEX]}" | |
if [[ ! " ${TAGS[@]} " =~ " ${TAG} " ]]; then | |
TAGS+=("$TAG") | |
fi | |
done | |
wp post term set "$1" project_tag "${TAGS[@]}" | |
} | |
# Function to randomly assign a post category | |
assign_random_category() { | |
RANDOM_INDEX=$((RANDOM % ${#CATEGORIES[@]})) | |
CATEGORY="${CATEGORIES[$RANDOM_INDEX]}" | |
wp post term set "$1" category "$CATEGORY" | |
} | |
# Function to randomly assign a project category (project_category taxonomy) | |
assign_random_project_category() { | |
RANDOM_INDEX=$((RANDOM % ${#PROJECT_CATEGORIES[@]})) | |
PROJECT_CATEGORY="${PROJECT_CATEGORIES[$RANDOM_INDEX]}" | |
wp post term set "$1" project_category "$PROJECT_CATEGORY" | |
} | |
# Define the content for the posts | |
POST_CONTENT="Pariatur ad est reprehenderit amet mollit aliqua fugiat pariatur. Esse deserunt in sit laboris. Anim deserunt do ex. Dolor sint ad magna voluptate et in voluptate ea excepteur aute dolore eu sit. Enim ullamco elit consequat dolore non eu id elit. | |
Ut quis commodo sit occaecat commodo exercitation qui. Ut voluptate magna aliquip deserunt. Veniam laboris duis eiusmod tempor adipisicing voluptate. Esse velit pariatur eiusmod anim et consectetur laboris anim aliquip ipsum exercitation et ut ut officia. Minim cillum reprehenderit adipisicing Lorem. Adipisicing duis laboris pariatur nulla sit mollit ut culpa qui ut ullamco. Tempor occaecat excepteur ullamco quis voluptate. | |
Amet nulla fugiat consequat magna enim et adipisicing magna adipisicing voluptate cillum minim ipsum consequat. Tempor aute sunt ea. Proident aute nisi et proident. Reprehenderit est reprehenderit magna sit consequat. Do dolore non ex cillum officia cupidatat sit cupidatat reprehenderit sunt pariatur commodo enim ea. Aliquip id nulla veniam ipsum occaecat excepteur cupidatat cillum ad excepteur duis qui ullamco adipisicing. Anim consequat tempor dolor velit ipsum non reprehenderit non aliqua et anim. Enim culpa consequat mollit magna ut dolore. | |
Consectetur laborum nisi laboris. Cupidatat est anim eu aliquip Lorem incididunt voluptate officia cillum. Fugiat aute velit ipsum do. Officia cupidatat Lorem ut proident deserunt deserunt exercitation exercitation duis aliqua anim exercitation. Proident deserunt incididunt sunt ullamco consectetur amet culpa incididunt ipsum quis mollit." | |
# Define an array of excerpts for each post | |
EXCERPTS=( | |
"Lorem ipsum dolor sit amet." | |
"Consectetur adipiscing elit." | |
"Sed do eiusmod tempor incididunt." | |
"Ut labore et dolore magna aliqua." | |
"Ut enim ad minim veniam." | |
"Quis nostrud exercitation ullamco laboris nisi ut aliquip." | |
"Ex ea commodo consequat." | |
"Duis aute irure dolor in reprehenderit." | |
"In voluptate velit esse cillum dolore." | |
"Eu fugiat nulla pariatur." | |
"Excepteur sint occaecat cupidatat non proident." | |
"Sunt in culpa qui officia deserunt mollit anim id est laborum." | |
"Curabitur pretium tincidunt lacus." | |
"Nulla gravida orci a odio." | |
"Nullam varius, turpis et commodo pharetra." | |
"Eros ligula aliquet magna." | |
"Quis viverra orci sagittis eu volutpat." | |
"Mauris ullamcorper purus sit amet nulla." | |
"Vivamus luctus egestas leo vel tempus." | |
"Donec odio libero, porttitor eu, tempus ac." | |
"Ut tincidunt tincidunt erat." | |
"Suspendisse potenti." | |
"Morbi vestibulum volutpat enim." | |
"Aliquam erat volutpat." | |
"Aenean vulputate eleifend tellus." | |
"Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac." | |
"Enim diam vulputate ut pharetra sit amet aliquam." | |
"Aliquam malesuada bibendum arcu vitae elementum." | |
"Turpis egestas pretium aenean pharetra magna." | |
"Et malesuada fames ac turpis egestas." | |
"Nulla facilisi cras fermentum odio eu feugiat pretium." | |
"Feugiat sed lectus vestibulum mattis ullamcorper velit." | |
"Cras semper auctor neque vitae tempus quam." | |
"Integer nec odio. Praesent libero." | |
"Sed cursus ante dapibus diam." | |
"Fusce nec tellus sed augue semper porta." | |
"Mauris massa. Vestibulum lacinia arcu eget nulla." | |
"Class aptent taciti sociosqu ad litora torquent per conubia nostra." | |
"In convallis nulla et erat posuere interdum." | |
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem." | |
"Accusantium doloremque laudantium, totam rem aperiam." | |
"Eaque ipsa quae ab illo inventore veritatis et quasi architecto." | |
"Beatae vitae dicta sunt explicabo." | |
"Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit." | |
"Neque porro quisquam est, qui dolorem ipsum quia dolor." | |
"Dolorem sit amet, consectetur, adipisci velit." | |
"Sed quia non numquam eius modi tempora incidunt." | |
"Ut labore et dolore magnam aliquam quaerat voluptatem." | |
"Excepteur sint occaecat cupidatat non proident." | |
"Sunt in culpa qui officia deserunt mollit anim id est laborum." | |
) | |
# Function to download image and get the attachment ID | |
# get_image_attachment_id() { | |
# IMAGE_PATH="/tmp/image_$1.jpg" | |
# curl -L -o "$IMAGE_PATH" https://picsum.photos/1920/1080 | |
# if [[ ! -s $IMAGE_PATH ]]; then | |
# echo "Error: Image $1 could not be downloaded." | |
# return 1 | |
# fi | |
# ATTACHMENT_ID=$(wp media import "$IMAGE_PATH" --porcelain) | |
# echo "$ATTACHMENT_ID" | |
# } | |
# Function to download image, convert to WebP, and get the attachment ID | |
get_image_attachment_id() { | |
IMAGE_PATH="/tmp/image_$1.jpg" | |
WEBP_PATH="/tmp/image_$1.webp" | |
# Download the image from picsum.photos | |
curl -L -o "$IMAGE_PATH" https://picsum.photos/1920/1080 | |
if [[ ! -s $IMAGE_PATH ]]; then | |
echo "Error: Image $1 could not be downloaded." | |
return 1 | |
fi | |
# Convert the downloaded JPG to WebP format using cwebp | |
cwebp -q 60 "$IMAGE_PATH" -o "$WEBP_PATH" | |
if [[ $? -ne 0 ]]; then | |
echo "Error: Failed to convert image $1 to WebP." | |
return 1 | |
fi | |
# Check if the WebP file exists | |
if [[ ! -s $WEBP_PATH ]]; then | |
echo "Error: WebP file for image $1 was not created." | |
return 1 | |
fi | |
# Upload the WebP image to WordPress and get the attachment ID | |
ATTACHMENT_ID=$(wp media import "$WEBP_PATH" --porcelain) | |
if [[ $? -ne 0 ]]; then | |
echo "Error: Failed to upload WebP image $1 to WordPress." | |
return 1 | |
fi | |
# Return the attachment ID | |
echo "$ATTACHMENT_ID" | |
} | |
# Step 1: Create categories, project categories, tags, and rename Uncategorized | |
create_categories | |
create_project_categories | |
rename_uncategorized | |
create_post_tags | |
create_project_tags | |
# Step 2: Loop to create posts, assign categories and tags | |
for i in {1..50}; do | |
POST_ID=$(wp post create --post_title="Post $i" --post_content="$POST_CONTENT" --post_status="publish" --post_type="post" --post_excerpt="${EXCERPTS[$((i-1))]}" --post_author=1 --porcelain) | |
ATTACHMENT_ID=$(get_image_attachment_id "$i") | |
if [[ $? -eq 0 ]]; then | |
wp post meta add "$POST_ID" _thumbnail_id "$ATTACHMENT_ID" | |
else | |
echo "Warning: Failed to assign featured image to Post $i" | |
fi | |
# Assign a random category to the post | |
assign_random_category "$POST_ID" | |
# Assign 2 random tags to the post | |
assign_random_post_tags "$POST_ID" | |
done | |
# Step 3: Loop to create projects, assign project categories and project tags | |
for i in {1..50}; do | |
PROJECT_ID=$(wp post create --post_title="Project $i" --post_content="$POST_CONTENT" --post_status="publish" --post_type="project" --post_excerpt="${EXCERPTS[$((i-1))]}" --post_author=1 --porcelain) | |
ATTACHMENT_ID=$(get_image_attachment_id "$i") | |
if [[ $? -eq 0 ]]; then | |
wp post meta add "$PROJECT_ID" _thumbnail_id "$ATTACHMENT_ID" | |
else | |
echo "Warning: Failed to assign featured image to Project $i" | |
fi | |
# Assign a random project category (project_category) | |
assign_random_project_category "$PROJECT_ID" | |
# Assign 2 random project tags to the project | |
assign_random_project_tags "$PROJECT_ID" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment