Skip to content

Instantly share code, notes, and snippets.

@shaunpalmer
Last active September 15, 2024 14:33
Show Gist options
  • Save shaunpalmer/e8ca6c2f8307cd1414fe58294912b93f to your computer and use it in GitHub Desktop.
Save shaunpalmer/e8ca6c2f8307cd1414fe58294912b93f to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Site Plugin
Description: Site-specific code with support for Gutenberg blocks
Author: Shaun Palmer
Author URI: https://yourwebsite.com/
*/
class SP_SitePlugin {
public function __construct() {
// General hooks and actions
add_filter('widget_text', [$this, 'execute_php'], 100);
add_filter('widget_text', 'do_shortcode');
add_action('pre_ping', [$this, 'disable_internal_ping']);
add_filter('the_excerpt_rss', [$this, 'add_thumbnail_to_rss']);
add_filter('the_content_feed', [$this, 'add_thumbnail_to_rss']);
add_action('after_setup_theme', [$this, 'add_editor_styles']);
add_filter('tiny_mce_before_init', [$this, 'override_mce_options']);
add_action('admin_bar_menu', [$this, 'edit_toolbar'], 999);
add_filter('upload_mimes', [$this, 'add_upload_mimes']);
add_filter('manage_posts_columns', [$this, 'posts_columns'], 5);
add_action('manage_posts_custom_column', [$this, 'posts_custom_columns'], 5, 2);
add_action('admin_enqueue_scripts', [$this, 'admin_styles']);
// Check if Gutenberg is available, and register blocks if so
if (function_exists('register_block_type')) {
add_action('init', [$this, 'register_gutenberg_blocks']);
}
}
// Execute PHP in text widgets
public function execute_php($html) {
if (strpos($html, "<?php") !== false) {
ob_start();
eval("?>" . $html);
$html = ob_get_contents();
ob_end_clean();
}
return $html;
}
// Disable internal pingbacks
public function disable_internal_ping(&$links) {
$home = get_option('home');
foreach ($links as $l => $link) {
if (0 === strpos($link, $home)) {
unset($links[$l]);
}
}
}
// Add featured image to RSS feed
public function add_thumbnail_to_rss($excerpt) {
global $post;
$content = '<p>' . get_the_post_thumbnail($post->ID) . '</p>' . get_the_excerpt();
return $content;
}
// Add editor styles
public function add_editor_styles() {
add_editor_style('custom-editor-style.css');
}
// Override TinyMCE options
public function override_mce_options($initArray) {
$opts = '*[*]';
$initArray['valid_elements'] = $opts;
$initArray['extended_valid_elements'] = $opts;
return $initArray;
}
// Edit the admin toolbar
public function edit_toolbar($wp_toolbar) {
$wp_toolbar->remove_node('wp-logo');
$wp_toolbar->remove_node('site-name');
$wp_toolbar->remove_node('updates');
$wp_toolbar->remove_node('comments');
$wp_toolbar->remove_node('new-content');
$wp_toolbar->remove_node('top-secondary');
}
// Add custom MIME types for file uploads
public function add_upload_mimes($mimes) {
$mimes['ott|odt|zip|ReaperThemeZip|ReaperConfigZip'] = 'application/octet-stream';
return $mimes;
}
// Add a thumbnail column to the post table
public function posts_columns($defaults) {
$defaults['riv_post_thumbs'] = __('Thumbs');
return $defaults;
}
public function posts_custom_columns($column_name, $id) {
if ($column_name === 'riv_post_thumbs') {
echo the_post_thumbnail('featured-thumbnail');
}
}
// Add admin CSS
public function admin_styles() {
wp_register_style('sp_admin_stylesheet', plugins_url('/admin.css', __FILE__));
wp_enqueue_style('sp_admin_stylesheet');
}
// Register Gutenberg blocks
public function register_gutenberg_blocks() {
// Block assets
wp_register_script(
'sp-gutenberg-block',
plugins_url('/blocks/block.js', __FILE__),
['wp-blocks', 'wp-element', 'wp-editor'],
filemtime(plugin_dir_path(__FILE__) . '/blocks/block.js')
);
wp_register_style(
'sp-block-editor-styles',
plugins_url('/blocks/editor.css', __FILE__),
['wp-edit-blocks'],
filemtime(plugin_dir_path(__FILE__) . '/blocks/editor.css')
);
wp_register_style(
'sp-block-styles',
plugins_url('/blocks/style.css', __FILE__),
[],
filemtime(plugin_dir_path(__FILE__) . '/blocks/style.css')
);
// Registering block
register_block_type('sp/custom-block', [
'editor_script' => 'sp-gutenberg-block',
'editor_style' => 'sp-block-editor-styles',
'style' => 'sp-block-styles',
]);
}
}
// Initialize the plugin
$SP_SitePlugin = new SP_SitePlugin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment