共计 2230 个字符,预计需要花费 6 分钟才能阅读完成。
文章目录[隐藏]
Puock 主题自带了是光圈的功能, 因此可以使用 memos 的 webhook 功能直接把说说同步到 wordpress.
步骤
在主题文件 functions.php
中末尾处添加
function register_moments_post_type() {
register_post_type('moments',
array(
'public' => true,
'has_archive' => true,
'show_in_rest' => true, // 确保这个参数设置为 true
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
'labels' => array('name' => __('Moments'),
'singular_name' => __('Moment'),
),
)
);
}
add_action('init', 'register_moments_post_type');
在 个人资料
处获取 应用程序密码
安装webhook
使用 docker-compose
部署
docker-compose.yaml
的内容为
services:
webhook:
image: jkjoy/webhook
container_name: webhook
command: -verbose -hooks=hooks.yml -hotreload
environment:
- TZ=Asia/Chongqing #中国时区
- LANG=C.UTF-8 #中文支持
volumes:
- ./config:/config
ports:
- 9000:9000
restart: always
在 hooks.yml
中添加
- id: wordpress
execute-command: "/config/wordpress.sh"
command-working-directory: "/"
wordpress.sh
的内容为
#!/bin/bash
# 设置参数
MEMOS_URL="https://memos.ee/api/v1/memos?pageSize=1&filter=%20creator%20==%20%27users/1%27"
MEMOS_TOKEN=" " #后台获取的 token
WP_SITE_URL="https://" #wordpress 地址
WP_ENDPOINT="/wp-json/wp/v2/moments"
WP_USERNAME="admin" #wordpress 的用户名
WP_APP_PASSWORD="0hc5 b9Ew 9pED ttZ8 8Qjn ms1Z" #后台获取到的应用密码
WP_AUTH="Authorization: Basic $(echo -n "${WP_USERNAME}:${WP_APP_PASSWORD}" | tr -d ' ' | base64)"
# Get Memos data
response=$(curl -s -H "Authorization: Bearer ${MEMOS_TOKEN}" -H "Accept: application/json" "${MEMOS_URL}")
if [$? -eq 0] && echo "${response}" | jq -e . >/dev/null 2>&1; then
echo "${response}" | jq -c '.memos[]' | while read -r memo; do
memos_content=$(echo ${memo} | jq -r '.content')
visibility=$(echo ${memo} | jq -r '.visibility')
if ["${visibility}" == "PUBLIC" ]; then
# URL encode the content for search
encoded_content=$(echo "${memos_content}" | jq -sRr @uri)
already_posted=$(curl -s "${WP_SITE_URL}${WP_ENDPOINT}?search=${encoded_content}" | jq -r 'length // 0')
if [["${already_posted}" =~ ^[0-9]+$ ]] && ["${already_posted}" -eq 0 ]; then
# Escape special characters in content
escaped_content=$(echo "${memos_content}" | jq -R -s '.')
# Create JSON payload with properly escaped content
json_payload="{"title":"New Moment","content":${escaped_content},"status":"publish"}"
# Send request with explicit content type
curl -X POST
"${WP_SITE_URL}${WP_ENDPOINT}"
-H "${WP_AUTH}"
-H "Content-Type: application/json; charset=UTF-8"
-H "Accept: application/json"
--data-binary "${json_payload}"
fi
fi
done
fi
在 memoswebhook 处填入 https://webhook/hooks/wordpress
即可完成全部设置
正文完