is_front_page() in the below code is not working… this function just doesn’t run when I’m on the front page, any idea why? am I doing something wrong?
function save_landing_page_slider()
{
if (isset($_GET['slider']) && is_front_page()) {
$current_user_id = get_current_user_id();
$slider = $_GET['slider'];
update_field('user_landing_slider', $slider, 'user_'.$current_user_id);
}
}
add_action('init', 'save_landing_page_slider', 10, 2);
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
add_action('init', 'save_landing_page_slider', 10, 2);
init is too early — if you look at the WordPress query overview on Codex, init is (as of writing) the second step, whereas is_ variables like $is_page (or WP_Query::$is_page) that are used by conditional tags like is_front_page() are only set in step 4, so instead of init, you should use a later hook like wp or template_redirect:
add_action( 'template_redirect', 'save_landing_page_slider' );
So in order for conditional tags to work properly, make sure to call them in the right hook or place.
Additionally, you should also understand when would is_front_page() return true, e.g. when your homepage is set to a static Page, and that you’re on the homepage.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0