• hey guys, what is the correct way to execute the condition correctly in function.php ?
    There is no change when I write the following code :

    if (is_tag()){
    echo "hi my name is mohamamad";
    }

    but when Write like this the code works:

    function test(){
    if (is_tag()){
    echo "hi my name is mohamamad";
    }
    }
    add_action("wp_head","test");

    as you see the code only works when I put it in header.php! why this is happen and what’s the correct way to execute function and condition in function.php ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The first echoes too early. It ends up outside the page’s <html> structure and is invalid HTML. The second echoes later because you’ve hooked to “wp_head” action. The output ends up in the page’s <head> section. For regular content, it’s still in the wrong place, but it becomes visible all the same. Regular content belongs in the <body> section. You’d need a different hook like “the_content”. This is a filter, not an action. Behavior is different, you return desired content instead of echoing. “wp_head” is appropriate for outputting <meta> and similar tags that do belong in the <head> section.

    When you modify WP behavior, it’s almost always via some filter or action hook. When you execute code outside of a hook’s callback, it’s almost always too early to do so. About the only functions that should be called directly are add_action() and add_filter(). There are numerous exceptions, but that’s the general rule. Understanding filter and action hooks is crucial to customizing WP behavior.

    You may wish to review https://developer.www.ads-software.com/plugins/hooks/

    Thank you for leaving me, I will definitely check your tips

    Im thinking that your notice helped me to find out something

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to execute the condition correctly in function.php’ is closed to new replies.