WordPress.org

Forums

How to print rewrite rules? (7 posts)

  1. Giovanni Putignano
    Member
    Posted 1 year ago #

    I've added a menu page and user var_dump function to print $wp_rewrite object but rules is set to null. How can I print rules? I think that admin_menu hook is called early and rules are not set yet.

  2. Christian1012
    Member
    Posted 1 year ago #

    did you call global $wp_rewrite; ?

    There are also plugins like the rewrite rules inspector. http://wordpress.org/plugins/rewrite-rules-inspector/

  3. Christian1012
    Member
    Posted 1 year ago #

    Also, the init hook would probably be best.

  4. Giovanni Putignano
    Member
    Posted 1 year ago #

    Yes, I called it. This is the code:

    add_action('admin_menu', 'gp_call_add_rewrite_page');
    
    function gp_call_add_rewrite_page() {
        add_menu_page('Rewrite Page', 'Rewrite Page', 'manage_options', __FILE__, 'gp_add_rewrite_page', 'dashicons-edit');
    }
    
    function gp_add_rewrite_page() {
        ?>
        <div class="wrap">
            <h2>Rewrite Page</h2>
            <?php
            global $wp_rewrite;
            var_dump($wp_rewrite);
            ?>
        </div>
        <?php
    }

    I set permalinks to /%postname%.html in Settings > Permalinks

  5. Christian1012
    Member
    Posted 1 year ago #

    I just tested, code works for me. Flush your rules perhaps.

  6. Giovanni Putignano
    Member
    Posted 1 year ago #

    I tried first time on 3.9-beta3 version so I tried also on 3.8.1 version but I can't see any rules also flushing rules.

  7. Giovanni Putignano
    Member
    Posted 1 year ago #

    I found a solution, I don't know if it is the best solution but it works.
    I read directly from database using this line of code:

    $rewrite_rules = get_option( 'rewrite_rules' );

    and then I loop the array.

Topic Closed

This topic has been closed to new replies.

About this Topic