Cuando se trabaja desarrollando o modificando plantillas o themes de WordPress, es muy útil contar con una colección de fragmentos de código, para implementar funcionalidades a nuestro tema.
Estos snippets para WordPress siempre son un ahorro en tiempo y aceleran el proceso de desarrollo, gracias a que WordPress posee una gran comunidad puedes encontrar toda clase de snippets navegando por la red.
En este post hemos recopilado algunos de los snippets básicos, que pueden llegar a tener gran utilidad y que sin duda te pueden servir para ahorrar tiempo y esfuerzo (y algún que otro plugin).
Copia y pega el código en el archivo functions.php y recuerda siempre tener una copia de seguridad de tu archivo original por si las moscas.
Índice
ToggleHabilitar Lightbox en las imágenes de un post
add_filter('the_content', 'add_lightbox');
function add_lightbox($content) {
global $post;
$pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\") (.*?)>/i";
$replacement = '<a$1href=$2$3.$4$5 rel="lightbox" title="'.$post->post_title.'"$6>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
Restringir acceso a wp-admin a suscriptores
function restrict_access_admin_panel(){
global $current_user;
get_currentuserinfo();
if ($current_user->user_level < 4) {
wp_redirect( get_bloginfo('url') );
exit;
}
}
add_action('admin_init', 'restrict_access_admin_panel', 1);
Marcar automáticamente somo Spam comentarios con una url muy larga
function check_spam( $approved , $commentdata ) {
return ( strlen( $commentdata['comment_author_url'] ) > 50 ) ? 'spam'
: $approved;
}
add_filter( 'pre_comment_approved', 'check_spam', 99, 2 );
Mostrar entradas relacionadas en los posts de cada autor
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $authordata->ID,
'post__not_in' => array( $post->ID ), 'posts_per_page' => 10 ) );
$output = '<ul>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li><a href="' . get_permalink( $authors_post->ID
) . '">' . apply_filters( 'the_title', $authors_post->post_title,
$authors_post->ID ) . '< /a>< /li>';
}
$output .= '< /ul>';
return $output;
}
Insertar Biografía del autor en cada post
function display_author_bio ($content=''){
global $post;
$post_author_name=get_the_author_meta(«display_name»);
$post_author_description=get_the_author_meta(«description»);
$html=»<div class=’clearfix’ id=’about_author’>\n»;
$html.=»<img width=’80’ height=’80’ class=’avatar’
src=’https://www.gravatar.com/avatar.php?gravatar_id=».md5(get_the_author_email())
. «&default=».urlencode($GLOBALS[‘defaultgravatar’]).»&size=80&r=PG’ alt=’PG’/>\n»;
$html.=»<div class=’author_text’>\n»;
$html.=»<h4>Author: «.$post_author_name.»</h4>\n»;
$html.= $post_author_description.»\n»;
$html.=»</div>\n»;
$html.=»<div class=’clear’></div>\n»;
$content .= $html;
}
return $content;
}
add_filter('the_content', 'display_author_bio');
Limpiar wp_head sin plugin
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
Elimina el campo URL del formulario de comentarios
function remove_comment_fields($fields) {
unset($fields['url']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
Excluir las páginas de los resultados de búsqueda
function filter_search($query) {
if ($query->is_search) {
$query->set('post_type', 'page');
}
return $query;
}
add_filter('pre_get_posts', 'filter_search');
Añadir la imagen destacada al feed de WordPress
function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = get_the_post_thumbnail($post->ID) . $content;
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');
Espero que te haya gustado, en esta entrada tienes más snippets relativos a la seguridad para .htaccess
Dejar un comentario
¿Quieres unirte a la conversación?Siéntete libre de contribuir!