0

I was trying to get this to work yesterday but my WP website is giving me trouble. This is my first attempt at creating plugin so please excuse me.

I want to show an ad banner in header based on image and link URLs that user input through plugin. I have got backend and database connection to work but I get PHP error when trying to show/call the information in the header template.

Here is my code:

if (function_exists('vis_undertoner_reklamebanner')) {
if( get_option( 'undertoner_reklamebanner_boolean' ) === 1 )
{
function vis_undertoner_reklamebanner() {
    echo '<div class="reklamebanner">
                <a href="<?php echo get_option( 'undertoner_reklamebanner_hjemmeside' ); ?>" target="_blank">
                    <img src="<?php echo get_option( 'undertoner_reklamebanner_reklamebillede' ); ?>" />
                </a>
            </div>';
        }
    }
}

Can you help?

  • Welcome to WordPress Stack Exchange :-)! We love to help. Unfortunately it's unclear what you are actually trying to achieve, what's wrong with the outcome, what it's actually supposed to do, where it's getting printed, and what plugin exactly? Please update your question and provide a narrowly-scoped and detailed question that enables us to provide you a canonical answer. Many thanks – leymannx May 10 '19 at 7:12
1

There's quite a few issues with your code:

  1. Your code is only defining a function if the condition is true. It doesn't actually output anything.
  2. Your condition is using a strict comparison to 1, but options will always be strings and 1 does not === to '1'.
  3. That function is only defined it if it's already defined, which is paradoxical.
  4. You have not mixed HTML and PHP correctly. This would be the source of any PHP issues. You need to properly concatenate and pay attention to when your quotes, " and ', are opened and closed.

What you should do is always define the function, and just check the condition inside the function. This should be done in functions.php, then you should call that function in header.php.

So in functions.php your function should look like this:

function vis_undertoner_reklamebanner() {
    if ( get_option( 'undertoner_reklamebanner_boolean' ) === '1' ) {
        echo '<div class="reklamebanner">
                <a href="' . get_option( 'undertoner_reklamebanner_hjemmeside' ). '" target="_blank">
                    <img src="' . get_option( 'undertoner_reklamebanner_reklamebillede' ). '" />
                </a>
            </div>';
        }
    }
}

Then you use it in header.php like this:

<?php vis_undertoner_reklamebanner(); ?>
|improve this answer|||||

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.