Skip to content

Instantly share code, notes, and snippets.

@paolo-mnet
Last active November 9, 2024 00:46
Show Gist options
  • Save paolo-mnet/61d4805a59a92c71f0fa210c024a8ff7 to your computer and use it in GitHub Desktop.
Save paolo-mnet/61d4805a59a92c71f0fa210c024a8ff7 to your computer and use it in GitHub Desktop.
A collection of WordPress snippets
<?php
// functions.php
/**
* Remove language dropdown from WordPress Admin login screen.
*/
add_filter('login_display_language_dropdown', '__return_false');
/**
* Disable XML-RPC by default.
*/
add_filter('xmlrpc_enabled', '__return_false');
remove_action('xmlrpc_rsd_apis', 'rest_output_rsd');
remove_action('template_redirect', 'rest_output_link_header', 11);
/**
* Disable 'users' route from REST API for security reason.
*
* @method rest_authentication_errors
*
* @param mixed $access [authentication response].
* @return mixed [authentication error or default].
*/
function mnet_rest_authentication_handler($access) {
// define user permission.
$has_permission = false;
if ( $user_id = get_current_user_id() ) {
$user = get_user_by('id', $user_id);
$allowed_roles = array('administrator', 'editor', 'author');
$has_permission = ( is_array($user->roles) && !empty(array_intersect($allowed_roles, $user->roles)) );
}
// get the current rest route.
$rest_route = $GLOBALS['wp']->query_vars['rest_route'];
if ( !empty($rest_route) || $rest_route != '/' ) {
$rest_route = untrailingslashit($rest_route);
}
// check for invalid access.
if ( strpos($rest_route, 'users') !== false && !$has_permission ) {
return new WP_Error(
'rest_cannot_access',
esc_html('You cannot access this REST API route.'),
['status' => rest_authorization_required_code()]
);
}
return $access;
}
add_filter('rest_authentication_errors', 'mnet_rest_authentication_handler', 99, 1);
/**
* Configure manually the SMTP parameters needed for wp_mail.
*
* @link https://developer.wordpress.org/reference/hooks/phpmailer_init/#comment-4298
* @param PHPMailer $phpmailer [default PHP Mailer instance].
*/
function mnet_phpmailer_smtp_config($phpmailer) {
if ( !is_object($phpmailer) ) {
$phpmailer = (object) $phpmailer;
}
$phpmailer->Mailer = 'smtp';
$phpmailer->SMTPAuth = true;
$phpmailer->Host = SMTP_HOST;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$from_name = get_bloginfo('name');
$phpmailer->From = SMTP_USER;
$phpmailer->FromName = "Noreply {$from_name}";
}
add_action('phpmailer_init', 'mnet_phpmailer_smtp_config', 11);
/**
* Trying to get a retina logo URL by attachment ID, if available.
*
* @param int $attachment_id
* @return string $retina_logo_url
*/
function mnet_retina_logo($attachment_id) {
$retina_logo_url = '';
$attachment_name = wp_basename( wp_get_attachment_url($attachment_id) );
$retina_attachment_title = str_replace('.png', '@2x', $attachment_name);
if ( $retina_attachment = get_page_by_title($retina_attachment_title, OBJECT, 'attachment') ) {
$retina_logo_url .= wp_get_attachment_url($retina_attachment->ID);
}
return $retina_logo_url;
}
/**
* Make sure to disable browser telephone format detection on iOS/Safari.
*
* @see https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html#//apple_ref/doc/uid/TP40008193-SW1
* @link https://stackoverflow.com/questions/226131/how-to-disable-phone-number-linking-in-mobile-safari
*
* @return void
*/
function mnet_telephone_format_detection_meta() {
echo "<meta name=\"format-detection\" content=\"telephone=no\" />\n";
}
add_action('wp_head', 'mnet_telephone_format_detection_meta', 11);
/**
* Make sure to save and load ACF fields across different environments.
*
* @return void
*/
function mnet_acf_save_fields($path) {
$path = get_stylesheet_directory() . '/acf_fields';
return $path;
}
add_filter('acf/settings/save_json', 'mnet_acf_save_fields', 11, 1);
function mnet_acf_load_fields($paths) {
if (isset($paths[0])) {
unset($paths[0]);
}
$paths[] = get_stylesheet_directory() . '/acf_fields';
return $paths;
}
add_filter('acf/settings/load_json', 'mnet_acf_load_fields', 11, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment