Confira aqui uma lista de scripts interessantes para ser usado no wordpress.
1. Melhorar a qualidade da foto ao salvar
add_filter( 'jpeg_quality', create_function( '', 'return 100;' ) );
_______________________
2. Melhorar a colocação nas buscas do google
Esse script abaixo define muitas coisas direto no cabeçalho do site para melhoras as buscas.
O que faz:
<?
$metakeywords = '';
$keywords = get_the_category( $post->ID ); foreach ( $keywords as $keyword ) {$metakeywords .= $keyword->cat_name . ", ";}
$posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $metakeywords .= $tag->name . ', '; } }
//imagem
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'medium' );
$image = $image[0];
?>
<meta name="title" content="<?php if ( is_single() ) { single_post_title('', true); } else { bloginfo('name'); } ?>" />
<?php if (is_single() || is_page() ) : if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<meta name="description" content="<?php echo get_the_excerpt();?>">
<?php endwhile; endif; elseif (is_home() ): ?>
<meta name="description" content="<?php bloginfo('description'); ?>">
<?php endif; ?>
<meta name="Keywords" content="<? echo $metakeywords; ?>">
<meta property="og:image" content="<? echo $image; ?>" />
<link rel="image_src" href="<? echo $image; ?>" />
_______________________
3. Adicionar um script no header e salve em functions.php
function add_meta_tags() {
echo "<script language='JavaScript'>document.onselectstart = function() {return false}</script>";
}
Pode ser colocado qualquer script, nesse acima por exemplo.
Nesse caso tirei as permissões de selecionar, pra copiar e colar meu conteúdo.
* chame esse script no header.
add_action('wp_head','add_meta_tags');
Um script mais completo seria assim
function add_meta_tags() {
echo "<script language='JavaScript'>document.onselectstart = function() {return false}</script>";
//para adicionar imagem destacada post
if(!has_post_thumbnail( $post->ID ) OR !is_singular()) {
$image = "https://www.agenciaempregoscuritiba.com.br/wp-content/uploads/2015/11/aec_capa.png";}
else{$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
$image = $image[0];}
echo "
<meta property=\"og:image\" content=\"$image\" />
<link rel=\"image_src\" href=\"$image\" />
";
}
add_action('wp_head', 'add_meta_tags');
Acima o que faz é:
_______________________
4. Colocar um contador de visualizações das postagens, direto em functions.php
//colocar um contador de visualizações das postagens.
function getPostViews($postID){
$count_key = '_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = '_post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
add_filter( 'the_content', 'contador' );
function contador( $content ) {
if ( is_singular('post')) {
setPostViews(get_the_ID());
//se quiser imprimir no post
//$content = $content . '<div style="font-size:10px;color:#ccc">'.getPostViews(get_the_ID()).'</div>';
}
return $content;
}
//para mostrar no painel de post
add_filter('manage_posts_columns', 'wpmidia_posts_column_views');
function wpmidia_posts_column_views($defaults){
$defaults['post_views'] = __('Visualizações');
return $defaults;
}
add_action('manage_posts_custom_column', 'wpmidia_custom_column_views',5,2);
function wpmidia_custom_column_views($column_name, $id){
if($column_name === 'post_views'){
$count = get_post_meta($id, '_post_views_count', true);
if( !empty($count) ){
echo $count. ' visualização(ões)';
}
}
}
Continuando…
Se quiser filtrar pelos mais vistos, coloque direto no código, em algum lugar, por exemplo em header.php
<ul>
<?php $popularpost = new WP_Query( array( 'posts_per_page' => 5, 'meta_key' => '_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) );
while ( $popularpost->have_posts() ) : $popularpost->the_post();
?>
<li>
<small><?php the_category(', '); ?></small>
<a href="<?php echo get_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<h3><?php the_title(); ?><h3>
</li>
<?php
endwhile;
wp_reset_query();
?>
</ul>
_______________________
5. Função para pesquisar apenas em posts, eliminando as pages
//funcao para pesquisar apenas em posts
function SearchFilter($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
_______________________
//adicionar script no rodape das páginas
function add_script_wp_footer() {
//codigo aqui
}
add_action('wp_footer', 'add_script_wp_footer');
_______________________
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode(get_permalink($post->ID)); ?>&layout=standard&show_faces=false&width=450&action=like&colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:450px; height:30px"></iframe>
<?php
$args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories=get_categories($args);
foreach($categories as $category) {
$arg2 = $category->name;
$args3 = 'category_name='.$arg2.'&showposts=1';
query_posts( $args3 );
if ( have_posts() ) :
while ( have_posts() ) : the_post();
?><div style ="margin:10px;padding:10px;border: 1px solid #a1a1a1;;width: 30%;height:300px;float: left;"><b><a href="<?php echo get_site_url() ?>/categoria/<?=$arg2;?>"><?=$arg2;?></a></b><br><a href="<?php the_permalink() ?>">
<?php the_post_thumbnail('thumbnail'); ?><br><?php the_title() ?></a></div><?php
endwhile;
endif;
wp_reset_query();
}
?>
<h1 class="entry-title">Postagens do site</h1>
<?php
//for each category, show all posts
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
$args = array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts'=> 1
);
$posts=get_posts($args);
if ($posts) {
echo '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . ' class="entry-title" >' . $category->name.'</a> </p> ';
echo '<ul>';
foreach($posts as $post) {
setup_postdata($post);
echo '<li><a href="' . get_permalink() .'" rel="bookmark" title="Permanent Link to '. the_title_attribute('echo=0') . '">' . get_the_title() . ' </a></li>';
} // foreach($posts
echo '</ul>';
} // if ($posts
} // foreach($categories
?>
10. Dar permissão para colaborador enviar fotos. Colocar direto em functions.php
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
_______________________
11. Permissões no painel administrativo
Se você criou a página, e quer restringir acesso para outra pessoa mexer .
Com esse script, você consegue esconder os menus.
Para testar cada uma coloque essa função e teste cada uma, colocando dois // na frente pra desabilitar.
//remover pagina widgets inicial
function remove_dashboard_meta() {
remove_meta_box( 'dashboard_incoming_links', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_plugins', 'dashboard', 'normal' );
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_drafts', 'dashboard', 'side' );
remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
remove_meta_box( 'dashboard_activity', 'dashboard', 'normal');//since 3.8
}
add_action( 'admin_init', 'remove_dashboard_meta' );
Agora, se você quer esconder tudo por exemplo para um colaborador.
//remover barra esquerda colaborador
add_action( 'admin_menu', 'remove_admin_menus' );
function remove_admin_menus(){
global $menu;
$menu = array();
}
Ou quando esse colaborador logar, redirecionar o mesmo.
//redirecionar pagina inicial do colaborador para qualquer pagina
add_action('load-index.php', 'dashboard_Redirect');
function dashboard_Redirect(){
wp_redirect(admin_url('edit.php'));
}
E pra terminar.
_______________________
12. Como colar imagens no feed do wordpress.
imagens em feed wordpress
// display featured post thumbnails in WordPress feeds
function inflar_custom_feed_content($content) {
global $wp_query;
$post_id = $wp_query->post->ID;
if(is_feed()) {
// pega o campo personalizado de nome image
$image_url = get_post_meta($post_id, 'image', true);
// caso vazio pega a imagem destacada
if($image_url == ''){
$image_url = get_the_post_thumbnail_src(get_the_post_thumbnail($post_id,'thumbnail'));
}
$content = '<a href="'.get_permalink($post_id).'" target="_blank"><img src="'.$image_url.'" style="float:left;margin-right:6px;"/></a>'.$content;
}
return $content;
}
add_filter('the_excerpt_rss', 'inflar_custom_feed_content'); //altera a versão resumida do post
add_filter('the_content', 'inflar_custom_feed_content'); // altera a versão completa do post
// extrair apenas a url do thumbnail
function get_the_post_thumbnail_src($img){
return (preg_match('~\bsrc="([^"]++)"~', $img, $matches)) ? $matches[1] : '';
}
Por hoje é isso.
Acompanhe o site para ver mais…
<< Anterior Como resetar o wordpress para as configurações originais, sem precisar reinstalar
Deixe um comentário