Last active
February 18, 2023 01:55
-
-
Save coulterpeterson/d2405425c11fa546fe3bb24f351dadeb to your computer and use it in GitHub Desktop.
Proper WordPress Plugin Boilerplate (With Class, Options Page, Setting Link, and API Helper Function)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: MYPLUGIN | |
Description: MYPLUGIN | |
Version: 1.0.0 | |
Author: MYPLUGIN | |
Author URI: https://MYPLUGIN.com | |
Text Domain: myplugin | |
*/ | |
/* | |
|-------------------------------------------------------------------------- | |
| CONSTANTS | |
|-------------------------------------------------------------------------- | |
*/ | |
// plugin version | |
if(!defined('MYPLUGIN_PLUGIN_VER')) | |
{ | |
define('MYPLUGIN_PLUGIN_VER', '1.0.0'); | |
} | |
// plugin folder url | |
if(!defined('MYPLUGIN_PLUGIN_URL')) | |
{ | |
define('MYPLUGIN_PLUGIN_URL', plugin_dir_url( __FILE__ )); | |
} | |
// plugin folder path | |
if(!defined('MYPLUGIN_PLUGIN_PATH')) | |
{ | |
define('MYPLUGIN_PLUGIN_PATH', plugin_dir_path( __FILE__ )); | |
} | |
/* | |
|-------------------------------------------------------------------------- | |
| MAIN CLASS | |
|-------------------------------------------------------------------------- | |
*/ | |
if ( !class_exists( 'MYPLUGIN' ) ) | |
{ | |
class MYPLUGIN | |
{ | |
public static function init() | |
{ | |
// Hook for shortcode | |
add_shortcode('myplugin', array('MYPLUGIN','shortcode')); | |
// Add Options Page for plugin | |
add_action('admin_menu', array('MYPLUGIN','register_options_page') ); | |
add_action('admin_init', array('MYPLUGIN','register_settings') ); | |
// Add settings link to plugins page listing | |
$plugin = plugin_basename(__FILE__); | |
add_filter("plugin_action_links_$plugin", array('MYPLUGIN', 'add_settings_link') ); | |
// Enqueue scripts | |
add_action( 'wp_enqueue_scripts', array('MYPLUGIN', 'enqueue_scripts') ); | |
// Register Rest Routes | |
add_action('rest_api_init', array('MYPLUGIN', 'register_rest_routes') ); | |
} | |
public static function enqueue_scripts() | |
{ | |
wp_enqueue_style( 'MYPLUGIN-style', plugins_url( 'css/style.css', __FILE__ ), array(), MYPLUGIN_PLUGIN_VER, 'all'); | |
} | |
// Shortcode function | |
public static function shortcode($atts) | |
{ | |
// override default attributes with user attributes | |
$a = shortcode_atts([ | |
"x" => "y", | |
], $atts); | |
$output = ''; | |
return $output; | |
} | |
// Settings link on plugin page | |
public static function add_settings_link($links) | |
{ | |
$settings_link = '<a href="options-general.php?page=myplugin">' . __('Settings') . '</a>'; | |
array_push($links, $settings_link); | |
return $links; | |
} | |
// Register Settings For a Plugin so they are grouped together | |
public static function register_settings() | |
{ | |
add_option('MYPLUGIN_setting', ''); | |
register_setting('MYPLUGIN_options_group', 'MYPLUGIN_setting', null); | |
} | |
// Create an options page | |
public static function register_options_page() | |
{ | |
add_options_page('MYPLUGIN Settings', 'MYPLUGIN Settings', 'manage_options', 'MYPLUGIN', array('MYPLUGIN','options_page')); | |
} | |
// Display Settings on Options Page | |
public static function options_page() | |
{ ?> | |
<div> | |
<h2 style="font-size: 2em;">MYPLUGIN Settings</h2> | |
<form method="post" action="options.php"> | |
<?php settings_fields('MYPLUGIN_options_group'); ?> | |
<input type="text" id="MYPLUGIN_setting" name="MYPLUGIN_setting" value="<?php echo get_option('MYPLUGIN_setting'); ?>" style="width:40%;" /> | |
<?php submit_button(); ?> | |
</form> | |
<?php | |
} | |
public static function register_rest_routes() | |
{ | |
register_rest_route( 'myplugin/v1', 'api-function',array( | |
'methods' => 'POST', | |
'callback' => array('MYPLUGIN', 'api_function'), | |
'permission_callback' => function() { | |
//return current_user_can('edit_posts'); | |
return true; | |
} | |
)); | |
} | |
public static function api_function($response) | |
{ | |
self::write_log('From our API call'); | |
self::write_log($_POST); | |
self::write_log( $response->get_json_params() ); // If payload is sent as a JSON body, this is how you access | |
} | |
private static function makeApiCall( $url, $payload, $method = 'POST' ) | |
{ | |
$response = wp_remote_request( $url, array( | |
'method' => $method, | |
'timeout' => 45, | |
'redirection' => 5, | |
'httpversion' => '1.1', | |
'blocking' => true, | |
'headers' => array( | |
//'Authorization' => 'Basic ' . base64_encode( 'anystring' . ':' . 'themailchimpapikey-us5' ), | |
//'Content-Type' => 'application/json; charset=utf-8', | |
//'Content-Type' => 'multipart/form-data', | |
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8', | |
), | |
//'body' => json_encode($payload), | |
'body' => $payload, | |
//'data_format' => 'body', | |
'cookies' => array(), | |
) ); | |
if ( is_wp_error( $response )) { | |
$error_message = $response->get_error_message(); | |
return array( false, $error_message ); | |
} else if ( wp_remote_retrieve_response_code($response) !== 200 ) { | |
return array( false, $response ); | |
} else { | |
return array( true, wp_remote_retrieve_body($response) ); | |
} | |
} | |
private static function write_log ( $log ) | |
{ | |
if ( is_array( $log ) || is_object( $log ) ) { | |
error_log( print_r( $log, true ) ); | |
} else { | |
error_log( $log ); | |
} | |
} | |
} // End class | |
MYPLUGIN::init(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment