Skip to content

Instantly share code, notes, and snippets.

@Tmeister
Created May 25, 2022 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tmeister/329e8d78290dfe1f8698c3b0607a68e0 to your computer and use it in GitHub Desktop.
Save Tmeister/329e8d78290dfe1f8698c3b0607a68e0 to your computer and use it in GitHub Desktop.
A really simple plugin to add a second parameter to the author URL
<?php
/**
* Plugin Name: Rewrite Author URL
* Plugin URI: https://example.com/plugins/the-basics/
* Description: A basic rewrite rules example.
* Version: 1.0.0
* Requires at least: 5.2
* Requires PHP: 7.4
* Author: Enrique Chavez
* Author URI: https://enriquechavez.co/
*/
add_filter('author_rewrite_rules', function ($author_rules) {
// Add a new regex to match the following URL structure author/name/metafield
// And assign the result to the metafield variable
$author_rules['author/([^/]+)/([^/]+)?$'] = 'index.php?author_name=$matches[1]&metafield=$matches[2]';
return $author_rules;
});
add_filter('query_vars', function ($vars) {
// Add the new var to be able to handle it using the WordPress way!
$vars[] = 'metafield';
return $vars;
});
add_shortcode('show_user_metafield', function ($atts) {
// Simple shortcode to show the new field value
// This should only be parsed on the author page
if ( ! is_author()) {
return '';
}
// Get the variable value
$metafield = get_query_var('metafield');
// If the metafield is empty do nothing.
if (empty($metafield)) {
return '';
}
// Do whatever you want with the value.
return '<div>Mostrando el meta field: ' . $metafield . '</div>';
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment