• I need to create a checkbox to load a popup only in the home page.
    If it’s checked only loads in the home, but I don’t know how to make work.

    function set_plugandpop() {
      $options = get_option('plugandpop_settings');
    
    // I don't know if this is right
      function checkWhere() {
        if ( $options['homeonly'] == '1' ) {
           return is_front_page() ;
        } else {
          return !is_admin() ;
        }
      }
    // I don't know If this is possible
      if ( checkWhere() && $options['active'] == '1' ) {
         create_plugandpop();
      }
    } add_action ('init', 'set_plugandpop');

    I tried if ( is_home && $options['active'] == '1' )

    But this doesn’t work.

Viewing 7 replies - 1 through 7 (of 7 total)
  • I don’t quite understand the code here or how it’s supposed to work…

    If you log in and go to Settings -> Reading you either have “Your latest posts” set or “A static page” set.

    IF Your latest posts is set then you’ll need to check against the conditional function is_home() which returns either true or false depending if you’re viewing the posts page.

    IF A static page is set then you’ll need to check against the conditional function is_front_page() which returns either true or false depending if you’re viewing your set front page ( set in the Settings -> Reading ).

    So I guess maybe the code could go something like this:

    // Assuming you have a set front-page.
    if( is_front_page() && $options['active'] == '1' ) {
         create_plugandpop();
    }
    Moderator bcworkz

    (@bcworkz)

    There is no point in returning any values from an action callback, any returned value is ignored. Returned values are used by filters, not actions. Rule #2 of WP coding after don’t alter core code 🙂

    “init” fires on every page request, so it’s important to check applicability first and immediately return if no further action is required. There is no reason to get option if it’s not a front page request.

    Where do you actually implement the popup code when it’s wanted? Enqueue scripts? “wp_head” action? It seems to me where ever that is is the appropriate place to check options and is_front_page(). Then either implement the popup or don’t.

    Thread Starter gisesonia

    (@gisesonia)

    Thanks howdy, i am a begginer and i am learning how WordPress plugins works, but i understand that I have to use is_home or is_front_page, what i don’t know is how to translate in code, what i need to do.

    I will try to explain better, when loads the first page of the website shows a popup, but when I navigate to another pages, i don’t want to load the or if i have another type of popup i want that the popup shows in all pages.

    The checkbox will control enable or desable where the popup will appear.

    This code it’s from a friend but I think that it’s something wrong and i don’t know how to fix that.

    Bcworks, i didn’t mean to change WordPress, so it’s wrong to return something in this case?

    “init” fires on every page request” – i didn’t know about that, where i can learn about it?

    “There is no reason to get option if it’s not a front page request.”

    The popup will load in the homepage, but the checkbox will change so can load in other pages.

    Thank you.

    An example could look like this:

    function set_plugandpop() {
    
    	// Check if we're viewing the front page
    	if( is_front_page() ) {
    		
    		$options = get_option( 'plugandpop_settings' );
    		
    		if( '1' === $options['active'] ) {
    			create_plugandpop();
    			
    		}
    		
    	}
    	
    }
    add_action( 'init', 'set_plugandpop' );

    It’s still unclear if a frontpage is set in Settings -> Reading though so again it may be is_home() instead of is_front_page(). If you’re learning I would suggest reading the above documentation and understanding the difference between the two functions. This is also a good doc on the topic:

    Creating a Static Front Page

    Finally, if you’re unfamiliar with dev it may be beneficial to checkout your local WordPress Meetup or local WordCamp where you can get active help.

    Thread Starter gisesonia

    (@gisesonia)

    It’s a static page, not the posts pages, the first pages that loads.

    Thread Starter gisesonia

    (@gisesonia)

    I don’t know If it’s the best solution create the plugin like this, If it’s the right place to put a condition.

    My friend saids that init doesn’t solve, só he changes to wp, he said that wasn’t load when he uses that code, so that was why it’s not working.

    Moderator bcworkz

    (@bcworkz)

    There’s never a reason to return a value from any action hook. It will be ignored in WP core. An action fires at a certain point in execution which allows us to do something special at that point. Filters OTOH must return a value because WP uses the returned value to do something.

    The best way to learn about “init” is to examine how it is used in source code. If you check code above the linked code, you can see WP is still loading and is unstable. You should get a sense that it fires on every request, though it’s not explicitly stated. You can prove it to yourself by working forward from index.php until you get to the linked code. I’d just accept it as fact for now 🙂

    If you scroll up from the link you’ll find the “wp” action, which is before user and theme code executes, so it’s a poor choice for something that affects output. “init” and “wp_loaded” are the first actions to fire once WP is stable.

    Getting an option takes some processing power so shouldn’t be done unless it’s really needed. By first checking is_front_page() like in Howdy’s example, you avoid doing so unless the option value is needed.

    Using the “init” action is probably fine, but there might be a more optimal way to setup the popup. Without knowing what create_plugandpop() does, I cannot suggest a more optimal hook.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to use checkbox option for check is_home’ is closed to new replies.