1

WordPress supports URL rewriting through the Rewrite API. It relies on underlying HTTP server mechanisms to get the job done.

It is not always possible for WordPress to enforce the Rewrite rules. For example, with Apache, when WordPress isn't allowed to create the .htaccess file.

The plugin I'm writing should take advantage of the Rewrite API, when it is available. Basically:

if (is_url_rewriting_available( ) ) {
    Plan A
} else {
    Plan B
}

But I can't find such function.

What I investigated so far:

  • Nothing in the doc, in particular in https://codex.wordpress.org/Class_Reference/WP_Rewrite
  • "Write" functions, such as flush_rewrite_rules, return nothing. So it can't be used to check the availability (eg. if (false == flush_rewrite_rules () )).
  • In wp-admin/options-permalink.php, around line 130, the code checks URL rewriting requirements. However, this code doesn't seem to be reusable. Sure, I can copy/paste this code but...

1 Answer 1

2

You want WP_Rewrite::using_permalinks.

<?php
add_action('init', 'wpse142273_add_rules');
function wpse142273_add_rules()
{
    global $wp_rewrite;
    if ($wp_rewrite->using_permalinks()) {
        // Plan A
    } else {
        // Plan B
    }
}

Here's a larger example.

<?php
// inside a class somewhere
public static function url($area, $additional=null)
{
    global $wp_rewrite;

    if ($wp_rewrite->using_permalinks()) {
        $path = "/account/{$area}";

        if ($additional) {
            $path .= '/' . $additional;
        }

        if ('/' === $wp_rewrite->permalink_structure[count($wp_rewrite->permalink_structure) - 1]) {
            $path = trailingslashit($path);
        }
    } else {
        $q = array(
            static::ACCOUNT_VAR => $area,
        );

        if ($additional) {
            $q[static::ADDITIONAL_VAR] = $additional;
        }

        $path = '?'.http_build_query($q);
    }

    return apply_filters('frontend_accounts_url', home_url($path), $area, $additional);
}

Keep in mind that it's a-okay to just blindly call add_rewrite_rule when permalinks aren't in use. You'll need to add custom query variables anyway (by filtering query_vars) so you plugin/theme can still work just fine without rewrites.

2
  • Thank you. I guess this is the best solution available. Unfortunately, this is not the reliable check we could expect: the fact that permalinks are activated is not a proof that rewriting works. But in this situation, accessing a post from the homepage causes a big 404. So I suppose this is the WP's way to handle this. At least if my plugin falls into this trap, I will say "but your whole blog is broken anyway!" :)
    – philippe_b
    Commented Apr 24, 2014 at 15:15
  • The best you can do is check that rewrites are setup on the application -- it's not your responsibility as a plugin/theme dev to worry about server configuration. If you manage an entire site, including hosting, for someone that's a different story. Commented Apr 24, 2014 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.