Last active
July 19, 2021 21:31
-
-
Save nathaningram/363edfc98e2f9ded48a712a98b5fbee5 to your computer and use it in GitHub Desktop.
PBDC 2020 - Custom Functions Plugin
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: My Awesome Custom Functions Plugin | |
| Plugin URI: https://nathaningram.com | |
| Description: A set of custom functions for client websites from Nathan Ingram's Page Builder Developer Course Nov 2020 | |
| Version: 1.0 | |
| Author: Nathan Ingram | |
| Author URI: https://nathaningram.com | |
| License: GPL2 | |
| */ | |
| /////////////////// WORDPRESS CORE RELATED FUNCTIONS /////////////////// | |
| // Disable Auto Update Emails for Themes and Plugins | |
| add_filter( 'auto_plugin_update_send_email', '__return_false' ); | |
| add_filter( 'auto_theme_update_send_email', '__return_false' ); | |
| // Disable Post Formats | |
| add_action ('after_setup_theme','ni_no_mo_po_fo',100); | |
| function ni_no_mo_po_fo () { | |
| remove_theme_support( 'post-formats' ); | |
| } | |
| // 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; | |
| } | |
| //Remove Gutenberg Block Library CSS from loading on the frontend | |
| function ni_remove_wp_block_library_css(){ | |
| wp_dequeue_style( 'wp-block-library' ); | |
| wp_dequeue_style( 'wp-block-library-theme' ); | |
| wp_dequeue_style( 'wc-block-style' ); // Remove WooCommerce block CSS | |
| } | |
| add_action( 'wp_enqueue_scripts', 'ni_remove_wp_block_library_css', 100 ); | |
| // 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. | |
| // Disable Post by Email at Settings > Writing | |
| add_filter( 'enable_post_by_email_configuration', '__return_false', 100 ); | |
| // Disable New User Reg Emails to Admin | |
| //From Disable User Registration Notification Emails by Potent Plugins | |
| add_action('init', 'pp_durne_init'); | |
| function pp_durne_init() { | |
| remove_action('register_new_user', 'wp_send_new_user_notifications'); | |
| remove_action('edit_user_created_user', 'wp_send_new_user_notifications', 10, 2); | |
| add_action('register_new_user', 'pp_durne_send_notification'); | |
| add_action('edit_user_created_user', 'pp_durne_send_notification', 10, 2); | |
| } | |
| function pp_durne_send_notification($userId, $to='both') { | |
| if (empty($to) || $to == 'admin') { | |
| return; | |
| } | |
| wp_send_new_user_notifications($userId, 'user'); | |
| } | |
| if ( ! function_exists( 'wp_password_change_notification' ) ) { | |
| function wp_password_change_notification( $user ) { | |
| return; | |
| } | |
| } | |
| /////////////////// 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;"> </div><span><a href="https://brilliantly.net" target="_blank" style="color:#555d66;text-decoration:none;font-weight:bold;">Brilliant Web Works</a> – 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', ' ', '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' ); | |
| /////////////////// PLUGIN RELATED FUNCTIONS /////////////////// | |
| // Give Editors Full Gravity Forms Access | |
| if ( is_plugin_active( 'gravityforms/gravityforms.php' ) ) { | |
| add_action('admin_init','ni_add_grav_forms'); | |
| } | |
| function ni_add_grav_forms(){ | |
| $role = get_role('editor'); | |
| $role->add_cap('gform_full_access'); | |
| } | |
| // 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 | |
| // delete the lines not in use, add as many as needed | |
| $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'); | |
| //Years Since Shortcode | |
| //usage [years-since date='2010-01-01'] | |
| function ni_years_since($atts, $content = null) { | |
| extract(shortcode_atts(array("date" => ''), $atts)); | |
| if(empty($date)) { | |
| return "<br /><br />************No date provided************<br /><br />"; | |
| } | |
| $mdr_unix_date = strtotime($date); | |
| $mdr_time_difference = time() - $mdr_unix_date ; | |
| $years = floor($mdr_time_difference / 31556926 ); | |
| $num_years_since = $years; | |
| return $num_years_since .' years'; | |
| } | |
| add_shortcode('years-since', 'ni_years_since'); | |
| /////////////// Beaver Builder Modifications /////////////// | |
| // Allow Beaver Builder modules to use custom image sizes | |
| add_filter('image_size_names_choose', 'ni_insert_custom_image_sizes'); | |
| function ni_insert_custom_image_sizes($sizes) { | |
| global $_wp_additional_image_sizes; | |
| if (empty($_wp_additional_image_sizes)) { | |
| return $sizes; | |
| } | |
| foreach ($_wp_additional_image_sizes as $id => $data) { | |
| if (!isset($sizes[$id])) { | |
| $sizes[$id] = ucfirst(str_replace('-', ' ', $id)); | |
| } | |
| } | |
| return $sizes; | |
| } | |
| // Removes Empty Modules in Themer if field is empty | |
| function ni_check_field_connections($is_visible, $node) | |
| { | |
| if (isset($node | |
| ->settings | |
| ->connections)) | |
| { | |
| foreach ($node | |
| ->settings->connections as $key => $connection) | |
| { | |
| if (!empty($connection) && empty($node | |
| ->settings | |
| ->$key)) | |
| { | |
| return false; | |
| } | |
| } | |
| } | |
| return $is_visible; | |
| } | |
| add_filter( 'fl_builder_is_node_visible', 'ni_check_field_connections', 10, 2 ); | |
| function ni_remove_slug( $post_link, $post, $leavename ) { | |
| if ( 'linked_article' != $post->post_type || 'publish' != $post->post_status ) { | |
| return $post_link; | |
| } | |
| $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link ); | |
| return $post_link; | |
| } | |
| add_filter( 'post_type_link', 'ni_remove_slug', 10, 3 ); | |
| /////////////////// MISCELLANEOUS FUNCTIONS /////////////////// | |
| // Define Custom Image Sizes | |
| if ( function_exists( 'add_image_size' ) ) { | |
| add_image_size( 'News Grid', 300, 200, true ); | |
| add_image_size( 'News Large', 800, 400, true ); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment