Skip to content

Instantly share code, notes, and snippets.

@gormus
Created October 6, 2019 21:51
Show Gist options
  • Save gormus/2db983347134bc9bcb0247781eff1c8e to your computer and use it in GitHub Desktop.
Save gormus/2db983347134bc9bcb0247781eff1c8e to your computer and use it in GitHub Desktop.
WordPress Must-Use Plugins - Save these files in `wp-content/mu-plugins` to start using them immediately.
<?php
/*
Plugin Name: Override: Admin Menu
Description: Modify admin menu items on the left panel.
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
/**
* Admin menus.
*/
function cake_admin_menu($context) {
// Hide from users with low privilege. Doesn't change access settings though.
if (!current_user_can('manage_options')) {
// Core Menus
// remove_menu_page( 'index.php' ); //Dashboard
// remove_menu_page( 'edit.php' ); //Posts
// remove_menu_page( 'upload.php' ); //Media
// remove_menu_page( 'edit.php?post_type=page' ); //Pages
remove_menu_page( 'edit-comments.php' ); //Comments
// remove_menu_page( 'themes.php' ); //Appearance
// remove_menu_page( 'plugins.php' ); //Plugins
// remove_menu_page( 'users.php' ); //Users
remove_menu_page('tools.php'); //Tools
// remove_menu_page( 'options-general.php' ); //Settings
// Plugins
// remove_menu_page( 'jetpack' ); //Jetpack
}
}
add_action('admin_menu', 'cake_admin_menu', 10, 1);
<?php
/*
Plugin Name: Debug
Description: Debugging related helper functions and overrides.
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
/**
* Allow only superadmins to access kint output.
*/
add_filter('kint_debug_display', function ($allow) {
return is_super_admin();
});
/**
* Prints HTML comments to mark beginning and ending of each template implementing it.
*/
function templateName($label = 'BEGIN', $path = __FILE__) {
if (defined('WP_DEBUG') && WP_DEBUG) {
if ($docroot = getenv('WEBSERVER_DOCROOT')) {
$path = str_replace($docroot, '',$path);
}
echo sprintf("\n<!-- %s TEMPLATE: %s -->\n", $label, $path);
}
}
<?php
/*
Plugin Name: Override: Excerpt Length
Description: Overrides the default excerpt length for all post types.
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
/**
* Set custom excerpt length.
*/
function cake_excerpt_length($number) {
return 20;
}
add_filter('excerpt_length', 'cake_excerpt_length');
<?php
/*
Plugin Name: Override: Flamingo
Description: Enable access to Flamingo pages for all users with permission to edit pages.
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
function cake_flamingo_map_meta_cap($meta_caps) {
$meta_caps = array_merge($meta_caps, array(
'flamingo_edit_contact' => 'edit_pages',
'flamingo_edit_contacts' => 'edit_pages',
'flamingo_delete_contact' => 'edit_pages',
'flamingo_edit_inbound_message' => 'edit_pages',
'flamingo_edit_inbound_messages' => 'edit_pages',
'flamingo_delete_inbound_message' => 'edit_pages',
'flamingo_delete_inbound_messages' => 'edit_pages',
'flamingo_spam_inbound_message' => 'edit_pages',
'flamingo_unspam_inbound_message' => 'edit_pages',
'flamingo_edit_outbound_message' => 'edit_users',
'flamingo_edit_outbound_messages' => 'edit_users',
'flamingo_delete_outbound_message' => 'edit_users',
));
return $meta_caps;
}
add_filter('flamingo_map_meta_cap', 'cake_flamingo_map_meta_cap');
<?php
/*
Plugin Name: In The Press
Description: Creates a new post-type "In The News" for news articles, mentions in the press, etc.
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
/**
* In The Press post type.
*
* @see https://codex.wordpress.org/Function_Reference/register_post_type
*/
function cake_news_post_type() {
$labels = [
'name' => _x('In The Press', 'Post Type General Name', 'cake'),
'singular_name' => _x('News Article', 'Post Type Singular Name', 'cake'),
'menu_name' => __('In The Press', 'cake'),
'name_admin_bar' => __('News Article', 'cake'),
'archives' => __('News Archives', 'cake'),
'attributes' => __('Item Attributes', 'cake'),
'parent_item_colon' => __('Parent Item:', 'cake'),
'all_items' => __('All news articles', 'cake'),
'add_new_item' => __('Add new article', 'cake'),
'add_new' => __('Add new', 'cake'),
'new_item' => __('New article', 'cake'),
'edit_item' => __('Edit article', 'cake'),
'update_item' => __('Update article', 'cake'),
'view_item' => __('View News Article', 'cake'),
'view_items' => __('View News Articles', 'cake'),
'search_items' => __('Search News Articles', 'cake'),
'not_found' => __('Not found', 'cake'),
'not_found_in_trash' => __('Not found in Trash', 'cake'),
'featured_image' => __('Featured Image', 'cake'),
'set_featured_image' => __('Set featured image', 'cake'),
'remove_featured_image' => __('Remove featured image', 'cake'),
'use_featured_image' => __('Use as featured image', 'cake'),
'insert_into_item' => __('Insert into item', 'cake'),
'uploaded_to_this_item' => __('Uploaded to this item', 'cake'),
'items_list' => __('Items list', 'cake'),
'items_list_navigation' => __('Items list navigation', 'cake'),
'filter_items_list' => __('Filter items list', 'cake'),
];
$args = [
'label' => __('News Article', 'cake'),
'labels' => $labels,
'description' => __('News Article information page.', 'cake'),
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-id-alt',
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'supports' => ['title', 'editor', 'author', 'thumbnail', 'excerpt', 'revisions'],
'taxonomies' => ['category'],
'has_archive' => 'about/in-the-news',
'rewrite' => ['slug' => 'about/in-the-news'],
'can_export' => true,
'show_in_rest' => true,
];
register_post_type('news', $args);
}
add_action('init', 'cake_news_post_type');
/**
* Flush Rewrite on Activation.
*/
function cake_news_rewrite_flush() {
cake_news_post_type();
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'cake_news_rewrite_flush');
<?php
/*
Plugin Name: Shortcode: Posts
Description: Creates a simple shortcode to embed a list of posts of selected type, and number. i.e. `[posts type='testimonials' limit='-1' orderby='date' order='DESC']`
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
// Add Shortcode
function cake_posts_shortcode($attributes) {
ob_start();
$attributes = shortcode_atts(
[
'type' => 'post',
'limit' => '10',
'orderby' => 'date',
'order' => 'DESC',
],
$attributes,
'posts'
);
$args = [
'post_type' => $attributes['type'],
'posts_per_page' => $attributes['limit'],
'orderby' => $attributes['orderby'],
'order' => $attributes['order'],
];
$wpQuery = new WP_Query($args);
// $output = '';
if ($wpQuery->have_posts()) {
while ($wpQuery->have_posts()) :
$wpQuery->the_post();
get_template_part('template-parts/content/content', 'excerpt');
endwhile;
}
else {
echo _('There are no posts the display.');
}
wp_reset_query();
}
add_shortcode('posts', 'cake_posts_shortcode');
<?php
/*
Plugin Name: Services
Description: Creates a new post-type Service, and a taxonomy with same name. Taxonomy is linked to post-type.
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
/**
* Cake: Service post type.
*
* @see https://codex.wordpress.org/Function_Reference/register_post_type
*/
function cake_service_post_type() {
$labels = [
'name' => _x('Service', 'Post Type General Name', 'cake'),
'singular_name' => _x('Service', 'Post Type Singular Name', 'cake'),
'menu_name' => __('Services', 'cake'),
'name_admin_bar' => __('Service', 'cake'),
'archives' => __('Services', 'cake'),
'attributes' => __('Item Attributes', 'cake'),
'parent_item_colon' => __('Parent Item:', 'cake'),
'all_items' => __('All Services', 'cake'),
'add_new_item' => __('Add New Service', 'cake'),
'add_new' => __('Add New', 'cake'),
'new_item' => __('New Service', 'cake'),
'edit_item' => __('Edit Service', 'cake'),
'update_item' => __('Update Service', 'cake'),
'view_item' => __('View Service', 'cake'),
'view_items' => __('View Services', 'cake'),
'search_items' => __('Search Services', 'cake'),
'not_found' => __('Not found', 'cake'),
'not_found_in_trash' => __('Not found in Trash', 'cake'),
'featured_image' => __('Featured Image', 'cake'),
'set_featured_image' => __('Set featured image', 'cake'),
'remove_featured_image' => __('Remove featured image', 'cake'),
'use_featured_image' => __('Use as featured image', 'cake'),
'insert_into_item' => __('Insert into item', 'cake'),
'uploaded_to_this_item' => __('Uploaded to this item', 'cake'),
'items_list' => __('Services list', 'cake'),
'items_list_navigation' => __('Services list navigation', 'cake'),
'filter_items_list' => __('Filter items list', 'cake'),
];
$args = [
'label' => __('Service', 'cake'),
'labels' => $labels,
'description' => __('', 'cake'),
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-gallery',
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'supports' => ['title', 'editor', 'author', 'thumbnail', 'excerpt', 'revisions'],
'taxonomies' => ['services'],
'has_archive' => 'services',
'rewrite' => ['slug' => 'services/%services%'],
'can_export' => true,
'show_in_rest' => true,
];
register_post_type('service', $args);
}
add_action('init', 'cake_service_post_type');
/**
* Cake: Services taxonomy used solely for Service post-type.
*/
function cake_service_taxonomy() {
$labels = array(
'name' => _x('Service types', 'Taxonomy General Name', 'cake'),
'singular_name' => _x('Service', 'Taxonomy Singular Name', 'cake'),
'menu_name' => __('Service types', 'cake' ),
'all_items' => __('All Service Types', 'cake'),
'edit_item' => __('Edit Service Type', 'cake'),
'view_item' => __('View Service Type', 'cake'),
'update_item' => __('Update Service Type', 'cake'),
'add_new_item' => __('Add New Service Type', 'cake'),
'new_item_name' => __('New Service Type', 'cake'),
'parent_item' => __('Parent Service Type', 'cake'),
'parent_item_colon' => __('Parent Service Type:', 'cake'),
'search_items' => __('Search Service Types', 'cake'),
'popular_items' => __('Popular Service Types', 'cake'),
'separate_items_with_commas' => __('Separate service types with commas', 'cake'),
'add_or_remove_items' => __('Add or remove service types', 'cake'),
'choose_from_most_used' => __('Choose from the most used service types', 'cake'),
'not_found' => __('Not Found', 'cake'),
'back_to_items' => __('Back to services', 'cake'),
);
$rewrite = array(
'slug' => 'services',
'with_front' => true,
'hierarchical' => true,
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_rest' => true,
'show_tagcloud' => true,
'show_in_quick_edit' => true,
'show_admin_column' => true,
'description' => __('Our available services'),
'hierarchical' => false,
'rewrite' => $rewrite,
);
register_taxonomy( 'services', array( 'service' ), $args );
}
add_action( 'init', 'cake_service_taxonomy', 0 );
function cake_service_post_link($post_link, $id = 0) {
$post = get_post($id);
if (is_object($post)) {
$terms = wp_get_object_terms($post->ID, 'services');
if ($terms) {
return str_replace('%services%', $terms[0]->slug, $post_link);
}
}
return $post_link;
}
add_filter('post_type_link', 'cake_service_post_link', 1, 3);
/**
* Flush Rewrite on Activation.
*/
function cake_service_rewrite_flush() {
cake_service_post_type();
cake_service_taxonomy();
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'cake_service_rewrite_flush');
<?php
/*
Plugin Name: Testimonials
Description: Creates a new post-type "Testimonials". Recommended to use Quote block for post entries.
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
/**
* Testimonial post type.
*
* @see https://codex.wordpress.org/Function_Reference/register_post_type
*/
function cake_testimonial_post_type() {
$labels = [
'name' => _x('Testimonials', 'Post Type General Name', 'cake'),
'singular_name' => _x('Testimonial', 'Post Type Singular Name', 'cake'),
'menu_name' => __('Testimonials', 'cake'),
'name_admin_bar' => __('Testimonial', 'cake'),
'archives' => __('Item Archives', 'cake'),
'attributes' => __('Item Attributes', 'cake'),
'parent_item_colon' => __('Parent Item:', 'cake'),
'all_items' => __('All Testimonials', 'cake'),
'add_new_item' => __('Add New Item', 'cake'),
'add_new' => __('Add New', 'cake'),
'new_item' => __('New Item', 'cake'),
'edit_item' => __('Edit Item', 'cake'),
'update_item' => __('Update Item', 'cake'),
'view_item' => __('View Item', 'cake'),
'view_items' => __('View Testimonials', 'cake'),
'search_items' => __('Search Item', 'cake'),
'not_found' => __('Not found', 'cake'),
'not_found_in_trash' => __('Not found in Trash', 'cake'),
'featured_image' => __('Featured Image', 'cake'),
'set_featured_image' => __('Set featured image', 'cake'),
'remove_featured_image' => __('Remove featured image', 'cake'),
'use_featured_image' => __('Use as featured image', 'cake'),
'insert_into_item' => __('Insert into item', 'cake'),
'uploaded_to_this_item' => __('Uploaded to this item', 'cake'),
'items_list' => __('Testimonials list', 'cake'),
'items_list_navigation' => __('Testimonials list navigation', 'cake'),
'filter_items_list' => __('Filter items list', 'cake'),
];
$args = [
'label' => __('Testimonial', 'cake'),
'labels' => $labels,
'description' => __('Read what others said about us.', 'cake'),
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-format-quote',
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'supports' => ['title', 'editor', 'author', 'thumbnail', 'revisions'],
'taxonomies' => [],
'has_archive' => 'about/testimonials',
'rewrite' => ['slug' => 'about/testimonials'],
'can_export' => true,
'show_in_rest' => true,
];
register_post_type('testimonials', $args);
}
add_action('init', 'cake_testimonial_post_type');
/**
* Flush Rewrite on Activation.
*/
function cake_testimonial_rewrite_flush() {
cake_testimonial_post_type();
flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'cake_testimonial_rewrite_flush');
<?php
/*
Plugin Name: Override: Toolbar
Description: Modify the $wp_admin_bar object before it is used to render the Toolbar to the screen.
Author: Osman Gormus
Version: 1.0.0
Author URI: https://gorm.us/
*/
/**
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/wp_before_admin_bar_render
*/
function cake_wp_before_admin_bar_render() {
global $wp_admin_bar;
//Remove the WordPress logo...
$wp_admin_bar->remove_menu('wp-logo');
}
add_action('wp_before_admin_bar_render', 'cake_wp_before_admin_bar_render');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment