• I created a WordPress Plugin to display Events.

    The plugin is developed via WordPress Block and works fine. The url to the detail page is http://localhost:4444/?slug=02-12-2024_artist_name

    To make it better readable, the url should look like http://localhost:4444/event/02-12-2024_artist_name

    I added this code to my plugin.php script

    add_action('init',  function () {
        add_rewrite_rule('event/([a-z0-9-]+)[/]?$', 'index.php?slug=$matches[1]', 'top');
        add_rewrite_tag('%slug%', '([^&]+)');
        error_log("Rewrite rules added");
    });
    
    add_filter('query_vars', function ($query_vars) {
        $query_vars[] = 'slug';
        return $query_vars;
    });
    
    

    But the rewrite doesn´t work correctely and i get a 404 “page not found”.

    I tried to implement the render.php a second time (first time is via block.json), but I think it´s not the right way, cause I didn´t get my attributes

    add_filter('template_include', function ($template) {
        if (get_query_var('slug') == false || get_query_var('slug') == '') {
            require_once plugin_dir_path(__FILE__) . 'src/render.php';
            return $template;
        }
    });
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘WordPress Block – add_rewrite_rule doesn´t work’ is closed to new replies.