1

I need to check if a page is an archive or not, before my plugin does some URI manipulations. These manipulations only work before wp but if I perform them first and then check is_archive, the check will always return false.

How can I check is_archive (or achieve the same end result) during a pre-wp hook?

It would be great if we could pass is_archive a postid to check, but it doesn't work that way.

EDIT: I realize perhaps I'm explaining myself poorly, as archives don't even HAVE postids, as far as I know. I just need some way to check if the current URI is an archive or not, before wp fires.

EDIT: On further experimenting, it appears that my URI manipulations can't take place any later than init, I think because by that point WP has already done its rewriting and such. So, I either need a way to check for an archive page status, within init, or do URI rewriting after init, either approach is fine with me as long as it gets the intended result.

1 Answer 1

1

This is what I could come up with, within init like you said ! Even if you print the $GLOBALS array, there is no way to check if the current page is an archive, is_archive is also set to blank! But if you check the $_GET array, the q variable contains the current archive type being displayed. Using this I came up with a hack, I cannot guarantee you if it will work always! But you can try something around this!

<?php

    add_action('init', 'q');

    function q()
    {
        //echo '<pre>';
        //print_r($_GET);
        //echo '</pre>';

        $q = $_GET['q'];

        //Here you can substitute other 'archive' terms, say 'author', 'date' or '<custom taxonomy name>'. Use a switch case or nested if-else to check all the archive terms in your environment!
        $pos = strpos($q, 'category');
        if($pos !== false )
            echo "CATEGORY ARCHIVE";
    }

Hope this one works!

9
  • Unfortunately it appears that I can't run this any later than init, I've updated my question to reflect that. Thanks though, any other creative ideas?
    – JVC
    Commented Oct 10, 2011 at 5:46
  • Please check the updated code and description now! Commented Oct 10, 2011 at 6:44
  • Thanks for the update, I'm afraid I'm not following it though, and it appears not to work. It seems to be looking for a query string parameter called 'q' but no such parameter exists, so I'm unclear on what it's supposed to do. Where would $_GET['q'] come from?
    – JVC
    Commented Oct 10, 2011 at 6:54
  • It comes from the $_GET array! Uncomment and check the print_r that I've commented out. It has the variable 'q'. Can you please post your code? What kind of an archive are you trying to check... 'author', 'category' or some other taxonomy? See the archive types here: codex.wordpress.org/Function_Reference/is_archive Commented Oct 10, 2011 at 6:59
  • Aha, I think I see what you meant, if I am on default permalinks (i.e. "ugly" permalink) there is a cat parameter passed by default (not category). Unfortunately I am always using "pretty" permalinks, so there is no such parameter in the query string. =(
    – JVC
    Commented Oct 10, 2011 at 7:00

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.