0

I want to do the following PHP statement within function.php: If there's an uploaded image for #header h1 a's background, indent the text (remove)...If not (else), keep the text (don't indent it).

I'm not very familiar with PHP but this is what I did:

<?php

// Include functions of Theme Options
require_once(TEMPLATEPATH . '/functions/admin-menu.php');

// If there's an image for the site title, remove it's text
function logo_exists() {
    ?><style type="text/css">
        #header h1 a {
        <?php
        if ( $options['logo'] == NULL ) { ?>
            float: left;
            text-indent: 0;
        <?php } else { ?>
            background: url(<?php echo $options['logo']; ?>) no-repeat scroll 0 0;
            float: left;
            text-indent: -9999px;
            width: 160px;
            height: 80px;
        }
        <?php } ?>
    </style><?php
}

header.php:

    // Initiate Theme Options
    $options = get_option('plugin_options');
var_dump($options['logo'])
    ?>

    <style>
        body {
            background-color: <?php echo $options['color_scheme']; ?>
        }
    </style>
</head>

<body <?php body_class(); ?>>
<div id="wrapper">
    <div id="header">
        <h1>
            <a href="<?php echo home_url( '/' ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a>
        </h1>

(I'm not sure if it is useful information but plugin_options is initialised just before the <style> tags.)

I did var_dump and #options['logo'] displays:

string(63) "http://localhost/wordpress/wp-content/uploads/2010/12/logo3.png" 

(So the image is working)

But right now I just see the Wordpress site title

Do I have to "activate" or "initiate" the function **logo_exists? or is there another problem?**

(Everything works ok if I don't use the PHP If statements and declaring $options[logo] directly from header.php)

Thanks in advance!

2
  • I recommend to reformat your code with if..endif syntax to become more readable (in mixed PHP/HTML codes). Commented Dec 19, 2010 at 9:30
  • BlotClock still has a point though, the $options variable has no scope inside your function, if you want to reference that variable inside that function you need to globalise it inside the function. The issue, i think, is that your function needs to be hook onto an appropriate action, for example add_action( 'wp_print_styles', 'logo_exists' ) to output the content into an appropriate position in header for styling. Commented Dec 19, 2010 at 11:18

1 Answer 1

3

$options doesn't exist in the scope of that function. You need to add

global $options;

just after the function declaration, so that it picks up the $options variable from the include. Like this:

function logo_exists() {
    global $options;

    ?><style type="text/css">
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I tried that but still not working (still seeing the text of #header h1 a. I'm not sure if it is useful information but plugin_options is initialised just before the <style> tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.