Skip to content

Instantly share code, notes, and snippets.

@XTechnology-TR
Created April 1, 2024 11:28
Show Gist options
  • Save XTechnology-TR/05d8db52691bdb13603647189cd6dceb to your computer and use it in GitHub Desktop.
Save XTechnology-TR/05d8db52691bdb13603647189cd6dceb to your computer and use it in GitHub Desktop.
WordPress'i optimize edin. Gereksiz kodu wp_head'den kaldırın. Geri izlemeleri ve pingleri devre dışı bırakın. Ön uç ve arka uçtaki yorumları devre dışı bırakın ve kaldırın. oEmbed işlevselliğini kaldırın. Ön uçtaki emojileri devre dışı bırakın ve Tinymce Emoji eklentisini kaldırın. Başlıktaki bağlantı etiketlerini kaldırın. jQuery Migrate'ı kal…
<?php
/**
* Strip `width` & `height` attributes from `img` elements.
*
* @file functions.php
*/
function remove_width_and_height_attribute( $html ) {
return preg_replace( '/(height|width)="\d*"\s/', "", $html );
}
add_filter( 'get_image_tag', 'remove_width_and_height_attribute', 10 );
add_filter( 'post_thumbnail_html', 'remove_width_and_height_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_width_and_height_attribute', 10 );
/**
* Prevent file editing via the dashboard editor.
*
* @file wp-config.php
*/
define( 'DISALLOW_FILE_EDIT', true);
?>
<?php
/**
* Display post time in relation to current time.
*
* @file page.php
*/
echo 'Posted ' . human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago.';
/**
* Display estimated reading time, ala Medium, et al.
*
* Assumes a reading speed of 200 words per minute.
*
* @todo make this more WordPress-friendly.
*/
function read_time($text){
$words = str_word_count(strip_tags($text));
$min = floor($words / 200);
if($min === 0){
return 'min read';
}
return $min . 'min read';
}
?>
<?php
/**
* Remove unnecessary self-closing tags
*
* @file functions.php
*/
function remove_self_closing_tags($input) {
return str_replace(' />', '>', $input);
}
add_filter('get_avatar', 'remove_self_closing_tags'); // <img />
add_filter('comment_id_fields', 'remove_self_closing_tags'); // <input />
add_filter('post_thumbnail_html', 'remove_self_closing_tags'); // <img />
?>
<?php
/**
* Add contact notice to Core Update notice
* First, remove the default notice.
* Next, replicate the original function, with our updated notice text.
*
* @todo Find a way to append to the original notice instead of duplicating
*/
add_action( 'admin_init', 'remove_default_update_nag' );
function remove_default_update_nag() {
remove_action( 'admin_notices', 'update_nag', 3 );
remove_action( 'network_admin_notices', 'update_nag', 3 );
}
add_action( 'admin_notices', 'custom_update_nag', 3 );
add_action( 'network_admin_notices', 'custom_update_nag', 3 );
function custom_update_nag();
if ( is_multisite() && !current_user_can('update_core') )
return false;
global $pagenow;
if ( 'update-core.php' == $pagenow )
return;
$cur = get_preferred_from_update_core();
if ( ! isset( $cur->response ) || $cur->response != 'upgrade' )
return false;
if ( current_user_can('update_core') ) {
$msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! <a href="%2$s">Please update now</a>.<br>Reach out to your friends at <a href="http://overhaulmedia.com/" target="_blank">Overhaul Media</a> for assistance!'), $cur->current, network_admin_url( 'update-core.php' ) );
} else {
$msg = sprintf( __('<a href="http://codex.wordpress.org/Version_%1$s">WordPress %1$s</a> is available! Please notify the site administrator.<br>Reach out to your friends at <a href="http://overhaulmedia.com/" target="_blank">Overhaul Media</a> for assistance!'), $cur->current );
}
echo '<div class="update-nag">' . $msg . '</div>';
}
?>
<?php
/**
* Display gallery of attached images
*
* @file page.php
*/
$args = array(
'numberposts' => -1, // Using -1 loads all posts
'orderby' => 'menu_order', // This ensures images are in the order set in the page media manager
'order' => 'ASC',
'post_mime_type' => 'image', // Make sure it doesn't pull other resources, like videos
'post_parent' => $post->ID, // Important part - ensures the associated images are loaded
'post_status' => null,
'post_type' => 'attachment',
);
$images = get_children( $args );
if ( $images ) {
foreach ( $images as $image ) {
$sized_image = wp_get_attachment_image_src( $image->ID, 'medium' );
echo $sized_image[0];
echo $image->guid;
}
}
?>
<?php
/**
* Display featured image thumbnail in post admin columns
*
* @file functions.php
*/
// Setup Admin Thumbnail Size
if ( function_exists( 'add_theme_support' ) ) {
add_image_size( 'admin-thumb', 100, 999999 ); // 100 pixels wide (and unlimited height)
}
// Thumbnails to Admin Post View
add_filter( 'manage_posts_columns', 'posts_columns', 5 );
add_action( 'manage_posts_custom_column', 'posts_custom_columns', 5, 2 );
function posts_columns($defaults){
$defaults['post_thumb'] = __( 'Thumbnail' );
return $defaults;
}
function posts_custom_columns($column_name, $id){
if ( $column_name === 'post_thumb' ) {
the_post_thumbnail( 'admin-thumb' );
}
}
?>
<?php
/**
* Automagically set a Featured Image for a post using the first image in the content.
*
* @file functions.php
*/
function autoset_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if( ! $already_has_thumb ) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if( $attached_image ) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
?>
<?php
/**
* Hide the admin bar for users that can't edit posts.
*
* @file functions.php
*/
add_action('set_current_user', 'hide_the_adminbar');
function hide_the_adminbar() {
if ( ! current_user_can('edit_posts') ) {
show_admin_bar(false);
}
}
?>
<?php
/**
* Adjust post revisioning and autosaving.
*
* @file wp-config.php
*/
// Adjust autosave frequency, in seconds
define('AUTOSAVE_INTERVAL', 300 );
// Limit number of revisions
define('WP_POST_REVISIONS', 3);
// Or disable revisions completely
//define('WP_POST_REVISIONS', false );
?>
<?php
/**
* Display only posts which haven't "expired".
*
* @file page.php
*/
if (have_posts()) :
while (have_posts()) : the_post();
$expirationtime = get_post_custom_values('expiration'); // Get date value from custom field
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// Post content is displayed here
}
endwhile;
endif;
?>
<?php
/**
* Display estimated reading time, ala Medium, et al.
*
* Assumes a reading speed of 200 words per minute.
*
* @todo make this more WordPress-friendly.
*/
function read_time($text){
$words = str_word_count(strip_tags($text));
$min = floor($words / 200);
if($min === 0){
return 'min read';
}
return $min . 'min read';
}
?>
<?php
/**
* Display post time in relation to current time.
*
* @file page.php
*/
echo 'Posted ' . human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago.';
?>
<?php
/**
* Create a Dashboard page with theme-specific settings controls.
*
* @file functions.php
*/
add_action( 'admin_menu', 'my_admin_menu' );
function my_admin_menu() {
add_theme_page( 'My Theme Options', 'My Theme Options', 'edit_theme_options', 'my-theme-options', 'my_theme_options' );
}
function my_theme_options() {
?>
<h2>Theme Options</h2>
<form method="post" action="options.php">
<?php wp_nonce_field( 'update-options' ); ?>
<?php settings_fields( 'my-options' ); ?>
<?php do_settings_sections( 'my-options' ); ?>
<?php submit_button(); ?>
</form>
<?php
}
add_action( 'admin_init', 'my_register_admin_settings' );
function my_register_admin_settings() {
register_setting( 'my-options', 'my-options' );
// Settings fields and sections
add_settings_section( 'section_typography', 'Typography Options', 'my_section_typography', 'my-options' );
add_settings_field( 'primary-font', 'Primary Font', 'my_field_primary_font', 'my-options', 'section_typography' );
}
function my_section_typography() {
echo 'Section description can go here.';
}
function my_field_primary_font() {
echo 'Font control will go here.';
}
?>
<?php
/**
* Custom login screen logo image
*
* Image should be 80px square, or overwrite the default height/width properties.
*
* @file functions.php
*/
function custom_login_logo() {
echo '<style type="text/css">
.login h1 a {
background-image: url(' . get_bloginfo('template_directory') . '/images/logo.png) !important;
background-image: none, url(' . get_bloginfo('template_directory') . '/images/logo.svg) !important;
}
</style>';
}
add_action('login_head', 'custom_login_logo');
?>
<?php
/**
* Allow SVG files in the media uploader
*
* @file functions.php
*/
function add_mime_types( $mimes ){
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter( 'upload_mimes', 'add_mime_types' );
?>
<?php
/**
* Adjust JPG compression quality.
*
* @file functions.php
*/
add_filter( 'jpeg_quality', function($arg) { return 100; } );
add_filter( 'wp_editor_set_quality', function($arg) { return 100; } );
?>
<?php
// Remove Unnecessary Code from wp_head
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
remove_action( 'wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'start_post_rel_link');
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'adjacent_posts_rel_link');
remove_action( 'wp_head', 'feed_links', 2 );
//Remove JQuery migrate
function remove_jquery_migrate($scripts)
{
if (!is_admin() && isset($scripts->registered['jquery'])) {
$script = $scripts->registered['jquery'];
if ($script->deps) { // Check whether the script has any dependencies
$script->deps = array_diff($script->deps, array(
'jquery-migrate'
));
}
}
}
add_action('wp_default_scripts', 'remove_jquery_migrate');
// Remove oEmbed
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
remove_action('rest_api_init', 'wp_oembed_register_route');
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
// Disable Trackbacks and Pings
add_action( 'pre_ping', 'c45_internal_pingbacks' );
add_filter( 'wp_headers', 'c45_x_pingback');
add_filter( 'bloginfo_url', 'c45_pingback_url') ;
add_filter( 'bloginfo', 'c45_pingback_url') ;
add_filter( 'xmlrpc_enabled', '__return_false' );
add_filter( 'xmlrpc_methods', 'c45_xmlrpc_methods' );
// Disable internal pingbacks
function c45_internal_pingbacks( &$links ) {
foreach ( $links as $l => $link ) {
if ( 0 === strpos( $link, get_option( 'home' ) ) ) {
unset( $links[$l] );
}
}
}
// Disable x-pingback
function c45_x_pingback( $headers ) {
unset( $headers['X-Pingback'] );
return $headers;
}
// Remove pingback URLs
function c45_pingback_url( $output, $show='') {
if ( $show == 'pingback_url' ) $output = '';
return $output;
}
// Disable XML-RPC methods
function c45_xmlrpc_methods( $methods ) {
unset( $methods['pingback.ping'] );
return $methods;
}
// Disable and Remove Comments functions
add_action('admin_init', function () {
// Redirect any user trying to access comments page
global $pagenow;
if ($pagenow === 'edit-comments.php') {
wp_redirect(admin_url());
exit;
}
// Remove comments metabox from dashboard
remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');
// Disable support for comments and trackbacks in post types
foreach (get_post_types() as $post_type) {
if (post_type_supports($post_type, 'comments')) {
remove_post_type_support($post_type, 'comments');
remove_post_type_support($post_type, 'trackbacks');
}
}
});
// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);
// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);
// Remove comments page in menu
add_action('admin_menu', function () {
remove_menu_page('edit-comments.php');
});
// Remove comments links from admin bar
add_action('init', function () {
if (is_admin_bar_showing()) {
remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
}
});
// Disable the emoji's
function 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', 'disable_emojis_tinymce' );
add_filter( 'wp_resource_hints', 'disable_emojis_remove_dns_prefetch', 10, 2 );
}
add_action( 'init', 'disable_emojis' );
// Filter function used to remove the tinymce emoji plugin.
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
// Remove emoji CDN hostname from DNS prefetching hints.
function disable_emojis_remove_dns_prefetch( $urls, $relation_type ) {
if ( 'dns-prefetch' == $relation_type ) {
/** This filter is documented in wp-includes/formatting.php */
$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;
}
// Clean up output of stylesheet <link> tags
function clean_style_tag($input) {
preg_match_all("!<link rel='stylesheet'\s?(id='[^']+')?\s+href='(.*)' type='text/css' media='(.*)' />!", $input, $matches);
if (empty($matches[2])) {
return $input;
}
// Only display media if it is meaningful
$media = $matches[3][0] !== '' && $matches[3][0] !== 'all' ? ' media="' . $matches[3][0] . '"' : '';
return '<link rel="stylesheet" href="' . $matches[2][0] . '"' . $media . '>' . "\n";
}
add_filter('style_loader_tag', 'clean_style_tag');
// Include optimize-wp.php in functions.php (optional)
if( file_exists( get_theme_file_path("/inc/optimize-wp.php") ) ) {
include( get_theme_file_path("/inc/optimize-wp.php") );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment