0

I'm having a run at developing a theme options plugin and right now working on implementing a 'hero' header, checking first to see if my plugin is active using:

if( function_exists( 'my_header_function ) ): my_header_function();
else: 
/* carry on and output the default header... */
endif;

Now as my hero header depends on whether the image and logo have been uploaded I wonder if I can manually return a 'false' result from my hook in order to have the original conditional in my header carry on an output the normal header.

Any ideas? Many thanks.

  • Why don't check the value of the image and logo options? (I assume they are options are you are developing a theme options plugin). – cybmeta Feb 10 '15 at 16:55
  • I'm doing that in the plugin file, cybmeta but trying to keep any mention of specific fields out of my theme files in order to maintain backwards compatibility in the event the plugin is deactivated. – Kevin Nugent Feb 11 '15 at 10:25
  • I meant exactly what is proposed in the acepted answer: check the image and logo field in the function to return false if needed. – cybmeta Feb 11 '15 at 10:47
  • Got it. Nice one cybmeta. I'm having a great time with hooks and shortcodes right now! – Kevin Nugent Feb 11 '15 at 21:12
0

Have your function return your HTML rather than echo it. Perform the logic in the function to see if the image/logo are present and only return the HTML if that's the case, otherwise returning null/false.

function my_header_function( $output = false ) {
     if ( /*image and logo have been uploaded */ ) {
          $output = /* HTML of your hero header */;
     }
     return $output;
}

Add a check to your hook function to see if your function returns content; if it does, echo it.

if ( function_exists( 'my_header_function' ) && ( $hero = my_header_function() ) ) {
     echo $hero;
}
| improve this answer | |
  • Thanks karpstrucking, that's worked like a charm. I have a follow up question however: I had originally implemented this with a custom hook: 'function nto_hero() { do_action('nto_hero'); }' but that doesn't really fit with this solution... or does it? Is there a benefit to favouring either method when it comes to plugin development? Thanks again. – Kevin Nugent Feb 11 '15 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.