最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - echo get_option in header template

programmeradmin1浏览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?

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?

Share Improve this question edited May 10, 2019 at 7:31 Pratik Patel 1,1111 gold badge11 silver badges23 bronze badges asked May 10, 2019 at 6:57 AsbjoedtAsbjoedt 53 bronze badges 1
  • 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 – norman.lol Commented May 10, 2019 at 7:12
Add a comment  | 

1 Answer 1

Reset to default 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(); ?>
发布评论

评论列表(0)

  1. 暂无评论