An open API service indexing awesome lists of open source software.

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

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/


Sagitarius dwarf galaxy

Youtube Video


https://www.youtube.com/watch?v=K4TOrB7at0Y


Tweet Thread


https://twitter.com/elonmusk/status/1645266104351178752?s=20


HR 8799 planet orbits




SVG Image
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

Buy Me A Coffee

## License

MIT

---

> [www.hablemosdeseo.net](https://www.hablemosdeseo.net)  · 
> GitHub [@hstanleycrow](https://github.com/hstanleycrow)  · 
> Twitter [@harold_crow](https://twitter.com/harold_crow)