Verified Commit 1e13b00b authored by Jakob Moser's avatar Jakob Moser
Browse files

Add shortcode and redirections for freshers' days links

[fscoli_latest_freshers_days] shows the title of the latest freshers days post and links to it

/erstitage/aktuell and /en/freshers-days/latest link to it
parent 88a0551f
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -251,6 +251,46 @@ function fscoli_disable_users_sitemap($provider, $provider_name) {
// To re-enable /wp-sitemap-users-1.xml and /en/wp-sitemap-users-1.xml, remove the line below
add_filter('wp_sitemaps_add_provider', 'fscoli_disable_users_sitemap', accepted_args: 2);

function fscoli_find_latest_post($search_query) {
    $posts = get_posts([
        "s" => $search_query,
        "search_columns" => ["post_title"],
        "post_type" => "post", 
        "order" => "desc", 
        "orderby" => "date"
    ]);

    return $posts[0];
}

function fscoli_redirect_freshers_days() {
    $uri = $_SERVER["REQUEST_URI"];
    
    if($uri == "/erstitage/aktuell" || $uri == "/erstitag/aktuell") {
        $post = fscoli_find_latest_post("Ersti");
        wp_redirect(get_page_link($post));
        exit;
    } else if($uri == "/en/freshers-days/latest") {
        $post = fscoli_find_latest_post("Fresher");
        wp_redirect(get_page_link($post));
        exit;
    }
}

add_action("template_redirect", "fscoli_redirect_freshers_days");

function fscoli_latest_freshers_days_shortcode() {
    if(str_starts_with(get_bloginfo('language'), 'de')) {
        $post = fscoli_find_latest_post("Ersti");
        return '<a href="/erstitage/aktuell">' . get_the_title($post) . "</a>";
    } else {
        $post = fscoli_find_latest_post("Fresher");
        return '<a href="/en/freshers-days/latest">' . get_the_title($post) . "</a>";
    }
}

add_shortcode("fscoli_latest_freshers_days", 'fscoli_latest_freshers_days_shortcode');

wp_enqueue_script('fscoli-utils', get_template_directory_uri() . '/js/utils.js');
wp_enqueue_script('fscoli-pills', get_template_directory_uri() . '/js/pills.js');
?>
+1 −1
Original line number Diff line number Diff line
@@ -7,7 +7,7 @@
 * Author: personads :: Maximilian Müller-Eberstein (ursprüngliche Version und Design); Jakob Moser
 * Author URI: https://personads.me/
 *
 * Version: 2.17.6
 * Version: 2.18.0
 * Requires PHP: 8.0
 *
 * License: GNU General Public License v3.0