Skip to content

Instantly share code, notes, and snippets.

@Phoenix2k
Last active October 25, 2024 03:59
Show Gist options
  • Save Phoenix2k/1f99cf6c5ba7253442b3 to your computer and use it in GitHub Desktop.
Save Phoenix2k/1f99cf6c5ba7253442b3 to your computer and use it in GitHub Desktop.
WordPress Gists
<?php
/**
* Adds support for `admin.css` (with child-theme support)
*
* You can use this to style anything from edit buttons on the frontend
* to customizing your admin screen for logged in users
*
* @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script
*/
add_action( 'admin_enqueue_scripts', 'theme_admin_styles' );
// Load admin styles on login screen
// add_action( 'login_enqueue_scripts', 'theme_admin_styles' );
// Load in frontend for content editors
if ( current_user_can( 'edit_posts' ) ) {
add_action( 'wp_enqueue_scripts', 'theme_admin_styles' );
}
if ( ! function_exists( 'theme_admin_styles' ) ) {
function theme_admin_styles() {
$css = '/admin.css';
$style = get_bloginfo( 'stylesheet_directory' ) . $css;
$version = filemtime( get_stylesheet_directory() . $css );
wp_enqueue_style( 'admin-style', $style, 'admin-css', $version );
}
}
/**
* Adds support for `login.css` (with child-theme support)
*
* You can use this to customize your login screen
*
* @link http://codex.wordpress.org/Customizing_the_Login_Form
* @link http://codex.wordpress.org/Function_Reference/wp_enqueue_script
*/
add_action( 'login_enqueue_scripts', 'theme_login_styles' );
if ( ! function_exists( 'theme_login_styles' ) ) {
function theme_login_styles() {
$css = '/login.css';
$style = get_bloginfo( 'stylesheet_directory' ) . $css;
$version = filemtime( get_stylesheet_directory() . $css );
wp_enqueue_style( 'login-style', $style, 'login-css', $version );
}
}
/**
* Adds support for `editor-style.css` (with child-theme support)
*
* @link http://codex.wordpress.org/Function_Reference/add_editor_style
*/
add_action( 'admin_init', 'theme_editor_styles' );
if ( ! function_exists( 'theme_editor_styles' ) ) {
function theme_editor_styles() {
$css = '/editor-style.css';
$style = get_bloginfo( 'stylesheet_directory' ) . $css;
add_editor_style( $style );
}
}
/**
* Define custom styles for TinyMCE
*
* @link http://codex.wordpress.org/TinyMCE_Custom_Styles
*/
if ( ! function_exists( 'theme_mce_before_init' ) ) {
add_filter( 'tiny_mce_before_init', 'theme_mce_before_init' );
function theme_mce_before_init( $settings ) {
$style_formats = array(
array(
'title' => __( 'Small text', 'text-domain' ),
'classes' => 'small',
'inline' => 'small',
),
);
$settings['style_formats'] = json_encode( $style_formats );
return $settings;
}
}
/**
* Hide top and sub level admin menus
*
* Please be aware that this will not prevent a user from accessing these screens directly.
* Removing a menu does not replace the need to filter a user's permissions as appropriate.
*
* @link http://codex.wordpress.org/Function_Reference/current_user_can
* @link http://codex.wordpress.org/Function_Reference/remove_menu_page
* @link http://codex.wordpress.org/Function_Reference/remove_submenu_page
*/
add_action( 'admin_menu', 'customize_admin_menu', 9999 );
function customize_admin_menu() {
// Hide based on capabilities
if ( ! current_user_can('capability') ) {
// Hide Dashboard
remove_menu_page( 'index.php' );
// Hide "Updates" under Dashboard menu
remove_submenu_page( 'index.php', 'update-core.php' );
// Hide Posts
remove_menu_page( 'edit.php' );
// Hide Media
remove_menu_page( 'upload.php' );
// Hide Pages
remove_menu_page( 'edit.php?post_type=page' );
// Hide Comments
remove_menu_page( 'edit-comments.php' );
// Hide Appearance
remove_menu_page( 'themes.php' );
// Hide Customizer
remove_submenu_page( 'themes.php', 'customize.php?return=' . urlencode( $_SERVER['REQUEST_URI'] ) );
// Hide Themes
remove_submenu_page( 'themes.php', 'themes.php' );
// Hide Widgets
remove_submenu_page( 'themes.php', 'widgets.php' );
// Hide Plugins
remove_menu_page( 'plugins.php' );
// Hide Users
remove_menu_page( 'users.php' );
// Hide Tools
remove_menu_page( 'tools.php' );
// Hide Settings
remove_menu_page( 'options-general.php' );
}
// Hide according to user email address ending with `@site.com`
if ( ! strpos( get_userdata( get_current_user_id() )->user_email, '@site.com' ) ) {
// Plugin: Advanced Custom Fields
remove_menu_page( 'edit.php?post_type=acf-field-group' );
}
}
/**
* Show second toolbar in TinyMCE by default
*
* @link http://codex.wordpress.org/TinyMCE
*/
add_filter( 'tiny_mce_before_init', 'show_tinymce_toolbar' );
function show_tinymce_toolbar( $in ) {
$in['wordpress_adv_hidden'] = FALSE;
return $in;
}
/**
* Redirect All Users to front page after a successful login
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
*/
add_filter( 'login_redirect', create_function( '$url, $query, $user', 'return home_url();' ), 10, 3 );
/**
* Redirect Subscribers to front page after a successful login
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
*/
add_filter( 'login_redirect', 'theme_login_redirect', 10, 3 );
if ( ! function_exists( 'theme_login_redirect' ) ) {
function theme_login_redirect( $redirect_to, $request, $user ) {
global $user;
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'subscriber', $user->roles ) && strpos( $redirect_to, 'wp-admin' ) ) {
return home_url();
} else {
return $redirect_to;
}
} else {
return $redirect_to;
}
}
}
/**
* Require users to be logged in when `WP_DEBUG` is set to `true`
* (remove `if ( WP_DEBUG ) ` to always require login before accessing the site)
*
* @link https://gist.github.com/richardmtl/a3b7a93131aaeb405990
* @link http://codex.wordpress.org/Function_Reference/auth_redirect
*/
if ( WP_DEBUG ) add_action( 'init', 'check_login_status' );
function check_login_status() {
if ( is_user_logged_in() ) return;
$excluded_pages = array(
'wp-login.php',
'wp-register.php',
'wp-cron.php',
'wp-trackback.php',
'wp-app.php',
'xmlrpc.php'
);
if ( in_array( basename( $_SERVER['PHP_SELF'] ), $excluded_pages ) ) return;
auth_redirect();
}
/**
* 'Save' post events
*
* @param int $post_id The ID of the post
* @param post $post the post
*
* @link http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
*/
add_action( 'save_post', 'theme_save_post', 10, 3 );
if ( ! function_exists( 'theme_save_post' ) ) {
function theme_save_post( $post_id, $post, $update ) {
switch( $post->post_type ) {
// Clear transient
// http://codex.wordpress.org/Transients_API
case 'post-type' :
delete_transient( 'transient_name' );
break;
// Update post contents
case 'post-type' :
// Prevent cascading effect
remove_action( 'save_post', 'theme_save_post' );
// Get the content
$content = $post->post_content;
// Do something with it
// Update post
// http://codex.wordpress.org/Function_Reference/wp_update_post
wp_update_post( array(
'ID' => $post_id,
'post_content' => $content
));
// Restore save event
add_action( 'save_post', 'theme_save_post' );
break;
// WPML: Clear all transients saved using ICL_LANGUAGE_CODE
// http://wpml.org/documentation/support/wpml-coding-api/
case 'post-type' :
if ( function_exists( 'icl_get_languages' ) ) {
$languages = icl_get_languages('skip_missing=0&orderby=code');
if ( ! empty( $languages ) ) {
foreach ( $languages as $language ) {
delete_transient( 'transient_name_' . $language['language_code'] );
}
}
}
break;
}
return $post;
}
}
/**
* Show current instance in Admin Bar if `WP_DEBUG` is set to `true`
* (remove `if ( WP_DEBUG ) ` to always show)
*
* CSS classes: .toolbar-instance, .toolbar-localhost (use theme admin.css)
*
// Default toolbar color
.toolbar-instance {
color: #e0e0e0;
}
.toolbar-instance:hover * {
background: none !important;
color: inherit !important;
}
// Localhost
.toolbar-localhost { color: red; }
*
* Add more classes:
*
$toolbar_classes .= condition ? ' ' . 'toolbar-name' : '';
*
* @link http://codex.wordpress.org/Class_Reference/WP_Admin_Bar
*/
if ( WP_DEBUG ) add_action( 'admin_bar_menu', 'add_current_host_to_admin_bar', 35 );
function add_current_host_to_admin_bar( $wp_admin_bar ) {
$before = '@ <strong>';
$server = $_SERVER['HTTP_HOST'];
$after = '</strong>';
// Define list of custom classes
$toolbar_classes = strstr( $server, 'localhost' ) ? ' toolbar-localhost' : '';
$args = array(
'id' => 'current-host',
'title' => $before . $server . $after,
'href' => false,
'meta' => array(
'class' => 'toolbar-instance' . $toolbar_classes
)
);
$wp_admin_bar->add_node( $args );
}
/**
* Show theme templates used to generate the page when `WP_DEBUG` is set to `true`
* (view page source and scroll all the way down)
*
* @link http://codex.wordpress.org/Template_Hierarchy
*/
if ( WP_DEBUG ) add_action( 'wp_footer', 'show_loaded_templates', 9999 );
function show_loaded_templates() {
$included_files = get_included_files();
$stylesheet_dir = str_replace( '\\', '/', get_stylesheet_directory() );
$template_dir = str_replace( '\\', '/', get_template_directory() );
foreach ( $included_files as $key => $path ) {
$path = str_replace( '\\', '/', $path );
if ( false === strpos( $path, $stylesheet_dir ) && false === strpos( $path, $template_dir ) ) {
unset( $included_files[$key] );
}
}
echo "\n" . "<!--" . "\n\n";
echo " Theme debugging" . "\n\n";
echo " Templates used to generate this page:" . "\n\n";
foreach ( $included_files as $file ) {
// Show templates without revealing their actual location
echo " * " . str_replace( $stylesheet_dir, '', $file ) . "\n";
}
echo "\n" . "-->";
}
}
/**
* Theme login header (with child theme support)
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/login_headertitle
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/login_headerurl
*/
// Change header link title
add_filter( 'login_headertitle', 'theme_login_headertitle' );
if ( ! function_exists( 'theme_login_headertitle' ) ) {
function theme_login_headertitle() {
return __( 'Theme name', 'text-domain' );
}
}
// Use theme home as header link (instead of WordPress.org)
add_filter( 'login_headerurl', 'theme_login_headerurl' );
if ( ! function_exists( 'theme_login_headerurl' ) ) {
function theme_login_headerurl() {
return home_url();
}
}
/**
* Remove admin bar from subscribers
*
* @link http://codex.wordpress.org/Function_Reference/show_admin_bar
*/
if ( ! current_user_can('edit_posts') ) {
show_admin_bar( false );
add_filter( 'show_admin_bar', '__return_false' );
}
<?php
/**
* ACF Options page
*
* @link http://www.advancedcustomfields.com/resources/acf_add_options_page/
*/
if ( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page( array(
'menu_title' => __( 'Options', 'your-theme' ),
'menu_slug' => 'theme-settings',
'capability' => 'switch_themes',
'icon_url' => 'dashicons-feedback',
'redirect' => true
));
acf_add_options_sub_page( array(
'title' => __( 'Theme settings', 'your-theme' ),
'parent' => 'theme-settings',
'capability' => 'switch_themes'
));
}
/**
* Filter data from ACF Relationship fields
*
* @link http://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
*/
add_filter( 'acf/fields/relationship/query', 'acf_relationship_query', 10, 3 );
function acf_relationship_query( $options, $field, $the_post ) {
// Show only published posts
$options['post_status'] = array(
'publish'
);
return $options;
}
<?php
/**
* Compatibility file for bbPress
* All actions are disabled by default. Uncomment # to enable. Use at your own risk!
*/
if ( function_exists( 'bbpress' ) ) :
/**
* Force reload bbPress translation
*/
# add_action( 'get_header','reload_bbpress_translation' );
function reload_bbpress_translation(){
bbpress()->load_textdomain();
}
/**
* Disable BBPress stylesheet
*/
# add_action( 'wp_print_styles', 'deregister_bbpress_styles', 15 );
function deregister_bbpress_styles() {
wp_deregister_style( 'bbp-default' );
}
endif;
<?php
/**
* Gravity Forms: Disable Admin Notifications
*
* @link http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_disable_admin_notification/
*/
add_filter( 'gform_disable_admin_notification', 'disable_gravity_forms_notification', 10, 3 );
function disable_gravity_forms_notification( $is_disabled, $form, $entry ) {
return true;
}
<?php
/**
* Jetpack Compatibility file
* This will remove certain features from Jetpack such as open graph tags, add-on styles
* All actions are disabled by default. Uncomment # to enable. Use at your own risk!
*
* @link http://jetpack.me/
*/
/**
* Add theme support for Infinite Scroll
* See: http://jetpack.me/support/infinite-scroll/
*/
# add_action( 'after_setup_theme', 'load_infinite_scroll' );
function load_infinite_scroll() {
add_theme_support( 'infinite-scroll', array(
'container' => 'main',
'footer' => 'page',
) );
}
/**
* Remove Jetpack styles
*/
# add_action( 'wp_print_styles', 'remove_jetpack_styles' );
function remove_jetpack_styles() {
// After the Deadline
# wp_deregister_style('AtD_style');
// Carousel
# wp_deregister_style('jetpack-carousel');
// Grunion contact form
# wp_deregister_style('grunion.css');
// Infinite Scroll
# wp_deregister_style('the-neverending-homepage');
// Infinite Scroll - Twentyten Theme
# wp_deregister_style('infinity-twentyten');
// Infinite Scroll - Twentyeleven Theme
# wp_deregister_style('infinity-twentyeleven');
// Infinite Scroll - Twentytwelve Theme
# wp_deregister_style('infinity-twentytwelve');
// Notes
# wp_deregister_style('noticons');
// Post by Email
# wp_deregister_style('post-by-email');
// Publicize
# wp_deregister_style('publicize');
// Sharedaddy
# wp_deregister_style('sharedaddy');
// Sharedaddy Sharing
# wp_deregister_style('sharing');
// Stats
# wp_deregister_style('stats_reports_css');
// Widgets
# wp_deregister_style('jetpack-widgets');
}
/**
* Add sharing icons manually by using: <?php echo sharing_display(); ?>
*/
# add_action( 'loop_start', 'remove_jetpack_sharing_display' );
function remove_jetpack_sharing_display() {
remove_filter( 'the_content', 'sharing_display', 19 );
remove_filter( 'the_excerpt', 'sharing_display', 19 );
if ( class_exists( 'Jetpack_Likes' ) ) {
remove_filter( 'the_content', array( Jetpack_Likes::init(), 'post_likes' ), 30, 1 );
}
}
/**
* Change Open Graph image on Front Page
*/
# add_filter( 'jetpack_open_graph_tags', 'change_og_image_on_frontpage' );
function change_og_image_on_frontpage( $tags ) {
unset( $tags['article:author'] );
if ( is_front_page() || ! has_post_thumbnail() ) {
$og_image = get_field( 'og-image', 'options' ); // This uses ACF as an example
if ( ! empty( $og_image ) && isset( $tags['og:image'] ) && is_array( $tags['og:image'] ) ) $tags['og:image'][0] = $og_image;
if ( ! empty( $og_image ) && isset( $tags['twitter:image:src'] ) && is_array( $tags['twitter:image:src'] ) ) $tags['twitter:image:src'][0] = $og_image;
}
return $tags;
}
<?php
/**
* Polylang setup
*
* @link https://polylang.wordpress.com/
*/
/**
* Localize slugs
*
* @link https://github.com/KLicheR/wp-polylang-translate-rewrite-slugs
*/
# add_filter( 'pll_translated_post_type_rewrite_slugs', 'theme_slug_translations' );
function theme_slug_translations( $post_type_translated_slugs ) {
// Add translations
$post_type_translated_slugs = array(
// Post type
'post-type' => array(
'en' => array(
'has_archive' => false,
'rewrite' => array(
'slug' => 'post-type-slug-in-english',
),
),
'fi' => array(
'has_archive' => false,
'rewrite' => array(
'slug' => 'post-type-slug-in-finnish',
),
),
'sv' => array(
'has_archive' => false,
'rewrite' => array(
'slug' => 'post-type-slug-in-swedish',
),
),
),
// Post type
'another-post-type' => array(
'en' => array(
'has_archive' => false,
'rewrite' => array(
'slug' => 'post-type-slug-in-english',
),
),
'fi' => array(
'has_archive' => false,
'rewrite' => array(
'slug' => 'post-type-slug-in-finnish',
),
),
'sv' => array(
'has_archive' => false,
'rewrite' => array(
'slug' => 'post-type-slug-in-swedish',
),
),
)
);
return $post_type_translated_slugs;
}
<?php
/**
* WordPress Compatibility file
* This will remove certain features such as metaboxes, XML-RPC, Emojis and Update Core notices
* All actions are disabled by default. Uncomment # to enable. Use at your own risk!
*/
# add_action( 'init', 'remove_wordpress_features' );
function remove_wordpress_features() {
// Remove RSS feeds
remove_action( 'wp_head', 'feed_links', 2 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
// Remove <link> elements from <head>
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
// Remove WordPress version and shortlink
add_action( 'the_generator', 'remove_version_info' );
function remove_version_info() { return ''; }
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
// Remove inline styles from Tag cloud
add_filter( 'wp_generate_tag_cloud', 'xf_tag_cloud', 10, 3 );
function xf_tag_cloud( $tag_string ) {
return preg_replace( "/style='font-size:.+pt;'/", '', $tag_string );
}
// Remove Emojis from WordPress
add_filter( 'tiny_mce_plugins', 'disable_emojicons_tinymce' );
function disable_emojicons_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
remove_action( 'admin_print_styles', 'print_emoji_styles' );
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_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
}
/**
* Disable XML-RPC functionalities
*
* @link http://codex.wordpress.org/Function_Reference/bloginfo
* @link http://codex.wordpress.org/Plugin_API/Action_Reference/wp
* @link https://developer.wordpress.org/reference/hooks/wp_headers/
* @link https://developer.wordpress.org/reference/hooks/xmlrpc_call/
* @link https://developer.wordpress.org/reference/hooks/xmlrpc_enabled/
* @link https://developer.wordpress.org/reference/hooks/xmlrpc_methods/
*/
// Disable XML-RPC completely
# add_filter( 'xmlrpc_enabled', '__return_false' );
// Disable specific XML-RPC calls
# add_action( 'xmlrpc_call', 'disable_xmlrpc_calls' );
function disable_xmlrpc_calls( $method ) {
switch ( $method ) {
case 'pingback.ping':
wp_die(
'Pingback functionality is disabled on this site',
'Pingback disabled', array( 'response' => 403 )
);
break;
default:
return;
}
}
// Disable X-Pingback HTTP Header
# add_filter( 'wp_headers', 'disable_pingback_header', 11, 2 );
function disable_pingback_header( $headers, $wp_query ) {
if ( isset( $headers['X-Pingback'] ) ) unset( $headers['X-Pingback'] );
return $headers;
}
// Disable pingback_url form get_bloginfo (<link rel="pingback" />)
# add_filter( 'bloginfo_url', 'disable_pingback_url', 11, 2 );
function disable_pingback_url( $output, $property ) {
return ( $property === 'pingback_url' ) ? null : $output;
}
// Remove Pingback method
# add_filter( 'xmlrpc_methods', 'remove_xmlrpc_pingback_method' );
function remove_xmlrpc_pingback_method( $methods ) {
unset( $methods['pingback.ping'] );
return $methods;
}
// Remove rsd_link from filters (<link rel="EditURI" />)
# add_action( 'wp', 'remove_rsd_link', 9 );
function remove_rsd_link() {
remove_action( 'wp_head', 'rsd_link' );
}
/**
* Remove Update Core message from Dashboard
*/
# add_action( 'admin_menu', 'remove_update_core_notices' );
function remove_update_core_notices() {
remove_action( 'admin_notices', 'update_nag', 3 );
}
/**
* Remove 'Posts' tab from admin
*/
# add_action( 'admin_menu','remove_default_post_type' );
function remove_default_post_type() {
remove_menu_page( 'edit.php' );
}
/**
* Move excerpt metabox above editor
*/
# add_action( 'admin_menu' , 'remove_excerpt_metabox' );
function remove_excerpt_metabox() {
remove_meta_box( 'postexcerpt' , 'post' , 'normal' );
remove_meta_box( 'postexcerpt' , 'page' , 'normal' );
remove_meta_box( 'postexcerpt' , 'speaker' , 'normal' );
}
# add_action( 'add_meta_boxes', 'register_excerpt_metabox' );
function register_excerpt_metabox( $post_type ) {
if ( in_array( $post_type, array( 'post', 'page', 'speaker' ) ) ) {
add_meta_box(
'contact_details_meta', __( 'Excerpt' ), 'post_excerpt_meta_box', $post_type, 'above',
'high'
);
}
}
# add_action( 'edit_form_after_title', 'move_excerpt_metabox' );
function move_excerpt_metabox() {
global $post, $wp_meta_boxes;
do_meta_boxes( get_current_screen(), 'above', $post );
}
/**
* Remove Dashboard widgets
* Use 'dashboard-network' as the second parameter to remove widgets from a network dashboard.
* @link http://codex.wordpress.org/Function_Reference/remove_meta_box
*/
# add_action( 'wp_dashboard_setup', 'remove_dashboard_widgets' );
function remove_dashboard_widgets() {
// Right Now
# remove_meta_box('dashboard_right_now', 'dashboard', 'normal');
// Recent Comments
# remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Incoming Links
# remove_meta_box('dashboard_incoming_links', 'dashboard', 'normal');
// Plugins
# remove_meta_box('dashboard_plugins', 'dashboard', 'normal');
// Quick Press
# remove_meta_box('dashboard_quick_press', 'dashboard', 'side');
// Recent Drafts
# remove_meta_box('dashboard_recent_drafts', 'dashboard', 'side');
// Remove WordPress news for all other user except @domain.com
if ( ! strpos( get_userdata( get_current_user_id() )->user_email, 'domain.com' ) ) {
// WordPress blog
remove_meta_box('dashboard_primary', 'dashboard', 'side');
// Other WordPress News
remove_meta_box('dashboard_secondary', 'dashboard', 'side');
}
}
<?php
/**
* Compatibility file for WPML
* @link https://wpml.org/
*/
global $sitepress;
if ( ! empty( $sitepress ) ) :
define( 'ICL_DONT_PROMOTE', true );
define( 'ICL_DONT_LOAD_NAVIGATION_CSS', true );
define( 'ICL_DONT_LOAD_LANGUAGE_SELECTOR_CSS', true );
define( 'ICL_DONT_LOAD_LANGUAGES_JS', true );
/*
* Disable WPML's admin notices
*/
remove_action( 'admin_notices', array( $sitepress, 'icl_reminders' ) );
/*
* Remove WPML's widget from Dashboard
*/
remove_action( 'wp_dashboard_setup', array( $sitepress, 'dashboard_widget_setup' ) );
/**
* Remove WPML's Multilingual Content Setup metabox
* @link http://codex.wordpress.org/Function_Reference/remove_meta_box
*/
add_action( 'admin_head', 'remove_wpml_metaboxes' );
function remove_wpml_metaboxes() {
$screen = get_current_screen();
remove_meta_box( 'icl_div_config', $screen->post_type, 'normal' );
}
/**
* Remove Types Access metabox
* @link https://wp-types.com/forums/topic/hide-access-group-meta-box/
*/
# if ( ! current_user_can( 'manage_options' ) ) {
if ( defined( 'Access_Helper' ) ) {
add_action( 'init', 'remove_content_template' );
function remove_content_template() {
remove_action( 'admin_head', array( Access_Helper, 'wpcf_access_select_group_metabox' ) );
}
}
# }
/**
* Remove WPML's translation queue from subscribers
*/
add_action( 'admin_menu', 'remove_wpml_admin_menu_entries', 9999 );
function remove_wpml_admin_menu_entries() {
if ( ! current_user_can('edit_posts') ) {
remove_menu_page( 'wpml-translation-management/menu/translations-queue.php' );
}
}
/*
* Remove WPML's generator meta tag
*/
add_action( 'wp_head', function() {
remove_action( current_filter(), array( $GLOBALS['sitepress'], 'meta_generator_tag' ) );
}, 0 );
endif;
<?php
/**
* Add new image size options to the list of selectable sizes in the Media Library
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/image_size_names_choose
*/
add_filter( 'image_size_names_choose', 'choose_custom_image_sizes' );
function choose_custom_image_sizes( $sizes ) {
return array_merge( $sizes, array(
'custom-image-size-1' => __( 'Custom Size Name 1', 'text-domain' ),
'custom-image-size-2' => __( 'Custom Size Name 2', 'text-domain' ),
) );
}
/**
* Change crop method for default image sizes
*
* @link http://codex.wordpress.org/Function_Reference/add_image_size
*/
$crop_position_medium = array( 'center', 'center' ); // Options: top, bottom, center / left, right, center
$crop_position_large = array( 'center', 'center' );
// Medium
# if ( ! get_option( 'medium_crop' ) ) add_option( 'medium_crop', $crop_position_medium );
# else update_option( 'medium_crop', $crop_position_medium );
// Full
# if ( ! get_option( 'large_crop' ) ) add_option('large_crop', $crop_position_large );
# else update_option( 'large_crop', $crop_position_large );
/**
* Set the image quality for Thumbnails
*
* @param int $quality The default quality (90)
* @return int $quality Full quality (100)
*/
add_filter( 'jpeg_quality', 'image_quality' );
add_filter( 'wp_editor_set_quality', 'set_custom_image_quality' );
function set_custom_image_quality( $quality ) {
return 100;
}
/**
* Function for getting all available image sizes
*
* @link https://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes
*/
if ( ! function_exists( 'get_image_size' ) ) {
function get_image_size( $size = '' ) {
global $_wp_additional_image_sizes;
$sizes = array();
$get_intermediate_image_sizes = get_intermediate_image_sizes();
// Create the full array with sizes and crop info
foreach( $get_intermediate_image_sizes as $_size ) {
if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
$sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
$sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
$sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' );
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
$sizes[ $_size ] = array(
'width' => $_wp_additional_image_sizes[ $_size ]['width'],
'height' => $_wp_additional_image_sizes[ $_size ]['height'],
'crop' => $_wp_additional_image_sizes[ $_size ]['crop']
);
}
}
// Get only 1 size if found
if ( $size ) {
if ( isset( $sizes[ $size ] ) ) {
return $sizes[ $size ];
} else {
return false;
}
}
return $sizes;
}
}
/**
* Remove height and width attributes from images
*
* @link http://css-tricks.com/snippets/wordpress/remove-width-and-height-attributes-from-inserted-images/
*/
add_filter( 'image_send_to_editor', 'remove_image_attributes', 10 );
add_filter( 'post_thumbnail_html', 'remove_image_attributes', 10 );
function remove_image_attributes( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
return $html;
}
/**
* Remove links from images by default
*
* @link http://andrewnorcross.com/tutorials/stop-hyperlinking-images/
*/
add_action( 'admin_init', 'remove_image_links', 10 );
function remove_image_links() {
$image_set = get_option( 'image_default_link_type' );
if ( $image_set !== 'none' ) {
update_option( 'image_default_link_type', 'none' );
}
}
/**
* Add support for additional File types and Mimes in Media Library
*/
add_filter( 'mime_types', 'theme_additional_upload_mimes' );
function theme_additional_upload_mimes( $existing_mimes ) {
$existing_mimes['svg'] = 'image/svg+xml';
return $existing_mimes;
}
<?php
/**
* Override SEO Framework Open Graph sharing image
*
* @link https://jetpack.com/tag/open-graph/
*/
add_filter( 'the_seo_framework_og_image_args', 'override_the_seo_framework_og_image_args' );
function override_the_seo_framework_og_image_args( $args ) {
$root_directory = get_bloginfo( 'stylesheet_directory' );
$image_path = '/img/social-share.jpg';
$args[ 'image' ] = apply_filters( 'jetpack_photon_url', $root_directory . $image_path );
$args[ 'override' ] = true;
$args[ 'frontpage' ] = true;
return $args;
}
<?php
/**
* Customize Body classes
*
* @link http://codex.wordpress.org/Function_Reference/body_class
*/
add_filter( 'body_class', 'theme_body_classes', 10, 2 );
if ( ! function_exists( 'theme_body_classes' ) ) {
function theme_body_classes( $wp_classes = array(), $extra_classes = array() ) {
global $post;
// List of general classes we want to keep
$whitelist = array(
'admin-bar',
'attachment',
'archive',
'blog',
'category',
'date',
'error404',
'has-post-thumbnail',
'home',
'logged-in',
'page',
'post',
'post-password-required',
'post-type-archive',
'search',
'single',
'singular',
'sticky',
'tag',
);
// List of dynamic classes we want to keep
$dynamic_classes = array(
'attachmentid-([a-zA-Z])', # attachmentid-[attachment-name]
'category-([a-zA-Z])', # category-[category-name]
'page-id-(.)', # page-id-[id]
'postid-(.)', # postid-[id]
'post-type-archive-(.)', # post-type-archive-[post-type]
'single-(.)', # single-[post-format]
'single-format-(.)', # single-format-[post-format]
'tag-([a-zA-Z])', # tag-[tag-name]
);
$pattern = '/(' . implode( '|', $dynamic_classes ) . ')/i';
foreach ( $wp_classes as $wp_class ) {
if ( preg_match( $pattern, $wp_class ) ) array_push( $extra_classes, $wp_class );
}
// Custom classes we want to add
if ( get_post_type() && ! is_attachment() && ! is_search() ) array_push( $extra_classes, 'post-type-' . sanitize_html_class( get_post_type() ) ); # [post-type]-[slug]
if ( is_page_template() ) array_push( $extra_classes, 'template-' . sanitize_html_class( pathinfo( get_page_template() )['filename'] ) ); # template-[filename]
if ( is_front_page() ) array_push( $extra_classes, 'template-front-page' ); # template-front-page
if ( is_home() ) array_push( $extra_classes, 'template-home' ); # template-blog (default)
// Remove non-whitelisted classes
$wp_classes = array_intersect( $wp_classes, $whitelist );
// Merge extra classes and sort everything alphabetically
$theme_classes = array_merge( $wp_classes, $extra_classes );
sort( $theme_classes );
// Return modified classes
return $theme_classes;
}
}
/**
* Customize wp_title behaviour
*
* @param string $title Default title text for current view
* @param string $sep Optional separator
* @return string The filtered title
*
* @link http://codex.wordpress.org/Function_Reference/wp_title#Customizing_with_the_filter
* @requires http://codex.wordpress.org/Title_Tag
*/
add_filter( 'wp_title', 'theme_name_wp_title', 10, 2 );
function theme_name_wp_title( $title, $sep ) {
if ( is_feed() ) return $title;
global $page, $paged, $post;
// Define your own separator
$sep = '-';
// Define default locations like home and front page
$page_titles = array(
'home' => _x( 'News', 'Text for the News-link', 'theme-name' ),
'front-page' => get_bloginfo( 'description', 'display' ) . " $sep " . get_bloginfo( 'name', 'display' ),
'search' => __( 'Search', 'theme-name' ),
'404' => _x( 'Page not found', 'Title for the 404 page', 'theme-name' )
);
// Function for getting post parents
function get_post_parents( $id = 0, $sep = " - " ) {
$current_page = get_post( $id );
$parents = '';
$parent_id = $current_page->post_parent;
while ( $parent_id !== 0 ) {
$parent = get_post( $parent_id );
$page_name = $parent->post_title;
$parents .= " $sep " . $parent->post_title;
$parent_id = $parent->post_parent;
}
return $parents;
}
if ( is_home() ) {
$page_title = $page_titles['home'];
$title = $page_title . " $sep " . $site_title;
} elseif ( is_front_page() ) {
$page_title = $page_titles['front-page'];
$title = $front_page;
} elseif ( is_search() ) {
$page_title = $page_titles['search'];
$title = $page_title . " $sep " . $site_title;
} elseif ( is_404() ) {
$page_title = $page_titles['404'];
$title = $page_title . " $sep " . $site_title;
} elseif ( is_tag() ) {
$page_title = get_theme_archive_title();
$title = $page_title . " $sep " . $site_title;
} elseif ( is_category() ) {
$category = get_the_category();
$page_title = $category[0]->cat_name;
$title = $page_title . " $sep " . $site_title;
} else {
$parents = ! empty( $post->ID ) ? get_post_parents( $post->ID, $sep ) : '';
// Convert UTF-8 characters to HTML entitities
$page_title = htmlspecialchars( get_the_title(), ENT_QUOTES | ENT_IGNORE, "UTF-8");
// Remove HTML special characters (like soft hyphens)
$page_title = html_entity_decode( $page_title );
// Remove HTML tags
$page_title = strip_tags( $page_title );
$title = $page_title . $parents . " $sep " . $site_title;
}
// Add a page number if necessary:
if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
$title .= " $sep " . sprintf( __( 'Page %s', 'theme-name' ), max( $paged, $page ) );
}
return $title;
}
/**
* Theme Archive title
*
* @usage <?php theme_archive_title( '<h1>', '</h1>' ); ?>
* @output <h1>Archive title</h1>
*
* @link https://developer.wordpress.org/reference/functions/the_archive_title/
* @link https://developer.wordpress.org/reference/functions/get_the_archive_title/
*/
function theme_archive_title( $before = '', $after = '' ) {
$title = get_theme_archive_title();
if ( ! empty( $title ) ) {
echo $before . $title . $after;
}
}
function get_theme_archive_title() {
if ( is_category() ) {
$title = sprintf( __( '%s', 'theme-name' ), single_cat_title( '', false ) );
} elseif ( is_tag() ) {
$title = sprintf( __( 'Tag: %s', 'theme-name' ), single_tag_title( '', false ) );
} elseif ( is_author() ) {
$title = sprintf( __( 'Author: %s', 'theme-name' ), '<span class="vcard">' . get_the_author() . '</span>' );
} elseif ( is_year() ) {
$title = sprintf( __( 'Year: %s', 'theme-name' ), get_the_date( _x( 'Y', 'yearly archives date format' ) ) );
} elseif ( is_month() ) {
$title = sprintf( __( 'Month: %s', 'theme-name' ), get_the_date( _x( 'F Y', 'monthly archives date format' ) ) );
} elseif ( is_day() ) {
$title = sprintf( __( 'Day: %s', 'theme-name' ), get_the_date( _x( 'F j, Y', 'daily archives date format' ) ) );
} elseif ( is_tax( 'post_format' ) ) {
if ( is_tax( 'post_format', 'post-format-aside' ) ) {
$title = _x( 'Asides', 'post format archive title', 'theme-name' );
} elseif ( is_tax( 'post_format', 'post-format-gallery', 'theme-name' ) ) {
$title = _x( 'Galleries', 'post format archive title', 'theme-name' );
} elseif ( is_tax( 'post_format', 'post-format-image', 'theme-name' ) ) {
$title = _x( 'Images', 'post format archive title', 'theme-name' );
} elseif ( is_tax( 'post_format', 'post-format-video', 'theme-name' ) ) {
$title = _x( 'Videos', 'post format archive title', 'theme-name' );
} elseif ( is_tax( 'post_format', 'post-format-quote', 'theme-name' ) ) {
$title = _x( 'Quotes', 'post format archive title', 'theme-name' );
} elseif ( is_tax( 'post_format', 'post-format-link', 'theme-name' ) ) {
$title = _x( 'Links', 'post format archive title', 'theme-name' );
} elseif ( is_tax( 'post_format', 'post-format-status', 'theme-name' ) ) {
$title = _x( 'Statuses', 'post format archive title', 'theme-name' );
} elseif ( is_tax( 'post_format', 'post-format-audio', 'theme-name' ) ) {
$title = _x( 'Audio', 'post format archive title', 'theme-name' );
} elseif ( is_tax( 'post_format', 'post-format-chat', 'theme-name' ) ) {
$title = _x( 'Chats', 'post format archive title', 'theme-name' );
}
} elseif ( is_post_type_archive() ) {
$title = sprintf( __( 'Archives: %s', 'theme-name' ), post_type_archive_title( '', false ) );
} elseif ( is_tax() ) {
$tax = get_taxonomy( get_queried_object()->taxonomy );
$title = sprintf( __( '%1$s: %2$s' ), $tax->labels->singular_name, single_term_title( '', false ) );
} else {
$title = __( 'Archives', 'theme-name' );
}
return apply_filters( 'get_the_archive_title', $title );
}
/**
* Removes attributes and whitespace from tables
*
* @param $html_table String containing HTML
* @param $settings Array containing attributes to be removed (supports 'height', 'width', 'style', 'whitespace')
*
* @usage <?php echo theme_clean_table( $content, array( 'style', 'width' ) ); ?>
* @return $string Cleaned HTML output
*/
function theme_clean_table( $html_table, $settings, $echo = false ) {
if ( empty( $html_table ) ) return;
if ( ! array( $settings ) ) $settings = array(
'style',
'width',
'height',
'whitespace'
);
// Removes inline styles
if ( ! function_exists('remove_inline_styles') ) {
function remove_inline_styles( $string ) {
return preg_replace( '/ style=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$2', $string );
}
}
// Removes width attributes
if ( ! function_exists('remove_width_attributes') ) {
function remove_width_attributes( $string ) {
return preg_replace( '/ width=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$2', $string );
}
}
// Removes height attributes
if ( ! function_exists('remove_height_attributes') ) {
function remove_height_attributes( $string ) {
return preg_replace( '/ height=("[^"]+"|\'[^\']+\')([^>]*>)/i', '$2', $string );
}
}
// Removes whitespace from cells (including `no-break` space)
if ( ! function_exists('remove_whitespace_from_table_cells') ) {
function remove_whitespace_from_table_cells( $string ) {
$string = hex2bin( preg_replace('/c2a0/', '', bin2hex( $string ) ) ); # 0xC2 0xA0 (c2a0)
$string = preg_replace('/<t([d|h])([^>]*)>([^a-zA-Z0-9*+-=<>]+)/', '<t$1$2>', $string);
$string = preg_replace('/([^a-zäöåàA-ZÄÖÅÀ0-9\.\$,;:!*#€%&\(\)+-=?<>]+)<\/t([d|h])>/', '</t$2>', $string);
return $string;
}
}
foreach ( $settings as $function_name ) {
switch( $function_name ) {
case 'style':
$html_table = remove_inline_styles( $html_table );
case 'width':
$html_table = remove_width_attributes( $html_table );
case 'height':
$html_table = remove_height_attributes( $html_table );
case 'whitespace':
$html_table = remove_whitespace_from_table_cells( $html_table );
}
}
// Run table through the content filter
$html_table = apply_filters( 'the_content', $html_table );
$html_table = str_replace( ']]>', ']]&gt;', $html_table );
if ( $echo ) echo $html_table; else return $html_table;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment