Created
November 21, 2023 15:10
-
-
Save dexit/5f6bff0c4328275d8ef98aa8df0acbf8 to your computer and use it in GitHub Desktop.
PHP wordpress funcions quick list
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 | |
/** | |
* Add some security headers | |
* | |
* @package wp-tweaks | |
*/ | |
if ( ! defined( 'WPINC' ) ) die(); | |
add_action( 'wp_headers', 'wp_tweaks_security_headers' ); | |
function wp_tweaks_security_headers ( $headers ) { | |
$options = WP_Tweaks::get_option( 'security-headers' ); | |
$values = apply_filters( | |
'wp_tweaks_security_headers_values', | |
[ | |
'strict-transport-security' => 'max-age=31536000; includeSubDomains; preload', | |
'x-content-type-options' => 'nosniff', | |
'x-xss-protection' => '1; mode=block', | |
'content-security-policy' => "default-src https:; font-src https: data:; img-src https: data:; script-src https: 'unsafe-inline' 'unsafe-eval'; style-src https: 'unsafe-inline'", | |
'x-frame-options' => 'sameorigin', | |
'referrer-policy' => 'strict-origin-when-cross-origin', | |
'permissions-policy' => 'geolocation=(), microphone=(), camera=(), interest-cohort=()' | |
] | |
); | |
foreach ( $options as $key ) { | |
$headers[ $key ] = $values[ $key ]; | |
} | |
return $headers; | |
} | |
# 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' ); | |
} | |
// Remove Pingback method | |
add_filter( 'xmlrpc_methods', 'remove_xmlrpc_pingback_method' ); | |
function remove_xmlrpc_pingback_method( $methods ) { | |
unset( $methods['pingback.ping'] ); | |
return $methods; | |
} | |
// 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; | |
} | |
// 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 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; | |
} | |
} | |
/** | |
* 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; | |
} | |
/** | |
* 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 | |
// Return modified classes | |
return $theme_classes; | |
} | |
} | |
/** | |
* Remove WordPress version number in frontend | |
* | |
* @package wp-tweaks | |
*/ | |
if ( ! defined( 'WPINC' ) ) die(); | |
add_filter( 'the_generator', '__return_empty_string' ); | |
/** | |
* Disable Emoji Mess | |
* | |
* @package wp-tweaks | |
*/ | |
if ( ! defined( 'WPINC' ) ) die(); | |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); | |
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' ); | |
remove_action( 'embed_head', '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', 'wp_tweaks_disable_emojicons_tinymce' ); | |
add_filter( 'emoji_svg_url', '__return_false' ); | |
function wp_tweaks_disable_emojicons_tinymce ( $plugins ) { | |
return is_array( $plugins ) ? array_diff( $plugins, [ 'wpemoji' ] ) : []; | |
} | |
/** | |
* Remove WordPress version number in frontend | |
* | |
* @package wp-tweaks | |
*/ | |
if ( ! defined( 'WPINC' ) ) die(); | |
add_filter( 'the_generator', '__return_empty_string' ); | |
/** | |
* Disable `/users` endpoint in REST API | |
* | |
* @package wp-tweaks | |
*/ | |
if ( ! defined( 'WPINC' ) ) die(); | |
add_filter( 'rest_endpoints', 'wp_tweaks_disable_rest_api_users_endpoint' ); | |
function wp_tweaks_disable_rest_api_users_endpoint ( $endpoints ) { | |
if ( isset( $endpoints['/wp/v2/users'] ) ) { | |
unset( $endpoints['/wp/v2/users'] ); | |
} | |
if ( isset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ) ) { | |
unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] ); | |
} | |
return $endpoints; | |
} | |
/** | |
* Display generic error message in login form | |
* | |
* @package wp-tweaks | |
*/ | |
if ( ! defined( 'WPINC' ) ) die(); | |
add_filter( 'wp_login_errors', 'wp_tweaks_login_error', 99 ); | |
function wp_tweaks_login_error ( $errors ) { | |
if ( ! is_wp_error( $errors ) ) return new WP_Error(); | |
$has_login_error = false; | |
$login_errors = [ | |
'empty_username', | |
'invalid_username', | |
'empty_password', | |
'incorrect_password', | |
'invalid_email', | |
]; | |
foreach ( $login_errors as $code ) { | |
if ( 0 === count( $errors->get_error_messages( $code ) ) ) { | |
continue; | |
} | |
$errors->remove( $code ); | |
$has_login_error = true; | |
} | |
if ( $has_login_error ) { | |
$errors->add( | |
'invalid', | |
apply_filters( | |
'wp_tweaks_login_error', | |
esc_html__( 'Incorrect username or password.', 'wp-tweaks' ) | |
) | |
); | |
} | |
return $errors; | |
} | |
/** | |
* Hide WordPress version in admin footer | |
* | |
* @package wp-tweaks | |
*/ | |
if ( ! defined( 'WPINC' ) ) die(); | |
add_action( 'admin_menu', 'wp_tweaks_hide_version_in_admin_footer' ); | |
function wp_tweaks_hide_version_in_admin_footer () { | |
if ( ! current_user_can( 'install_plugins' ) ) { | |
remove_filter( 'update_footer', 'core_update_footer' ); | |
} | |
} | |
/** | |
* Remove some Dashboard Widgets | |
* | |
* @package wp-tweaks | |
*/ | |
if ( ! defined( 'WPINC' ) ) die(); | |
add_action( 'admin_init', 'wp_tweaks_remove_dashboard_widgets' ); | |
function wp_tweaks_remove_dashboard_widgets () { | |
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_comments', 'dashboard', 'normal' ); | |
if ( ! current_user_can('administrator') ) { | |
remove_meta_box( 'dashboard_site_health', 'dashboard', 'normal' ); | |
} | |
} | |
// Remove classic theme styles. | |
// https://github.com/WordPress/WordPress/commit/143fd4c1f71fe7d5f6bd7b64c491d9644d861355 | |
function remove_classic_theme_styles(): void | |
{ | |
wp_dequeue_style('classic-theme-styles'); | |
} | |
// Remove Gutenberg's front-end block styles. | |
function remove_block_styles(): void | |
{ | |
wp_deregister_style('wp-block-library'); | |
wp_deregister_style('wp-block-library-theme'); | |
} | |
add_action('wp_enqueue_scripts','remove_block_styles'); | |
// Remove Gutenberg's global styles. | |
// https://github.com/WordPress/gutenberg/pull/34334#issuecomment-911531705 | |
function remove_global_styles(): void | |
{ | |
wp_dequeue_style('global-styles'); | |
} | |
add_action('wp_enqueue_scripts','remove_global_styles'); | |
function disable_rest_endpoints(array $endpoints): array | |
{ | |
if (!is_user_logged_in()) { | |
if (isset($endpoints['/wp/v2/users'])) { | |
unset($endpoints['/wp/v2/users']); | |
} | |
if (isset($endpoints['/wp/v2/users/(?P<id>[\d]+)'])) { | |
unset($endpoints['/wp/v2/users/(?P<id>[\d]+)']); | |
} | |
} | |
return $endpoints; | |
} | |
add_filter('rest_endpoints','disable_rest_endpoints'); | |
// Remove JPEG compression. | |
function remove_jpeg_compression(): int | |
{ | |
return 70; | |
} | |
add_filter('jpeg_quality','remove_jpeg_compression', 10, 2); | |
/** | |
* Remove query strings from resources. | |
* | |
* @param string $src The link from which to remove the version query string. | |
*/ | |
function apc_cleanup_query_string( $src ) { | |
$parts = explode( '?ver', $src ); | |
return $parts[0]; | |
} | |
add_filter( 'script_loader_src', 'apc_cleanup_query_string', 15, 1 ); | |
add_filter( 'style_loader_src', 'apc_cleanup_query_string', 15, 1 ); | |
/** | |
* Add DNS Prefetching and Preconnecting Resource Hints to the <head> | |
* | |
* Preload: fetch high priority resource used in current route | |
* Preconnect: resolves DNS and TCP handshaking | |
* Prefetch: fetches resources probably needed for next page load (low priority) | |
*/ | |
function swm_dns_prefetch_preconnect() { | |
echo " | |
<meta https-equiv='x-dns-prefetch-control' content='on'> | |
<link rel='dns-prefetch' href='//s.gravatar.com/' /> | |
<link rel='dns-prefetch' href='//0.gravatar.com/' /> | |
<link rel='dns-prefetch' href='//1.gravatar.com/' /> | |
<link rel='dns-prefetch' href='//2.gravatar.com/' /> | |
<link rel='dns-prefetch' href='//maps.googleapis.com/' /> | |
<link rel='dns-prefetch' href='//maps.gstatic.com/' /> | |
<link rel='dns-prefetch' href='//ajax.googleapis.com/' /> | |
<link rel='dns-prefetch' href='//fonts.gstatic.com/' /> | |
<link rel='preconnect' href='//fonts.gstatic.com/' crossorigin> | |
<link rel='dns-prefetch' href='//fonts.googleapis.com' /> | |
<link rel='preconnect' href='//fonts.googleapis.com/' crossorigin> | |
<link rel='dns-prefetch' href='//apis.google.com/' /> | |
<link rel='dns-prefetch' href='//youtube.com/' /> | |
<link rel='dns-prefetch' href='//s0.wp.com/' /> | |
<link rel='dns-prefetch' href='//s1.wp.com/' /> | |
<link rel='dns-prefetch' href='//s2.wp.com/' /> | |
<link rel='dns-prefetch' href='//stats.wp.com/' /> | |
"; | |
/* | |
<link rel='dns-prefetch' href='//api.pinterest.com/' /> | |
<link rel='dns-prefetch' href='//google-analytics.com/' /> | |
<link rel='dns-prefetch' href='//www.google-analytics.com/' /> | |
<link rel='dns-prefetch' href='//ssl.google-analytics.com/' /> | |
//cdnjs.cloudflare.com | |
//pixel.wp.com | |
//connect.facebook.net | |
//platform.twitter.com | |
//syndication.twitter.com | |
//platform.instagram.com | |
//disqus.com | |
//sitename.disqus.com | |
//s7.addthis.com | |
//platform.linkedin.com | |
//w.sharethis.com | |
Not needed because it is included with DIVI: | |
*/ | |
} | |
add_action( 'wp_head', 'swm_dns_prefetch_preconnect', 0 ); | |
/** | |
* Filter the except length to 20 words. | |
* | |
* By default, the excerpt length is 50 words. | |
* | |
* @param int $length Excerpt length. | |
* @return int (Maybe) modified excerpt length. | |
*/ | |
function apway_custom_excerpt_length( $length ) { | |
return 20; | |
} | |
add_filter( 'excerpt_length', 'apway_custom_excerpt_length', 999 ); | |
/** | |
* Filter the "read more" excerpt string link to the post. | |
* | |
* @param string $more "Read more" excerpt string. | |
* @return string (Maybe) modified "read more" excerpt string. | |
*/ | |
function apway_excerpt_readmore( $more ) { | |
if ( ! is_single() ) { | |
$more = sprintf( | |
'<div class="view-full-post"><a class="read-more" href="%1$s" class="view-full-post-btn">… %2$s »</a></div>', | |
get_permalink( get_the_ID() ), | |
__( 'Read More', 'textdomain' ) | |
); | |
} | |
return $more; | |
} | |
add_filter( 'excerpt_more', 'apway_excerpt_readmore' ); | |
/** | |
* Change the `Read More` text after the trimmed excerpt | |
* | |
* Filters the string in the “more” link displayed after a trimmed excerpt. This | |
* filter is used by wp_trim_excerpt() function. By default it is set to echo '[…]' | |
* more string at the end of the excerpt. | |
* | |
* @see https://developer.wordpress.org/reference/hooks/excerpt_more/ | |
* | |
* @param string $translated "Read more" excerpt string. | |
* @return string $translated modified "read more" excerpt string. | |
*/ | |
function apway_translate_readmore_text( $translated ) { | |
$translated = str_ireplace( 'read more', '… Read More »', $translated ); | |
// $translated = str_ireplace( '« Older Entries', '< Older Posts', $translated ); | |
// $translated = str_ireplace( 'Next Entries »', 'Newer Posts >', $translated ); | |
return $translated; | |
} | |
add_filter( 'gettext', 'apway_translate_readmore_text' ); | |
add_filter( 'ngettext', 'apway_translate_readmore_text' ); | |
/* Filters the array of parsed query variables. */ | |
add_filter( 'request', 'apway_filter_referral_spam_requests', 0 ); | |
/** Serve 404 to referrers on the current Blacklist */ | |
function apway_filter_referral_spam_requests( $request ) { | |
global $wp_query; | |
/* Retrieve referer from ‘_wp_http_referer’ or HTTP referer. */ | |
$referrer = wp_get_referer() !== false ? wp_get_referer() : ( isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '' ); // Input var okay. | |
if ( empty( $referrer ) ) { | |
return $request; | |
} | |
/* Parses a URL and returns an associative array containing its components. | |
* The values of the array elements are not URL decoded | |
*/ | |
$referrer = parse_url( $referrer, PHP_URL_HOST ); | |
/* Get the blacklist */ | |
$referrers_blacklist = apway_referrals_blacklist(); | |
if ( empty( $referrers_blacklist ) ) { | |
return $request; | |
} | |
$is_blacklisted = false; | |
/* check the referrer against the blacklist */ | |
foreach ( $referrers_blacklist as $blist_ref ) { | |
if ( false !== stripos( $referrer, $blist_ref ) ) { | |
$is_blacklisted = true; | |
break; | |
} | |
} | |
/* if they are blacklisted, serve them a 404 */ | |
if ( $is_blacklisted ) { | |
/* Set HTTP status header. */ | |
status_header( 404 ); | |
$wp_query->set_404(); | |
get_template_part( 404 ); | |
exit(); | |
} | |
return $request; | |
} | |
/** | |
* Gets a list of blacklisted referals from the JSON file of the plugin | |
* Stop Referrer Spam by Krzysztof Wielogórski | |
* | |
* @see https://wordpress.org/plugins/stop-referrer-spam/ | |
*/ | |
function apway_referrals_blacklist() { | |
/* Retrieves the value of a transient. */ | |
$ret = get_transient( '_referalls_spam_blacklist' ); | |
/* If the transient does not exist/no value/expired, then return = false. */ | |
if ( false === $ret ) { | |
/* Performs an HTTP request using the GET method and returns its response. */ | |
$response = wp_remote_get( 'https://srs.wielo.co/blacklist.json' ); | |
if ( $response instanceof WP_Error ) { | |
return; | |
} | |
$ret = $response['body']; | |
if ( empty( $ret ) ) { | |
return; | |
} | |
/* Decodes a JSON string for use in PHP */ | |
$ret = json_decode( $ret, true ); | |
if ( null === $ret ) { | |
return; | |
} | |
set_transient( '_referalls_spam_blacklist', $ret, DAY_IN_SECONDS ); // Refresh daily. | |
} | |
return $ret; | |
} | |
function add_post_image_to_rss( $content ) { | |
global $post; | |
if ( has_post_thumbnail( $post->ID ) ) { | |
$content = '<div>' . get_the_post_thumbnail( $post->ID, 'large', array( 'style' => 'margin-bottom: 15px;' ) ) . '</div>' . $content; | |
} | |
return $content; | |
} | |
add_filter( 'the_excerpt_rss', 'add_post_images_to_rss' ); | |
add_filter( 'the_content_feed', 'add_post_images_to_rss' ); | |
function the_excerpt_max_charlength( $charlength ) { | |
$excerpt = get_the_excerpt(); | |
$charlength++; | |
if ( mb_strlen( $excerpt ) > $charlength ) { | |
$subex = mb_substr( $excerpt, 0, $charlength - 5 ); | |
$exwords = explode( ' ', $subex ); | |
$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) ); | |
if ( $excut < 0 ) { | |
echo mb_substr( $subex, 0, $excut ); | |
} else { | |
echo $subex; | |
} | |
echo '...'; | |
} else { | |
echo $excerpt; | |
} | |
} | |
// Put this code in functions.php or a separate plugin | |
add_action( 'rest_api_init', 'register_rest_images' ); | |
function register_rest_images() { | |
register_rest_field( array( 'post' ), | |
'fimg_url', | |
array( | |
'get_callback' => 'get_rest_featured_image', | |
'update_callback' => null, | |
'schema' => null, | |
) | |
); | |
} | |
function get_rest_featured_image( $object, $field_name, $request ) { | |
if ( $object['featured_media'] ) { | |
$img = wp_get_attachment_image_src( $object['featured_media'], 'thumbnail' ); // change 'thumbnail' to other image size if needed | |
if ( empty( $img ) ) { | |
return false; | |
} | |
return $img[0]; | |
} | |
return false; | |
} | |
add_action('wp_footer', function () { | |
?> | |
<script> | |
jQuery( document ).ready( function( $ ){ | |
setTimeout( function(){ | |
$('.flatpickr-input').each(function(){ flatpickr( $(this)[0] ).set('dateFormat', 'd/m/Y');}); | |
}, 1000 ); | |
}); | |
</script> | |
<?php | |
}); | |
function my_login_logo_one() { | |
?> | |
<style type="text/css"> | |
body.login div#login h1 a { | |
background-image: url("Logo File"); | |
padding-bottom: 30px; | |
} | |
</style> | |
<?php | |
} add_action( 'login_enqueue_scripts', 'my_login_logo_one' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment