Skip to content

Instantly share code, notes, and snippets.

@nathaningram
Last active May 29, 2020 20:19
BBDC 2019 - Custom Functions Plugin
<?php
/*
Plugin Name: Nathan's Awesome Custom Functions Plugin
Plugin URI: https://nathaningram.com
Description: A set of custom functions for client websites from Nathan Ingram's Beaver Builder Developer Course Dec 2019.
Version: 1.0
Author: Nathan Ingram
Author URI: https://nathaningram.com
License: GPL2
*/
/////////////////// WORDPRESS CORE RELATED FUNCTIONS ///////////////////
// Disable Emojis
function ni_disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', 'ni_disable_emojis_tinymce' );
add_filter( 'wp_resource_hints', 'ni_disable_emojis_remove_dns_prefetch', 10, 2 );
}
add_action( 'init', 'ni_disable_emojis' );
function ni_disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
function ni_disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
if ( 'dns-prefetch' == $relation_type ) {
$emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/2/svg/' );
$urls = array_diff( $urls, array( $emoji_svg_url ) );
}
return $urls;
}
// Disables feeds
function ni_clean_feeds() {
// Redirects all feeds to home page.
$url = site_url();
wp_redirect( $url );
}
add_action( 'do_feed', 'ni_clean_feeds', 1 );
add_action( 'do_feed_rdf', 'ni_clean_feeds', 1 );
add_action( 'do_feed_rss', 'ni_clean_feeds', 1 );
add_action( 'do_feed_rss2', 'ni_clean_feeds', 1 );
add_action( 'do_feed_atom', 'ni_clean_feeds', 1 );
add_action( 'do_feed_rss2_comments', 'ni_clean_feeds', 1 );
add_action( 'do_feed_atom_comments', 'dni_clean_feeds', 1 );
// Clean Up WP_Head
remove_action( 'wp_head', 'wp_generator' ); // Removes WordPress version.
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); // Removes shortlink.
remove_action( 'wp_head', 'rsd_link' ); // Removes Really Simple Discovery link.
remove_action( 'wp_head', 'feed_links', 2 ); // Removes RSS feed links.
remove_action( 'wp_head', 'feed_links_extra', 3 ); // Removes all extra RSS feed links.
remove_action( 'wp_head', 'wlwmanifest_link' ); // Removes wlwmanifest.xml.
remove_action( 'wp_head', 'wp_resource_hints', 2 ); // Removes meta rel=dns-prefetch href=//s.w.org
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // Removes relational links for the posts.
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 ); // Removes oEmbeds.
/////////////////// WP-ADMIN CUSTOMIZATIONS ///////////////////
// Replace Howdy
function ni_replace_howdy( $wp_admin_bar ) {
$my_account=$wp_admin_bar->get_node('my-account');
$newtitle = str_replace( 'Howdy,', 'Greetings,', $my_account->title );
$wp_admin_bar->add_node( array(
'id' => 'my-account',
'title' => $newtitle,
) );
}
add_filter( 'admin_bar_menu', 'ni_replace_howdy',25 );
// Remove WP Dashboard Menu
function ni_admin_bar_remove() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
}
add_action('wp_before_admin_bar_render', 'ni_admin_bar_remove', 0);
// Remove Dashboard Widgets
function ni_remove_dashboard_widgets() {
global $wp_meta_boxes;
// Activity Widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
// At a Glance Widget
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
// Quick Draft Widget
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
// News Widget
unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']) ;
}
add_action('wp_dashboard_setup', 'ni_remove_dashboard_widgets',11);
// WordPress Welcome Menu
remove_action('welcome_panel', 'wp_welcome_panel');
// Modify the thank you footer text
add_filter('admin_footer_text', 'ni_modify_footer_admin');
function ni_modify_footer_admin () {
echo '<div style="float:left;margin-right:8px;""><img src="//nathaningram-archive.s3.amazonaws.com/icons/brilliant-logo-32px.png"></div><div style="height:8px;">&nbsp;</div><span><a href="https://brilliantly.net" target="_blank" style="color:#555d66;text-decoration:none;font-weight:bold;">Brilliant Web Works</a> &ndash; Custom WordPress Management System</span>';
}
/////////////////// CREATE CUSTOM DASHBOARD WIDGETS ///////////////////
// Create Custom Client Dashboard Widget
add_action('wp_dashboard_setup', 'ni_custom_dashboard_widget');
function ni_custom_dashboard_widget() {
global $wp_meta_boxes;
wp_add_dashboard_widget('ni_client_widget', '&nbsp;', 'ni_client_widget_content');
}
function ni_client_widget_content() {
$url = get_site_url();
echo '<p style="text-align:center"><img src="//nathaningram-archive.s3.amazonaws.com/icons/brilliant-logo-250w.png" /></p>
<p style="margin: 1.5em 0;"><a style="min-width:45%; text-align:center;float:left;margin:5px;" class="button" href="#" target="_blank" rel="noopener noreferrer"> Google Analytics Instructions</a></p>
<p style="margin: 1.5em 0;"><a style="min-width:45%; text-align:center;float:left;margin:5px;" class="button" href="#">WordPress Help Videos</a></p>
<p style="margin: 1.5em 0;"><a style="min-width:45%; text-align:center;float:left;margin:5px;" class="button" href="#">WordPress Manual</a></p>
<p style="margin: 1.5em 0;"><a style="min-width:45%; text-align:center;float:left;margin:5px;" class="button" href="mailto
:support@wpnathan.com?Subject=BRIEFLY DESCRIBE YOUR ISSUE&body=Change the subject line above to a summary of your issue, then provide more detail here.">Create a Support Ticket</a></p>
<div style="clear:both;float:none;"></div>';
}
//Add a Support Form Widget
function ni_register_custom_dashboard_support_widget() {
wp_add_dashboard_widget(
'custom_dashboard_widget',
'Support Request Form', //Title for Dashboard Widget
'ni_custom_dashboard_support_widget_content'
);
}
function ni_custom_dashboard_support_widget_content() {
echo do_shortcode('[gravityform id="2" title="false" description="false" ajax="true"]'); //Add your shortcode here
}
add_action( 'wp_dashboard_setup', 'ni_register_custom_dashboard_support_widget' );
/////////////////// GRAVITY FORMS RELATED FUNCTIONS ///////////////////
// Give Editors Full Gravity Forms Access
function ni_add_grav_forms(){
$role = get_role('editor');
$role->add_cap('gform_full_access');
}
add_action('admin_init','ni_add_grav_forms');
/////////////////// PODS RELATED FUNCTIONS ///////////////////
// Change Pods MORE FIELDS Metabox Title to Custom
add_filter('pods_meta_default_box_title','ni_change_pod_metabox_title',10,5);
function ni_change_pod_metabox_title($title, $pod, $fields, $type, $name ) {
// assuming we are changing the meta box titles pods named pod1, pod2, and pod3
$title = ($name=='pod1') ? __('More Info', 'plugin_lang') : $title ;
$title = ($name=='pod1') ? __('Even More Info', 'plugin_lang') : $title ;
$title = ($name=='pod3') ? __('Way More Info', 'plugin_lang') : $title ;
return $title;
}
/////////////////// SHORTCODE CREATION ///////////////////
// Site URL Shortcode
function ni_site_url() {
$siteurl = get_site_url();
return $siteurl;
}
add_shortcode('siteurl','ni_site_url');
//Current Year Shortcode
function ni_year_shortcode() {
$year = date('Y');
return $year;
}
add_shortcode('year', 'ni_year_shortcode');
//Anti-Spam Email Shortcode
//Use this shortcode [email]nathan@ithemes.com[/email]
function ni_protect_email_address( $atts , $content=null ) {
for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';';
return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>';
}
add_shortcode('email', 'ni_protect_email_address');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment