is_home vs is_front_page
-
Hi,
I need to run some code just on the home page of my site with free Virtue Theme.
Currently my configuration is to show the latest posts as homepage.
I found this code in an old thread
$kills = array('page', 'sticky', 'home', 'front_page', 'category', 'tag', 'archive', 'search', 'single', 'attachment'); foreach ($kills as $k) { $fn = "is_$k"; printf("Output of %s is: %b \n", $fn, $fn()); }
and the result is that my homepage is neither is_home nor is_front_page
How could it be? How can I solve?
Thanks
-
I’m confused by your code? What are you trying to do exactly?
If you set the front page as your posts page then it is both is_home and is_front_page.
If you’re using a static page then it would be is_front_page and your posts page would be is_home.
Kadence Themes
I found this code on this forum and it’s supposed to tell whatever type of page is the displayed one.
The problem is that i get all 0 on my homepage…
I need to add a code to functions.php in order to automatically log-out users if they’re on the homepage.
Tnx
So you can use:
if(is_front_page()) { //code here }
Would that work?
Kadence ThemesAlready tried, not working, this is why I’m asking for help ??
Neither is_front_page nor is_home did the trick, since my homepage seems to be nothing ??
I’m not trying to sound unfair but is it possible that your code you’re using inside is_front_page() is not working?
Can you post what that code is?
I assume you’re working in a child theme? And you have that activated. Is there any other code in your child theme functions.php file?
How are you hooking the function that runs is_front_page(). Which page hook are you using?
Kadence Themes
The code i’m using is simple: wp_logout();
I want users to automatically logout if on the homepage of my site.
Yes, I’m using a child theme. Yes, I have other codes in function.php and all of them is working.
Thanks
Just want to get a clear picture.. can you send me your whole code.
How are you hooking the function that runs is_front_page(). Which page hook are you using?
Kadence Themes
How are you hooking the function that runs is_front_page(). Which page hook are you using?
I’m honest, I don’t know what this means…
This is the whole code of my functions.php
<?php /** Carica stile per tema child */ add_action( 'wp_enqueue_scripts', 'enqueue_parent_theme_style' ); function enqueue_parent_theme_style() { wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' ); } /** Lingua italiana per plugin Gravity Uploader add_filter( 'prso_gform_pluploader_i18n_script', 'ben_test_18n' ); function ben_test_18n( $plupload_i18n_script ) { return 'it'; } */ /** * Gravity Wiz // Gravity Forms // Send Manual Notifications * * Provides a custom notification event that allows you to create notifications that can be sent * manually (via Gravity Forms "Resend Notifications" feature). * * @version 1.2 * @author David Smith <[email protected]> * @license GPL-2.0+ * @link https://gravitywiz.com/send-manual-notifications-with-gravity-forms/ */ class GW_Manual_Notifications { private static $instance = null; public static function get_instance() { if( null == self::$instance ) self::$instance = new self; return self::$instance; } private function __construct() { add_filter( 'gform_notification_events', array( $this, 'add_manual_notification_event' ) ); add_filter( 'gform_before_resend_notifications', array( $this, 'add_notification_filter' ) ); } public function add_notification_filter( $form ) { add_filter( 'gform_notification', array( $this, 'evaluate_notification_conditional_logic' ), 10, 3 ); return $form; } public function add_manual_notification_event( $events ) { $events['manual'] = __( 'Send Manually' ); return $events; } public function evaluate_notification_conditional_logic( $notification, $form, $entry ) { // if it fails conditional logic, suppress it if( $notification['event'] == 'manual' && ! GFCommon::evaluate_conditional_logic( rgar( $notification, 'conditionalLogic' ), $form, $entry ) ) { add_filter( 'gform_pre_send_email', array( $this, 'abort_next_notification' ) ); } return $notification; } public function abort_next_notification( $args ) { remove_filter( 'gform_pre_send_email', array( $this, 'abort_next_notification' ) ); $args['abort_email'] = true; return $args; } } function gw_manual_notifications() { return GW_Manual_Notifications::get_instance(); } gw_manual_notifications(); /** Aggiunge un numero univoco di registrazione */ add_filter("gform_field_value_uuid", "get_unique"); function get_unique(){ $prefix = "GAR16"; // update the prefix here do { $unique = mt_rand(); $unique = substr($unique, 0, 8); $unique = $prefix . $unique; } while (!check_unique($unique)); return $unique; } function check_unique($unique) { global $wpdb; $table = $wpdb->prefix . 'rg_lead_detail'; $form_id = 1; // update to the form ID your unique id field belongs to $field_id = 12; // update to the field ID your unique id is being prepopulated in $result = $wpdb->get_var("SELECT value FROM $table WHERE form_id = '$form_id' AND field_number = '$field_id' AND value = '$unique'"); if(empty($result)) return true; return false; } /** Custom activation email //First we'll setup the from name and sender email. //Here is the filter for the from name: //add_filter('wp_mail_from', 'new_mail_from'); //Here is the filter for the sender name: //add_filter('wp_mail_from_name', 'new_mail_from_name'); //Here is where the new from email goes: //function new_mail_from($old) { //return '[email protected]'; //} //Here is where the new sender name goes: //function new_mail_from_name($old) { //return 'New Sender Name'; //} //Next we'll hook into the subject and setup a function to change it: add_filter( 'wpmu_signup_user_notification_subject', 'my_activation_subject', 10, 4 ); function my_activation_subject( $text ) { //Here is where to input the new subject for the activation email: return 'Gardena Ti Rimborsa - Conferma indirizzo email'; } // Finally we hook into the email itself and run a function to modify the message. add_filter('wpmu_signup_user_notification_email', 'my_custom_email_message', 10, 4); function my_custom_email_message($message, $user, $user_email, $key) { //Here is the new message: $message = sprintf(__(( "Gentile cliente,\n\n benvenuto nella promo Gardena Ti Rimborsa.\n\n Per confermare il tuo indirizzo email clicca il seguente link:\n\n%s\n\n Grazie,\n\n Il Team Gardena Ti rimborsa\n\n" ), $user, $user_email, $key, $meta),site_url( "?page=gf_activation&key=$key" )); return sprintf($message); } */ /** Gravity Forms scroll */ add_filter( 'gform_confirmation_anchor', '__return_true' ); /** * Gravity Forms Custom Activation Template * https://gravitywiz.com/customizing-gravity-forms-user-registration-activation-page add_action('wp', 'custom_maybe_activate_user', 9); function custom_maybe_activate_user() { $template_path = STYLESHEETPATH . '/gfur-activate-template/activate.php'; $is_activate_page = isset( $_GET['page'] ) && $_GET['page'] == 'gf_activation'; if( ! file_exists( $template_path ) || ! $is_activate_page ) return; require_once( $template_path ); exit(); } */ /** Autologin */ if (!is_user_logged_in()) { if (isset($_GET['u']) && !empty($_GET['u'])) { $u = $_GET['u']; $result = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_users WHERE concat('MKe4eUKM72GeoXRCjd7231Tusu3X9iTTG0N7pju5LHlZ4IIVh6uXKcTVe1cqgsk1OD7ImkRS7fXeaiBTrQrzjpjEZwNCHsOCZW4GBRXR5gK5uV',user_email,'EkkdTXyNpeJAgB6Bg2QDpiVwKzsVet1YxDyZstTFPBAWI851hCMivKi9JxV0aCQj8GzoG6N3hUfp2nTNtGTWxhTjfS') = '%s' AND user_login != 'admin'",$u)); if (isset($result->ID) && isset($result->user_login)) { wp_set_current_user($result->ID, $result->user_login); wp_set_auth_cookie($result->ID); } } } /** Autologout if (is_home()) { wp_logout(); } $kills = array('page', 'sticky', 'home', 'front_page', 'category', 'tag', 'archive', 'search', 'single', 'attachment'); foreach ($kills as $k) { $fn = "is_$k"; printf("Output of %s is: %b \n", $fn, $fn()); } */ /*Auto-login After Gravity Forms Registration */ add_action( 'gform_user_registered', 'tme_gravity_registration_autologin', 10, 4 ); function tme_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) { $user = get_userdata( $user_id ); $user_login = $user->user_login; $user_password = $password; wp_signon( array( 'user_login' => $user_login, 'user_password' => $user_password, 'remember' => false ) ); } /* Disable WordPress Admin Bar for all users */ show_admin_bar(false);
As said before I tried both if (is_home()) and if(is_front_page())
ok,
so I see this:
/** Autologout if (is_home()) { wp_logout(); }
But that isn’t’ hooked into the front end so it would never run.
Here is an example how you could change that.
add_action('wp_enqueue_scripts', 'custom_logout'); function custom_logout(){ if (is_front_page()) { wp_logout(); } }
But you have to note that you would still appear to be logged in (admin bar for example) until you reloaded or went to a differnet page because by the time you can check for front page you’re already loading it logged in.
so if you need it to appear as though you are logged out you would need to add a script to reload which will make the page load twice.
add_action('wp_enqueue_scripts', 'custom_logout'); function custom_logout(){ if (is_front_page()) { if(is_user_logged_in() ){ wp_logout(); header("Refresh:0"); } } }
Kadence Themes
- The topic ‘is_home vs is_front_page’ is closed to new replies.