Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dantetesta/d2ad514a87f382cc6f1d57f073c87338 to your computer and use it in GitHub Desktop.

Select an option

Save dantetesta/d2ad514a87f382cc6f1d57f073c87338 to your computer and use it in GitHub Desktop.
ALTERA A URL DO AUTHOR - wordpress
<?php
// Adiciona uma nova regra de reescrita
function wpscripts_add_rewrite_rules() {
add_rewrite_rule(
'^([a-zA-Z0-9_-]+)/?$',
'index.php?author_name=$matches[1]',
'top'
);
}
add_action('init', 'wpscripts_add_rewrite_rules');
// Adiciona a variável de consulta author_name
function wpscripts_add_query_vars($query_vars) {
$query_vars[] = 'author_name';
return $query_vars;
}
add_filter('query_vars', 'wpscripts_add_query_vars');
// Manipula a requisição para buscar o autor pelo nome
function wpscripts_parse_request($query) {
if (isset($query->query_vars['author_name'])) {
$author_name = $query->query_vars['author_name'];
$author = get_user_by('slug', $author_name);
if ($author) {
$query->set('author', $author->ID);
$query->is_author = true;
}
}
}
add_action('pre_get_posts', 'wpscripts_parse_request');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment