Skip to content

Instantly share code, notes, and snippets.

@ramasilveyra
Last active June 16, 2024 07:12
Show Gist options
  • Save ramasilveyra/1045fe91c56edf75dbdb to your computer and use it in GitHub Desktop.
Save ramasilveyra/1045fe91c56edf75dbdb to your computer and use it in GitHub Desktop.
My base functions extras for any WordPress themes that I develop
<?php
/**
* Logitud de the_excerpt() a 25 palabras
*/
function ramasilveyra_custom_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'ramasilveyra_custom_excerpt_length', 999 );
/**
* Remueve […] de the_excerpt()
*/
function ramasilveyra_new_excerpt_more( $more ) {
return '...';
}
add_filter('excerpt_more', 'ramasilveyra_new_excerpt_more');
/**
* Remueve etiquetas innecesarias de wp_head()
*/
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );
/**
* Remueve widgets por defecto
*/
function ramasilveyra_unregister_default_wp_widgets() {
unregister_widget( 'WP_Widget_Pages' );
unregister_widget( 'WP_Widget_Calendar' );
unregister_widget( 'WP_Widget_Archives' );
unregister_widget( 'WP_Widget_Links' );
unregister_widget( 'WP_Widget_Meta' );
unregister_widget( 'WP_Widget_Search' );
unregister_widget( 'WP_Widget_Text' );
unregister_widget( 'WP_Widget_Categories' );
unregister_widget( 'WP_Widget_Recent_Posts' );
unregister_widget( 'WP_Widget_Recent_Comments' );
unregister_widget( 'WP_Widget_RSS' );
unregister_widget( 'WP_Widget_Tag_Cloud' );
}
add_action( 'widgets_init', 'ramasilveyra_unregister_default_wp_widgets', 1 );
/**
* Remueve widgets innecesarios de la pagina inicial del panel
*/
function ramasilveyra_remove_dashboard_widgets() {
global $wp_meta_boxes;
unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links'] );
unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now'] );
unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins'] );
unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_drafts'] );
unset( $wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments'] );
unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_primary'] );
unset( $wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary'] );
}
add_action( 'wp_dashboard_setup', 'ramasilveyra_remove_dashboard_widgets' );
remove_action( 'welcome_panel', 'wp_welcome_panel' );
/**
* Remover botones de la barra superior de administracion
*/
function ramasilveyra_remove_admin_bar_links() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu( 'wp-logo' ); // Remover submenu Acerca de WordPress
$wp_admin_bar->remove_menu( 'updates' ); // Remover botón de actualizaciones
$wp_admin_bar->remove_menu( 'comments' );
$wp_admin_bar->remove_menu( 'new-media' );
$wp_admin_bar->remove_menu( 'themes' );
$wp_admin_bar->remove_menu( 'customize' );
$wp_admin_bar->remove_menu( 'widgets' );
$wp_admin_bar->remove_menu( 'menus' );
$wp_admin_bar->remove_menu( 'new-post' );
$wp_admin_bar->remove_menu( 'new-page' );
}
add_action( 'wp_before_admin_bar_render', 'ramasilveyra_remove_admin_bar_links' );
function ramasilveyra_remove_tamanos_menu() {
$current_user = wp_get_current_user();
remove_menu_page( 'edit.php' );
if ($current_user->user_login != 'shanky' && $current_user->user_login != 'ramasilveyra') {
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'plugins.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'options-general.php' );
remove_submenu_page( 'edit.php?post_type=page', 'post-new.php?post_type=page' );
remove_submenu_page( 'index.php', 'update-core.php' );
remove_submenu_page( 'themes.php', 'themes.php' );
remove_submenu_page( 'themes.php', 'customize.php' );
remove_submenu_page( 'themes.php', 'nav-menus.php' );
remove_submenu_page( 'themes.php', 'theme-editor.php' );
}
}
add_action( 'admin_menu', 'ramasilveyra_remove_tamanos_menu', 999 );
/**
* Ocultar noticias de actualizacion
*/
function ramasilveyra_hide_update_notice() {
remove_action( 'admin_notices', 'update_nag', 3 );
}
add_action( 'admin_head', 'ramasilveyra_hide_update_notice', 1 );
/**
* Remover pestaña de ayuda
*/
function ramasilveyra_remove_helph_tab( $old_help, $screen_id, $screen ) {
$screen->remove_help_tabs();
return $old_help;
}
add_filter( 'contextual_help', 'ramasilveyra_remove_helph_tab', 999, 3 );
/**
* Cambiar base de las urls
*/
function ramasilveyra_change_slug() {
global $wp_rewrite;
$wp_rewrite->author_base = 'autor';
$wp_rewrite->category_base = 'categoria';
$wp_rewrite->pagination_base = 'pagina';
$wp_rewrite->search_base = 'busqueda';
}
add_action( 'init', 'ramasilveyra_change_slug' );
/**
* Cambiar logo de inicio de sesión
*/
function ramasilveyra_login_logo() { ?>
<style>body.login div#login h1 a {background-image: url('<?php echo get_site_url(); ?>/apple-touch-icon.png');}</style>
<?php }
add_action( 'login_enqueue_scripts', 'ramasilveyra_login_logo' );
/**
* Cabeceras de correo personalizadas.
*/
function ramasilveyra_website_email() {
$sitename = strtolower( $_SERVER['SERVER_NAME'] );
if ( substr($sitename, 0, 4) == 'www.' ) {
$sitename = substr( $sitename, 4 );
}
$sender_email = 'notificacion@' . $sitename;
return $sender_email;
}
add_filter( 'wp_mail_from', 'ramasilveyra_website_email' );
function ramasilveyra_website_name() {
$site_name = get_bloginfo( 'name' );
return $site_name;
}
add_filter( 'wp_mail_from_name', 'ramasilveyra_website_name' );
/**
* Redirije los resultados de busqueda de /?s=query a /search/query/, y convierte los %20 a +.
*/
function ramasilveyra_nice_search() {
global $wp_rewrite;
if ( !isset($wp_rewrite) || !is_object($wp_rewrite) || !$wp_rewrite->using_permalinks() ) {
return;
}
$search_base = $wp_rewrite->search_base;
if ( is_search() && !is_admin() && strpos($_SERVER['REQUEST_URI'], "/{$search_base}/") === false ) {
wp_redirect( esc_url_raw(home_url("/{$search_base}/" . get_query_var('s'))) );
exit();
}
}
add_action( 'template_redirect', 'ramasilveyra_nice_search' );
/**
* Permitir iniciar sesión con el correo electrónico
*/
function ramasilveyra_authenticate_example( $username ) {
$user = get_user_by( 'email', $username );
if ( ! empty( $user->user_login ) ) {
$username = $user->user_login;
}
return $username;
}
add_action( 'wp_authenticate', 'ramasilveyra_authenticate_example' );
/**
* Contactarme
*/
function ramasilveyra_before_admin_bar_render_example() {
global $wp_admin_bar;
$wp_admin_bar->add_node( array(
'id' => 'contact-designer',
'title' => 'Contactar Ramiro Silveyra d\'Avila',
'href' => 'https://ramasilveyra.com.ar/#contacto',
'meta' => array( 'target' => '_blank' )
) );
}
add_action( 'wp_before_admin_bar_render', 'ramasilveyra_before_admin_bar_render_example' );
/**
* Eliminar atributos img y height de las imagenes
*/
add_filter( 'post_thumbnail_html', 'ramasilveyra_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'ramasilveyra_width_attribute', 10 );
function ramasilveyra_width_attribute( $html ) {
$html = preg_replace( '/(width|height)="\d*"\s/', '', $html );
return $html;
}
/**
* Deshablilitar los comentarios
*/
// Disable support for comments and trackbacks in post types
function ramasilveyra_disable_comments_post_types_support() {
$post_types = get_post_types();
foreach ( $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' );
}
}
}
add_action( 'admin_init', 'ramasilveyra_disable_comments_post_types_support' );
// Cerrar comentarios
function ramasilveyra_disable_comments_status() {
return false;
}
add_filter( 'comments_open', 'ramasilveyra_disable_comments_status', 20, 2 );
add_filter( 'pings_open', 'ramasilveyra_disable_comments_status', 20, 2 );
// Ocultar comentarios existentes
function ramasilveyra_disable_comments_hide_existing_comments( $comments ) {
$comments = array();
return $comments;
}
add_filter('comments_array', 'ramasilveyra_disable_comments_hide_existing_comments', 10, 2);
// Eliminar menu de comentarios del panel
function ramasilveyra_disable_comments_admin_menu() {
remove_menu_page( 'edit-comments.php' );
}
add_action( 'admin_menu', 'ramasilveyra_disable_comments_admin_menu' );
// Redireccionar cualquier usuario intentando accede a la pagina de comentarios
function ramasilveyra_disable_comments_admin_menu_redirect() {
global $pagenow;
if ( $pagenow === 'edit-comments.php' ) {
wp_redirect( admin_url() );
exit;
}
}
add_action( 'admin_init', 'ramasilveyra_disable_comments_admin_menu_redirect' );
// Eliminar widget de comentarios del panel de administración
function ramasilveyra_disable_comments_dashboard() {
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
}
add_action( 'admin_init', 'ramasilveyra_disable_comments_dashboard' );
// Eliminar enlaces a comentarios del panel de administración
function ramasilveyra_disable_comments_admin_bar() {
if ( is_admin_bar_showing() ) {
remove_action( 'admin_bar_menu', 'wp_admin_bar_comments_menu', 60 );
}
}
add_action( 'init', 'ramasilveyra_disable_comments_admin_bar' );
/**
* Soporte para Woocommerce y añade moneda argentina
*/
add_action( 'after_setup_theme', 'ramasilveyra_woocommerce_support' );
function ramasilveyra_woocommerce_support() {
add_theme_support( 'woocommerce' );
}
function ramasilveyra_add_ars_currency( $currencies ) {
$currencies['ARS'] = __( 'Pesos Argentinos', 'woothemes' );
return $currencies;
}
add_filter( 'woocommerce_currencies', 'ramasilveyra_add_ars_currency' );
function ramasilveyra_add_ars_symbol( $currency_symbol, $currency ) {
switch( $currency ) {
case 'ARS':
$currency_symbol = '$';
break;
}
return $currency_symbol;
}
add_filter( 'woocommerce_currency_symbol', 'ramasilveyra_add_ars_symbol', 1, 2);
/**
* Fomentar una emantica valida
*/
function ramasilveyra_tinymce_init( $init ) {
$init['theme_advanced_blockformats'] = 'p,h2,h3,h4,h5,h6';
return $init;
}
add_filter('tiny_mce_before_init', 'ramasilveyra_tinymce_init' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment