wp_head() Clean & Optimize & Secure WordPress

enable classic editor / disable gutenberg in function.php without plugin wordpress
//code from wp.org/plugins/enable-classic-editor/ and another
if( !function_exists('classic_widgets')){
function classic_widgets(){
remove_theme_support( 'widgets-block-editor' );
}
add_action( 'after_setup_theme', 'classic_widgets' );
}
add_filter('use_block_editor_for_post','__return_false');
add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' );
add_filter( 'use_widgets_block_editor', '__return_false' );

edit /wp-content/themes/namexyz/functions.php
// disable post revisions
define('WP_POST_REVISIONS', 0);

add_action( 'admin_enqueue_scripts', 'txz_admin_enqueue_scripts' );
function wps_cpt_admin_enqueue_scripts() {
if ( 'post' == get_post_type() )
wp_dequeue_script( 'autosave' );
if ( 'txz' == get_post_type() )
wp_dequeue_script( 'autosave' );
}

// removes the link to the Really Simple Discovery service endpoint, EditURI link
remove_action( 'wp_head', 'rsd_link' );

// removes the link to the Windows Live Writer manifest file.
remove_action( 'wp_head', 'wlwmanifest_link' );

// remove WP version on the XHTML generator that is generated on the wp_head hook
remove_action( 'wp_head', 'wp_generator' ); //only web
add_filter( 'the_generator', '__return_false' ); //all switch($type){ (wp-includes/general-template.php)

//Remove All Meta Generators https://stackoverflow.com/questions/16335347/wordpress-how-do-i-remove-meta-generator-tags
ini_set('output_buffering', 'on'); // turns on output_buffering
function remove_meta_generators($html) {
$pattern = '/<meta name(.*)=(.*)"generator"(.*)>/i';
$html = preg_replace($pattern, '', $html);
return $html;
}
function clean_meta_generators($html) {
ob_start('remove_meta_generators');
}
add_action('get_header', 'clean_meta_generators', 100);
add_action('wp_footer', function(){ ob_end_flush(); }, 100);

// remove admin bar
add_filter( 'show_admin_bar', '__return_false' );

// removes all extra rss comment feed links(%postname%/comment/feed/)
add_filter( 'feed_links_show_comments_feed', '__return_false' );

// removes the links to the general feeds: Post and Comment Feed
remove_action( 'wp_head', 'feed_links', 2 );

// removes all extra rss feed links(%postname%/feed/)
remove_action( 'wp_head', 'feed_links_extra', 3 );

// remove emoji js & css
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );

// remove link
remove_action( 'wp_head', 'wp_shortlink_wp_head' ); //short link (domain.ltd/?p=$post-id)
remove_action( 'wp_head', 'index_rel_link' ); // index link
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // prev link
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // start link
remove_action( 'wp_head', 'adjacent_posts_rel_link' ); // Display relational links for the posts adjacent to the current post.

/*remove_action('wp_head','feed_links_extra', 3);
remove_action('wp_head','rsd_link');
remove_action('wp_head','feed_links', 2);
remove_action('wp_head','wlwmanifest_link');
remove_action('wp_head','index_rel_link');
remove_action('wp_head','parent_post_rel_link', 10, 0);
remove_action('wp_head','start_post_rel_link', 10, 0);
remove_action('wp_head','adjacent_posts_rel_link', 10, 0);
remove_action('wp_head','noindex');
remove_action('wp_head','wp_generator');
remove_action('wp_head','rel_canonical');
remove_action('wp_head', 'wp_shortlink_wp_head');*/

// Remove WP Version From Styles
add_filter( 'style_loader_src', 'sdt_remove_ver_css_js', 9999 );
// Remove WP Version From Scripts
add_filter( 'script_loader_src', 'sdt_remove_ver_css_js', 9999 );
// Function to remove version numbers
function sdt_remove_ver_css_js( $src ) {
// if ( strpos( $src, 'ver='. get_bloginfo( 'version' ) ) ) // Remove only the "ver" parameter which have WordPress version
if ( strpos( $src, 'ver=' ) ) // Remove the "ver" parameter from all enqueued CSS and JS files
$src = remove_query_arg( 'ver', $src );
return $src;
}

// move script form wp_head() to wp_footer()
remove_action('wp_head', 'wp_enqueue_scripts', 1); // remove all .css & .js from wp_head()
add_action('wp_footer', 'wp_enqueue_scripts', 5); // add all .css & .js to wp_footer()

// Disable REST API link tag
remove_action('wp_head', 'rest_output_link_wp_head', 10);

// Disable oEmbed Discovery Links
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);

// Disable REST API link in HTTP headers
remove_action('template_redirect', 'rest_output_link_header', 11, 0);


define('WP_POST_REVISIONS', 0);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
remove_action('wp_head', 'rest_output_link_wp_head', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links', 10);
remove_action('template_redirect', 'rest_output_link_header', 11, 0);
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_generator');
add_filter('the_generator', '__return_false');
add_filter('show_admin_bar', '__return_false');
add_filter('feed_links_show_comments_feed', '__return_false');
remove_action('wp_head', 'feed_links_extra', 3);
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
add_filter('style_loader_src', 'sdt_remove_ver_css_js', 9999);
add_filter('script_loader_src', 'sdt_remove_ver_css_js', 9999);
function sdt_remove_ver_css_js( $src ) {
if (strpos($src, 'ver=')) // Remove the "ver" parameter from all enqueued CSS and JS files
$src = remove_query_arg('ver', $src);
return $src;
}


remove yoastseo breadcrumb schema
//https://developer.yoast.com/features/schema/api/#to-add-or-remove-graph-pieces
add_filter( 'wpseo_schema_graph_pieces', 'remove_breadcrumbs_from_schema', 11, 2 );
add_filter( 'wpseo_schema_webpage', 'remove_breadcrumbs_property_from_webpage', 11, 1 );
function remove_breadcrumbs_from_schema( $pieces, $context ) {
return \array_filter( $pieces, function( $piece ) {
return ! $piece instanceof \Yoast\WP\SEO\Generators\Schema\Breadcrumb;
} );
}
function remove_breadcrumbs_property_from_webpage( $data ) {
if (array_key_exists('breadcrumb', $data)) {
unset($data['breadcrumb']);
}
return $data;
}


remove input box [url/website] in wordpress comment & protect htmlcode comment
//remove inputbox url
add_filter('comment_form_default_fields', 'unset_url_field');
function unset_url_field($fields){
if(isset($fields['url']))
unset($fields['url']);
return $fields;
}

/* https://www.wpbeginner.com/beginners-guide/vital-tips-and-tools-to-combat-comment-spam-in-wordpress/ */
function wpb_comment_post( $incoming_comment ) {
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
//$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content']);
return( $incoming_comment );
}
function wpb_comment_display( $comment_to_display ) {
//$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
}
add_filter( 'preprocess_comment', 'wpb_comment_post', '', 1);
add_filter( 'comment_text', 'wpb_comment_display', '', 1);
add_filter( 'comment_text_rss', 'wpb_comment_display', '', 1);
add_filter( 'comment_excerpt', 'wpb_comment_display', '', 1);
remove_filter( 'comment_text', 'make_clickable', 9 );


regex find/replace final output html wordpress
/* https://wordpress.stackexchange.com/questions/52840/filter-all-html-output */
add_action('wp_loaded', 'buffer_start'); function buffer_start() { ob_start("myy_callback"); }
add_action('shutdown', 'buffer_end'); function buffer_end() { ob_end_flush(); }
function myy_callback($buffer) {
// modify buffer here, and then return the updated code
$buffer = str_replace('findword','replaceword',$buffer);
return $buffer;
}

/*only header https://wordpress.stackexchange.com/questions/226696/is-there-a-way-to-read-or-list-wp-head-contents */
function start_wp_head_buffer() {
ob_start();
}
add_action('wp_head','start_wp_head_buffer',0);

function end_wp_head_buffer() {
$in = ob_get_clean();

// here do whatever you want with the header code
echo $in; // output the result unless you want to remove it
}
add_action('wp_head','end_wp_head_buffer', PHP_INT_MAX); //PHP_INT_MAX will ensure this action is called after all other actions that can modify head

//https://stackoverflow.com/questions/30512084/wordpress-replace-meta-description
function remove_meta_descriptions($html) {
$pattern = '//i';
$html = preg_replace($pattern, '', $html);
return $html;
}
function clean_meta_descriptions($html) {
ob_start('remove_meta_descriptions');
}
add_action('get_header', 'clean_meta_descriptions', 100);
add_action('wp_footer', function(){ ob_end_flush(); }, 100);

Blog |
Line it!