Advertisement
javiersantos

20 modificaciones en functions.php - Ponencia Javier Santos

Sep 15th, 2012
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.12 KB | None | 0 0
  1. // 1. Añadir Google Analytics
  2.  
  3. <?php
  4. add_action('wp_footer, 'add_googleanalytics');
  5. function add_googleanalytics() { ?>
  6. // Pegar aquí el código de Analytics
  7. <?php } ?>
  8.  
  9. // 2. Eliminar versión de WordPress
  10.  
  11. function wpbeginner_remove_version() {
  12. return '';
  13. }
  14. add_filter('the_generator', 'wpbeginner_remove_version');
  15.  
  16. // 3. Custom dashboard logo
  17.  
  18. add_action('admin_head', 'my_custom_logo');
  19.  
  20. function my_custom_logo() {
  21. echo '
  22. <style type="text/css">
  23. #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
  24. </style>
  25. ';
  26. }
  27.  
  28. // 4. Cambiar Gravatar por defecto
  29.  
  30. add_filter( 'avatar_defaults', 'newgravatar' );
  31.  
  32. function newgravatar ($avatar_defaults) {
  33. $myavatar = get_bloginfo('template_directory') . '/images/gravatar.gif';
  34. $avatar_defaults[$myavatar] = "Gravatar personalizado";
  35. return $avatar_defaults;
  36. }
  37.  
  38. // 5. Añadir más campos de perfil
  39.  
  40. function perfil_social( $contactmethods ) {
  41. // Añadir Twitter
  42. $contactmethods['twitter'] = 'Twitter';
  43. // Añadir Facebook
  44. $contactmethods['facebook'] = 'Facebook';
  45.  
  46. return $contactmethods;
  47. }
  48. add_filter('user_contactmethods','perfil_social',10,1);
  49.  
  50. // 6. Personalizar longitud de extracto
  51.  
  52. function new_excerpt_length($length) {
  53. return 100;
  54. }
  55. add_filter('excerpt_length', 'new_excerpt_length');
  56.  
  57. // 7. Mover barra de administración
  58.  
  59. function admin_bar_abajo() { ?>
  60. <style type="text/css">
  61. body {
  62. margin-top: -28px;
  63. padding-bottom: 28px;
  64. }
  65. body.admin-bar #wphead {
  66. padding-top: 0;
  67. }
  68. body.admin-bar #footer {
  69. padding-bottom: 28px;
  70. }
  71. #wpadminbar {
  72. top: auto !important;
  73. bottom: 0;
  74. }
  75. #wpadminbar .quicklinks .menupop ul {
  76. bottom: 28px;
  77. }
  78. </style>
  79. <?php }
  80. // En panel de administración (Dashboard)
  81. add_action( 'admin_head', 'admin_bar_abajo' );
  82. // En el front-end
  83. add_action( 'wp_head', 'admin_bar_abajo' );
  84.  
  85. // 8. Eliminar barra de administración
  86.  
  87. wp_deregister_script('admin-bar');
  88. wp_deregister_style('admin-bar');
  89. remove_action('wp_footer','wp_admin_bar_render',1000);
  90. remove_action('init', 'wp_admin_bar_init');
  91.  
  92. // 9. Aviso de actualización solo para dministradores
  93.  
  94. global $user_login;
  95. get_currentuserinfo();
  96. if ($user_login !== "admin") {
  97. add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
  98. add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
  99. }
  100.  
  101. // 10. Redireccionar búsqueda
  102.  
  103. add_action('template_redirect', 'single_result');
  104. function single_result() {
  105. if (is_search()) {
  106. global $wp_query;
  107. if ($wp_query->post_count == 1) {
  108. wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
  109. }
  110. }
  111. }
  112.  
  113. // 11. Número total de palabras escritas
  114.  
  115. function post_word_count() {
  116. $count = 0;
  117. $posts = get_posts( array(
  118. 'numberposts' => -1,
  119. 'post_type' => array( 'post', 'page' )
  120. ));
  121. foreach( $posts as $post ) {
  122. $count += str_word_count( strip_tags( get_post_field( 'post_content', $post->ID )));
  123. }
  124. $num = number_format_i18n( $count );
  125. $text = _n( 'Palabra', 'Palabras', $num );
  126. echo "<tr><td class='first b'>{$num}</td><td class='t'>{$text}</td></tr>";
  127. }
  128. add_action( 'right_now_content_table_end', 'post_word_count');
  129.  
  130. // 12. Mensaje personalizado al registrarse
  131.  
  132. add_action('register_form', 'trw_mensaje_registro');
  133. function trw_mensaje_registro() {
  134. $html = '
  135. <div style="margin:10px 0;border:1px solid #e5e5e5;padding:10px">
  136. <p style="margin:5px 0;">
  137. Ya estoy registrado, ¿y ahora qué?
  138. </p>
  139. </div>';
  140. echo $html;
  141. }
  142.  
  143. // 13. Acortar dirección de búsqueda
  144.  
  145. function trw_search_url_rule() {
  146. if ( is_search() && !empty($_GET['s'])) {
  147. wp_redirect(home_url("/buscar/") . urlencode(get_query_var('s')));
  148. exit();
  149. }
  150. }
  151. add_action('template_redirect', 'trw_search_url_rule');
  152.  
  153. // 14. Mostrar visitas de un artículo
  154.  
  155. add_filter('manage_posts_columns', 'posts_column_views');
  156. add_action('manage_posts_custom_column', 'posts_custom_column_views',5,2);
  157. function posts_column_views($defaults){
  158. $defaults['post_views'] = __('Visitas');
  159. return $defaults;
  160. }
  161. function posts_custom_column_views($column_name, $id){
  162. if($column_name === 'post_views'){
  163. echo getPostViews(get_the_ID());
  164. }
  165. }
  166.  
  167. // FIN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement