Skip to content

Instantly share code, notes, and snippets.

@audrasjb
Last active September 28, 2022 21:07
Show Gist options
  • Save audrasjb/d7855887d453bff97ff8e0e30a1669e0 to your computer and use it in GitHub Desktop.
Save audrasjb/d7855887d453bff97ff8e0e30a1669e0 to your computer and use it in GitHub Desktop.
Plugin created for Philippe from the WordPress France Entraide Facebook group.
<?php
/**
* Plugin Name: Redirect Authors
* Description: Redirect authors to a specific front-end page with their posts.
* Author: audrasjb
* Author URI: https://jeanbaptisteaudras.com
* License: GPLv2 or later
* Version: 1.0.0
*/
function jba_declare_ra_shortcode() {
add_shortcode( 'author-posts', 'jba_ra_shortcode' );
}
add_action( 'init', 'jba_declare_ra_shortcode' );
function jba_ra_shortcode( $atts ) {
$user_id = get_current_user_id();
$content = '';
if ( 0 !== $user_id ) {
$args = array(
'post_type' => 'post',
'author' => $user_id,
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$content .= '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
$content .= '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
$content .= '</ul>';
}
wp_reset_postdata();
}
return $content;
}
function jba_ra_login_redirect( $redirect_to, $request, $user ) {
if ( isset( $user->roles ) && is_array( $user->roles ) ) {
if ( in_array( 'author', $user->roles ) ) {
// HERE, REPLACE THE URL WITH THE URL OF THE PAGE/POST YOU WANT TO REDIRECT AUTHORS TO :)
$redirect_to = 'https://example.com/authors-list/';
}
}
return $redirect_to;
}
add_filter( 'login_redirect', 'jba_ra_login_redirect', 10, 3 );
@audrasjb
Copy link
Author

Mode d'emploi en français :

  1. Créer une page dans laquelle on insère le shortcode [author-posts] puis bien noter son URL
  2. Récupérer le code ci-dessus et le placer dans un fichier PHP nommé redirect-authors.php
  3. Aller à la ligne 46 du code ci-dessus et remplacer l'URL par l'URL de la page créée au point 1.
  4. Activer l'extension
  5. Tester : attention la redirection ne marche que pour le rôle "auteur/autrice". Les autres rôles restent redirigés en back-office comme d'habitude après connexion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment