Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter jadermenezes7

    (@jadermenezes7)

    Hi,

    Thank you very much for your response and for trying to help me, it means a lot to me. I made a small modification directly in the AMP plugin, making it remove the AMP page tag for news and sports categories. This is because AMP does not work properly with articles from these categories. However, this approach is not ideal, as updating the plugin will inevitably overwrite this rule.

    Here is my redirection code:

    function plugin_redir_activate () {
    custom_rewrite_rules();
    flush_rewrite_rules();
    }
    
    function plugin_redir_deactivate () {
    flush_rewrite_rules();
    }
    
    register_activation_hook(FILE, 'plugin_redir_activate');
    register_deactivation_hook(FILE, 'plugin_redir_deactivate');
    
    add_filter('post_link', 'custom_permalink', 10, 3);
    function custom_permalink($permalink, $post, $leavename) {
    $categories = get_the_category($post->ID);
    $custom_category = array_filter($categories, function ($category) {
    return in_array($category->slug, ['news', 'sport']);
    });
    
    if (!empty($custom_category) && isset($custom_category[0])) {
        $category_slug = $custom_category[0]->slug;
        $post_date = get_the_date('Y/m/d', $post->ID);
        $permalink = home_url("/$category_slug/{$post_date}/{$post->post_name}/");
    }
    return $permalink;
    
    }
    
    add_action('init', 'custom_rewrite_rules');
    function custom_rewrite_rules() {
    $categories = ['news', 'sport'];
    foreach ($categories as $category) {
    add_rewrite_rule($category . '/(\d{4})/(\d{2})/(\d{2})/([^/]*)/?$', 'index.php?name=$matches[4]', 'top');
    }
    }
    
    add_action('parse_request', 'custom_path_validation');
    function custom_path_validation($wp) {
    if (get_query_var('custom_path_redirected')) {
    return;
    }
    $request_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
    $categories = ['news', 'sport'];
    
    foreach ($categories as $category) {
        $category_regex = '^' . $category . '/(\d{4})/(\d{2})/(\d{2})/([^/]+)/?$';
        if (preg_match("~$category_regex~", $request_path, $matches)) {
            $post_name = sanitize_title($matches[4]);
            $new_url = home_url("/$category/{$matches[1]}/{$matches[2]}/{$matches[3]}/$post_name/");
            if ($request_path !== ltrim(parse_url($new_url, PHP_URL_PATH), '/')) {
                set_query_var('custom_path_redirected', true);
                $wp->query_vars = array('name' => $matches[4]);
            }
        }
    }
    
    }
    
    add_action('template_redirect', 'redirect_old_urls');
    function redirect_old_urls() {
    if (is_single() && get_query_var('name')) {
    $post_id = url_to_postid(get_permalink());
    $categories = get_the_category($post_id);
    
        $category_slugs = wp_list_pluck($categories, 'slug');
        $matched_categories = array_intersect($category_slugs, ['news', 'sport']);
    
        if (!empty($matched_categories)) {
            $category_slug = reset($matched_categories);
            $post_date = get_the_date('Y/m/d', $post_id);
            $new_url = home_url("/$category_slug/{$post_date}/" . get_query_var('name') . '/');
    
            if (home_url(add_query_arg(array())) !== $new_url) {
                wp_redirect($new_url, 301);
                exit;
            }
        }
    }
    
    }
    
    add_action('init', 'custom_redirect_rules');
    function custom_redirect_rules() {
    $categories = ['news', 'sport'];
    foreach ($categories as $category) {
    add_rewrite_rule($category . '/(\d{4})/(\d{2})/(\d{2})/([^/]*)/?$', 'index.php?name=$matches[4]', 'top');
    }
    }
    
    add_action('parse_request', 'custom_redirect_check');
    function custom_redirect_check($wp) {
    $request_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
    $categories = ['news', 'sport'];
    
    foreach ($categories as $category) {
        $category_regex = '^' . $category . '/(\d{4})/(\d{2})/(\d{2})/?$';
    
        if (preg_match("~$category_regex~", $request_path)) {
            wp_redirect(home_url(), 301);
            exit;
        }
    }
    
    }
Viewing 1 replies (of 1 total)