Créer une url personnalisée sur WordPress

Exemple pour créer une url personnalisée sur WordPress.
(Dans l’exemple ci-dessous, nous changeons l’url de base du post type « programme » en fonction du status du programme)

<?php

/**
 *  1. QUERY VAR
 *  - Create query var "pip_template"
 */
add_filter( 'query_vars', 'pip_custom_query_var' );
function pip_custom_query_var( $vars ) {
    $vars[] = 'pip_template';
    return $vars;
}

/**
 *  2. TEMPLATE INCLUDE
 *  - Get correct template for our custom url
 */
add_filter( 'template_include', 'pip_custom_url_template', 20 );
function pip_custom_url_template( $template ) {

    $post_slug = get_query_var( 'pip_template' );
    if ( !$post_slug ) {
        return;
    }

    $template_path         = get_template_directory() . '/single-programme.php'; // TODO: <-- Change this
    $correct_template_path = locate_template( $template_path );
    if ( !$correct_template_path ) {
        return $template;
    }

    return $correct_template_path;
}


/**
 *  3. TEMPLATE REDIRECT
 *  - Redirect user somewhere else when the condition is invalid
 */
add_action( 'template_redirect', 'pip_custom_url_redirect', 20 );
function pip_custom_url_redirect() {

    $post_type    = 'programme';  // TODO: <-- Change this
    $post_slug    = get_query_var( 'pip_template' );
    $is_programme = is_singular( $post_type );

    if ( $is_programme && !$post_slug ) {
        wp_safe_redirect( get_home_url(), '302', 'PilotIn' );
        die;
    }

}

/**
 *  4. REWRITE URL
 *  - Create a custom url
 */
add_action( 'init', 'pip_create_custom_urls' );
function pip_create_custom_urls() {

    $post_slug = get_query_var( 'pip_template' );
    if ( !$post_slug ) {
        return;
    }

    $post_type   = 'programme'; // TODO: <-- Change this
    $post_object = get_page_by_path( $post_type . '/' . $post_slug, 'OBJECT', $post_type );
    if ( !$post_object ) {
        return;
    }

    $current_post_status = wp_list_pluck( $post_object, 'post_status' );
    $allowed_post_status = array( 'publish', 'reference' );
    if ( !$current_post_status || !in_array( $current_post_status, $allowed_post_status ) ) {
        return;
    }

    // TODO: Change below
    if ( $current_post_status === 'publish' ) {
        add_rewrite_rule( '^programmes-immobiliers-neufs/([a-z0-9-]+)[/]?$', "index.php?pip_template=$post_slug", 'top' );
    } elseif ( $current_post_status === 'reference' ) {
        add_rewrite_rule( '^references-precedents-programmes-de-katrimmo/([a-z0-9-]+)[/]?$', "index.php?pip_template=$post_slug", 'top' );
    }

}

/**
 *  5. POST TYPE LINK
 *  - Change permalink based on post status
 */
add_filter( 'post_type_link', 'pip_change_post_link', 20, 4 );
function pip_change_post_link( $post_link, $post, $leavename, $sample ) {

    $post_type_wanted = 'programme'; // TODO: <-- Change this
    $post_type = wp_list_pluck( $post, 'post_type' );
    if ( $post_type !== $post_type_wanted ) {
        return $post_link;
    }

    $current_post_status = wp_list_pluck( $post, 'post_status' );

    // TODO: Change conditions below
    $allowed_post_status = array( 'publish', 'reference' );
    if ( !$current_post_status || !in_array( $current_post_status, $allowed_post_status ) ) {
        return $post_link;
    }

    $post_slug = wp_list_pluck( $post, 'post_name' );

    if ( $current_post_status === 'publish' ) {
        return get_home_url( "programmes-immobiliers-neufs/$post_slug" );
    } elseif ( $current_post_status === 'reference' ) {
        return get_home_url( "references-precedents-programmes-de-katrimmo/$post_slug" );
    }

    return $post_link;
}

 

Modèle AARRR : comment l’appliquer à votre stratégie ?

Atelier UX : quels avantages pour votre projet ?

Combien de temps pour créer un site WordPress ?