/wp-import-reddit

[POC] Simple cron script for WordPress CMS used for importing embeds of new posts from Reddit.

Primary LanguagePHPMIT LicenseMIT

wp-import-reddit

license paypal

Simple cron script for WordPress CMS used for importing embeds of new posts from Reddit.

This script is proof of concept. Never was used in production.


Usage

If you completed the installation and setup process, you have cron set up and running. Every period of time set in crontab, you should get new posts on your blog if there were any on reddit.

Requirements

Install

# change directory to wp root
$ cd /path/to/your/wp-root

# create cron directory if not exists
$ mkdir cron

# change directory to wp cron root
$ cd cron

# clone this repo
$ git clone https://github.com/oliverfindl/wp-import-reddit.git wp-import-reddit-temp

# copy wp-import-reddit files from repo to wp cron root
$ cp wp-import-reddit-temp/src/wp-import-reddit.php .

# delete repo
$ rm -r wp-import-reddit-temp

# add reddits embed dependency library into functions.php file
$ vim ../wp-content/themes/<THEME-NAME>/functions.php
// reddits embed dependency library
wp_enqueue_script("reddit-embed", "https://embed.redditmedia.com/widgets/platform.js", array(), null, false);

Setup

# set preferred options in wp-import-reddit.php file
$ vim wp-import-reddit.php

Options

define("WP_LOAD_PATH", dirname(__DIR__) . "/wp-load.php"); // path to wp-load.php file
define("WP_POST_STATUS", "publish"); // post status, format: publish|draft|pending|private
define("WP_AUTHOR_ID", 1); // author id from wp, format: integer
define("WP_CATEGORY_IDS", [1]); // array of category ids from wp, format: array of integers

define("DB_FILE_PATH",  __DIR__ . "/wp-import-reddit.sqlite3"); // path to cron sqlite3 database file

define("SINCE_TIME", strtotime("-1 day")); // posts since time for import, format: timestamp
define("UNTIL_TIME", strtotime("-1 hour")); // posts until time for import, format: timestamp

define("REGEXP_URL", "/^https?:\/\/(?:www\.)?(i\.redd\.it|(?:i\.)?imgur\.com|(?:media\.)?giphy\.com|gph\.is|gfycat\.com|youtu(?:be\.com|\.be))\//"); // regular expression for filtering posts based on url of original media, format: false|regexp
# run script manually
/path/to/your/wp-root/cron/wp-import-reddit.php anime manga japan

# add script to crontab
$ crontab -e

0 0 * * * /path/to/your/wp-root/cron/wp-import-reddit.php anime manga japan

Uninstall

# change directory to wp root
$ cd /path/to/your/wp-root

# remove reddits embed dependency library from functions.php file
$ vim wp-content/themes/<THEME-NAME>/functions.php

# change directory to wp cron root
$ cd cron

# remove wp-import-reddit files
$ rm wp-import-reddit.{php,sqlite3}

# remove cron directory if its empty
$ rm -r cron

# remove script from crontab
$ crontab -e

License

MIT