https://github.com/hstanleycrow/easyphptowordpress
publish in Wordpress from PHP using WP API
https://github.com/hstanleycrow/easyphptowordpress
php php8 wordpress wordpress-api
Last synced: 2 months ago
JSON representation
publish in Wordpress from PHP using WP API
- Host: GitHub
- URL: https://github.com/hstanleycrow/easyphptowordpress
- Owner: hstanleycrow
- License: mit
- Created: 2023-03-15T22:40:30.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-05-02T23:51:59.000Z (about 3 years ago)
- Last Synced: 2025-01-14T12:25:11.005Z (over 1 year ago)
- Topics: php, php8, wordpress, wordpress-api
- Language: PHP
- Homepage:
- Size: 133 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Easy PHP To Wordpress
Free PHP Library to post from PHP to Wordpress
Key Features •
How To Use •
Download •
Credits •
License
## Key Features
* Get Categories - Get a list of categories from Worpress site
* Get Tags - Get a list of tags from Worpress site
* Publish HTML content, including:
- Featured Image
- Category
- Status: publish, draft, pending, etc
- all images are uploaded to wordpress and the image URL is replaced by the Wordpress URL
- You can include Youtube videos in the content
- You can include Tweets in the content
* Wordpress URL Validator
* Wordpress credentials (user and application password) validator
* Easily handle errors
## How To Use
```bash
# Clone this repository
$ git clone https://github.com/hstanleycrow/EasyPHPToWordpress/
# install libraries
$ composer update
# or install using composer
$ composer require hstanleycrow/easyphptowordpress
```
```php
# define credentials
# You need a Wordpress application password https://make.wordpress.org/core/2020/11/05/application-passwords-integration-guide/
#This examples are using [DotEnv](https://github.com/vlucas/phpdotenv) to configure the credentials, but you can setup it as you want.
$wpSiteURL = $_ENV["WP_SITE_URL"];
$wpUsername = $_ENV["WP_USERNAME"];
$wpApplicationPassword = $_ENV["WP_APPLICATION_PASSWORD"];
# configure in your PHP script the timezone to the Wordpress timezone. This is important.
date_default_timezone_set("America/El_Salvador");
# create an object
$obj = new WordpressAPI($wpSiteURL, $wpUsername, $wpApplicationPassword);
```
### Examples
```php
#Example to validate the URL
if ($obj->validateURL()) :
echo "URL resolve 200";
else :
echo $obj->errorMessage();
endif;
#Example how to get Wordpress Categories list
echo "
";
if ($categories = $obj->categories()) :
print_r($categories);
else :
echo $obj->errorMessage();
endif;
echo "
";
#Example how to get Wordpress Tags list
echo "
";
if ($tags = $obj->tags()) :
print_r($tags);
else :
echo $obj->errorMessage();
endif;
echo "
";
#Example how to validate credentials
echo "
";
if ($obj->validateCredentials()) :
echo "Valid Credentials" . PHP_EOL;
else :
echo "Credentials not valid" . PHP_EOL;
echo $obj->errorMessage() . PHP_EOL;
endif;
echo "
";
#Example to publish into WP
$content = << Hey this is some text for the blog post
Documentation
The documentation for this library is hosted at https://simplehtmldom.sourceforge.io/docs/
Youtube Video
https://www.youtube.com/watch?v=K4TOrB7at0Y
Tweet Thread
https://twitter.com/elonmusk/status/1645266104351178752?s=20

HTML;
$featureImagePath = "https://eluniverso.space/wp-content/uploads/Three-merging-galaxies-1-1024x511.jpg";
$categories = [58];
echo "
";
if ($url = $obj->publishPost(
"Title of the post",
$content,
$categories,
$featureImagePath
)) :
echo $url . PHP_EOL;
if ($obj->hasImagesErrors()) :
print_r($obj->imagesError());
endif;
else :
#echo "Credentials not valid" . PHP_EOL;
echo $obj->errorMessage() . PHP_EOL;
endif;
echo "
";
```
## Limitations
* For now, you can't add tags to the post but it is considered for future versions.
## Download
You can [download](https://github.com/hstanleycrow/EasyPHPToWordpress/) the latest version here.
## PHP Versions
I have tested this class only in this PHP versions. So, if you have an older version and do not work, let me know.
| PHP Version |
| ------------- |
| PHP 8.0 |
| PHP 8.1 |
| PHP 8.2 |
## Credits
This software uses the following open source packages:
- [PHP Simple HTML DOM Parser](https://simplehtmldom.sourceforge.io/docs/1.9/index.html)
## Support
## License
MIT
---
> [www.hablemosdeseo.net](https://www.hablemosdeseo.net) ·
> GitHub [@hstanleycrow](https://github.com/hstanleycrow) ·
> Twitter [@harold_crow](https://twitter.com/harold_crow)
