Skip to content

Instantly share code, notes, and snippets.

@fabianjaehnke
Last active May 14, 2021 08:34
Show Gist options
  • Save fabianjaehnke/b5e34fd54ec1bf220b0ea5f44a3b9fb1 to your computer and use it in GitHub Desktop.
Save fabianjaehnke/b5e34fd54ec1bf220b0ea5f44a3b9fb1 to your computer and use it in GitHub Desktop.
useful functions for Wordpress functions.php file
<?php
//
// Auto update wordpress core, plugins, themes, translations
//
define( 'AUTOMATIC_UPDATER_DISABLED', true ); //Diasable all
define( 'WP_AUTO_UPDATE_CORE' , true ); //true, false, minor
add_filter( 'auto_update_theme' , '__return_true' );
add_filter( 'auto_update_plugin', '__return_true' );
add_filter( 'auto_update_theme', '__return_true' );
add_filter( 'auto_update_translation', '__return_false' );
//
// Add Odd and Even CSS Classes to WordPress Posts
//
function oddeven_post_class ( $classes ) {
global $current_class;
$classes[] = $current_class;
$current_class = ($current_class == 'odd') ? 'even' : 'odd';
return $classes;
}
add_filter ( 'post_class' , 'oddeven_post_class' );
global $current_class;
$current_class = 'odd';
//
// Change Excerpt Length
//
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
//
// Replaces the excerpt "Read More" text by a link
//
function new_excerpt_more($more) {
global $post;
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
//
// Remove Dashboard Widgets
//
function remove_dashboard_meta() {
remove_meta_box( 'dashboard_welcome', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
// remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
// remove_meta_box( 'dashboard_primary', 'dashboard', 'side' );
remove_meta_box( 'dashboard_secondary', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
// remove_meta_box( 'dashboard_recent_drafts', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
}
add_action( 'admin_init', 'remove_dashboard_meta' );
//
// Custom Footer in admin area
//
function custom_admin_footer() {
// echo '<p />';
echo '<p>some custom footer text</p>';
}
add_filter('admin_footer_text', 'custom_admin_footer');
//
// Restrict CPT to only any number of posts per page
//
function set_posts_per_page_for_towns_cpt( $query ) {
if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'news' ) ) {
$query->set( 'posts_per_page', '9' );
}
}
add_action( 'pre_get_posts', 'set_posts_per_page_for_towns_cpt' );
//
// Reorder CPT
//
function some_func( $query ){
if ( is_post_type_archive('news') && !empty( $query->query['post_type'] == 'news' ) ) {
// $query->set( 'posts_per_page', 2 );
//Set the order ASC or DESC
$query->set( 'order', 'DESC' );
//Set the orderby
$query->set( 'orderby', 'menu_order date' );
// Do stuff
}
}
add_action('pre_get_posts','some_func');
//
// Disable Checked_ontop for Categories
//
add_filter( 'wp_terms_checklist_args', 'checked_not_ontop', 1, 2 );
function checked_not_ontop( $args, $post_id ) {
// if ( 'post' == get_post_type( $post_id ) && $args['taxonomy'] == 'post_category' )
$args['checked_ontop'] = false;
return $args;
}
//
// Tests if any of a post's assigned categories are descendants of target categories
//
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
function post_is_in_descendant_category( $cats, $_post = null ) {
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category' );
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
}
//
// Remove Categories and Tags
//
add_action('init', 'myprefix_remove_tax');
function myprefix_remove_tax() {
register_taxonomy('category', array());
register_taxonomy('post_tag', array());
}
// Remove post meta boxes
function remove_my_post_metaboxes() {
remove_meta_box( 'categorydiv','post','normal' ); // Categories Metabox
remove_meta_box( 'tagsdiv-post_tag','post','normal' ); // Tags Metabox
}
add_action('admin_menu','remove_my_post_metaboxes');
//
// Remove Comments from admin menu
//
add_action( 'admin_menu', 'remove_admin_menus' );
function remove_admin_menus() {
// remove_menu_page( 'edit.php' );
remove_menu_page( 'edit-comments.php' );
// remove_submenu_page( 'edit.php', 'edit-tags.php?taxonomy=post_tag' );
}
//
//Remove comments links from admin bar
//
function fj_disable_comments_admin_bar() {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
}
add_action('init', 'fj_disable_comments_admin_bar');
//
// hide the comment icon in the admin bar
//
add_action( 'admin_bar_menu', 'clean_admin_bar', 999 );
function clean_admin_bar( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'comments' );
}
//
// Remove items from toolbar
//
function remove_toolbar_menus() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu( 'new-post' );
}
//
// Add Additional File Types to be Uploaded
//
function my_myme_types($mime_types){
$mime_types['svg'] = 'image/svg+xml'; //Adding svg extension
$mime_types['psd'] = 'image/vnd.adobe.photoshop'; //Adding photoshop files
return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);
//
// Remove Default Image Links
//
function wpb_imagelink_setup() {
$image_set = get_option( 'image_default_link_type' );
if ($image_set !== 'none') {
update_option('image_default_link_type', 'none');
}
}
add_action('admin_init', 'wpb_imagelink_setup', 10)
//
// Add google Analytics OptOut link for DSGVO/GDPR
//
function google_analytics_optout_handler( $atts, $content = null ) {
return '<a href="javascript:gaOptout();">' . $content . '</a>';
}
add_shortcode( 'google_analytics_optout', 'google_analytics_optout_handler' );
// [google_analytics_optout]Google Analytics deactivation[/google_analytics_optout]
<script type="text/javascript">
var gaProperty = 'UA-XXXXXXX-X';
var disableStr = 'ga-disable-' + gaProperty;
if (document.cookie.indexOf(disableStr + '=true') > -1) {
window[disableStr] = true;
}
function gaOptout() {
document.cookie = disableStr + '=true; expires=Thu, 31 Dec 2099 23:59:59 UTC; path=/';
window[disableStr] = true;
alert('Das Tracking durch Google Analytics wurde in Ihrem Browser für diese Website deaktiviert.');
}
</script>
//
// Dynamic Copyright Date in WordPress Footer
//
function wpb_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
<?php echo wpb_copyright(); ?>
//
// Adding Custom Fonts for tinymce
//
function load_custom_fonts($init) {
$stylesheet_url = get_stylesheet_directory_uri() . '/dist/fonts/custom-fonts.css';
if(empty($init['content_css'])) {
$init['content_css'] = $stylesheet_url;
} else {
$init['content_css'] = $init['content_css'].','.$stylesheet_url;
}
// $font_formats = isset($init['font_formats']) ? $init['font_formats'] : 'Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats';
$font_formats = isset($init['font_formats']) ? $init['font_formats'] : 'HelveticaNeueLTStd-BlkCn=HelveticaNeueLTStd-BlkCn';
$custom_fonts = ';'.'HelveticaNeueLTStd-HvCn=HelveticaNeueLTStd-HvCn;HelveticaNeueLTStd-BdCn=HelveticaNeueLTStd-BdCn;HelveticaNeueLTStd-MdCn=HelveticaNeueLTStd-MdCn;HelveticaNeueLTStd-Cn=HelveticaNeueLTStd-Cn;HelveticaNeueLTStd-LtCn=HelveticaNeueLTStd-LtCn;HelveticaNeueLTStd-ThCn=HelveticaNeueLTStd-ThCn';
$init['font_formats'] = $font_formats . $custom_fonts;
return $init;
}
add_filter('tiny_mce_before_init', 'load_custom_fonts');
function load_custom_fonts_frontend() {
echo '<link type="text/css" rel="stylesheet" href="' . get_stylesheet_directory_uri() . '/dist/fonts/custom-fonts.css">';
}
add_action('wp_head', 'load_custom_fonts_frontend');
add_action('admin_head', 'load_custom_fonts_frontend');
//
// Disable Adminbar on frontend
//
add_filter( 'show_admin_bar', '__return_false' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment