ak86/wp-auto-post

一个简单的PHP库,用于通过编程方式创建Wordpress文章。

v1.0.1 2022-04-23 12:14 UTC

This package is auto-updated.

Last update: 2024-09-23 18:08:59 UTC


README

WP Auto Post 客户端是一个简单的PHP API客户端库,允许通过Wordpress REST API编程创建新文章,无需登录到仪表板。

要求

  • 配置了PHP 7.4或更高版本的Nginx/Apache服务器
  • Composer包管理器
  • 安装了Wordpress 5.6或更高版本的正常工作安装,支持应用程序密码。点击此链接了解如何获取应用程序密码。如果您在WordPress用户配置文件底部看不到应用程序密码部分,您可能需要手动启用它

安装

composer require ak86/wp-auto-post

基本用法

// Require composer autoloader
require_once '/path/to/vendor/autoload.php';

// Import the library
use Ak86\WPAutoPost;

// Set required config vars
$wp_post_endpoint = 'http://yourwordpresssite/wp-json/wp/v2/posts';
$wp_media_endpoint = 'http://yourwordpresssite/wp-json/wp/v2/media';
$wp_auth_username = 'your_wordpress_username';
$wp_auth_password = 'your_wordpress_application_password';

// Initialize the client instance
$wpap_client = new WPAutoPost($wp_post_endpoint, $wp_media_endpoint, $wp_auth_username, $wp_auth_password);

// creating a new post: set post image url 
$imageUrl = 'http://url/to/image.jpg';

// creating a new post: upload the image
$uploaded_image = $wpap_client->upload_image($imageUrl);

// creating a new post: set post data
$postData = array(
    "title" => 'Sample Post Title',
    "content" => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit ...',
    "status" => "publish"
);

// creating a new post: submit post data
$wpap_client->add_wp_post($postData, $uploaded_image->id);

示例应用

请查看以下仓库以获取一个功能演示项目,展示如何使用此库。

https://github.com/amilak86/wp-auto-post-demo

许可证

MIT

作者

Amila Kalansooriya

参考资料