2

I set static page as front page.

I need to know if user currently on homepage in my custom plugin.

Functions is_home() and is_front_page() doesn't works, since homepage is static page.

I can get an id of this page :

$frontpage_id = get_option('page_on_front');

But how to get id of current page from admin plugin? (Then i'll be able to compare those ids and detect if current page is homepage!)

2 vancoder Code:

1) Set any static page as frontpage.

2) Create dummy plugin

3) Code of plugin :

$d = is_front_page();
var_dump($d);

==> 

bool(false)

UPD

http://codex.wordpress.org/Conditional_Tags#The_Front_Page

should work, by didn't

2 Vancoder

Admin plugin means just plugin :) sorry

And those two lines is all context for now, try it yourself, it doesn't works.

WP version 3.3.2

3
  • is_front_page() should work. Can you show some of your code?
    – vancoder
    Commented Apr 4, 2013 at 21:04
  • I think you need to show your code in more context. is_front_page should work.
    – vancoder
    Commented Apr 4, 2013 at 21:17
  • 1
    What does your question mean by admin plugin?
    – vancoder
    Commented Apr 4, 2013 at 21:18

2 Answers 2

4

This is why I asked for more code context. I'll have to guess that you are checking for the front page outside of any hooked function, or inside a function that is called too early, before is_front_page() is ready.

The following will work.

function your_function()  {
$d = is_front_page();
var_dump($d);
}

add_action( 'wp', 'your_function' );
1
  • It works!!! Tnx, mate, saved a lot of time :)
    – johnode
    Commented Apr 4, 2013 at 22:01
3

I guess you are talking about the editor screen? If so, you can use the global variable $post_ID:

add_action( 'edit_form_after_title', 'wpse_94626_front_page_detection' );

function wpse_94626_front_page_detection()
{
    global $post_ID, $post_type;

    if ( empty ( $post_ID ) or 'page' !== $post_type )
        return;

    if ( $post_ID === (int) get_option( 'page_on_front' ) )
        print '<p><b>This is the front page!</b></p>';
}

enter image description here

3
  • No, i talk about: when user loads any page on frontend, i should detect if this is homepage (in my custom plugin)
    – johnode
    Commented Apr 4, 2013 at 21:15
  • @sbxmal You cannot get to the front page page; WordPress will redirect that request to the front page automatically, and then is_front_page() is true.
    – fuxia
    Commented Apr 4, 2013 at 21:20
  • Maybe, i explained this not really clearly. I have plugin with code above ($d = is_front_page(); var_dump($d);). When i go to homepage of the site, i want to check in plugin is it really homepage or any other page. Homepage is set as static page.
    – johnode
    Commented Apr 4, 2013 at 21:25

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.