开发者

PHP getimagesize is not working when is called from a function in function.php (Wordpress)?

开发者 https://www.devze.com 2023-01-31 18:48 出处:网络
PHP getimagesize is not working when is called from a function in function.php. function.php: <?php // Theme Options

PHP getimagesize is not working when is called from a function in function.php.

function.php:

<?php

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

    add_action('wp_head', 'theme_options', 'get_image_size');

    function theme_options() {
        // Initiate Theme Options
        $options = get_option('plugin_options');

        // If a logo image was uploaded then remove text from site title
        if ($options['logo'] != NULL)
            $remove_text = '-9999px';

        else
            $remove_text = 0;

        ?><style>
        body {
            background-color: <?p开发者_如何学JAVAhp echo $options['color_scheme']; ?>
        }

        #header h1 a {
            background: url(<?php echo $options['logo']; ?>) no-repeat scroll 0 0;
            text-indent: <?php echo $remove_text; ?>;
        }
        </style><?php
    }

    function get_image_size() {

        list($width, $height, $type, $attr) = getimagesize($options['logo']);

        echo "Image width " .$width;
        echo "<BR>";
        echo "Image height " .$height;
        echo "<BR>";

        var_dump($width);
        var_dump($heigt);
    }

$options['logo'] is returning http://localhost/wordpress/wp-content/uploads/2010/12/logo4.png so the image is being displayed.

I also did var_dump to $width and $height but they didn't show up.

Any suggestions?

EDIT: I pasted the full code of functions.php. $options['logo'] works perfectly in the theme_option function so I don't know why it doesn't work in the get_image_size function.


$options['logo'] is undefined in your code. If it is defined outside of your function, it is not by default available inside of your function.

Please enable error reporting using ini_set('display_errors', 1) and error_reporting(E_ALL), when developing. This will make sure any errors are reported.


I you do not see any error messages, turn up error_reporting and display_errors.

$options['logo'] works perfectly in the theme_option function so I don't know why it doesn't work in the get_image_size function.

As @Sjoerd said: $options is not defined in function get_image_size. It is only defined in function theme_options. That's what functions are about, they are a black box that know about their environment only from the arguments they receive. If you want to make the options visible in function get_image_size, you have to Initiate Theme Options in that function as well.


I found out how to fix it (separating the functions into different add_action statements):

add_action('wp_head', 'theme_options');

function theme_options { 
    ...
}

add_action('wp_head', 'get_image_size');

function get_image_size { 
    ...
}

The add_action only allows one function?

What was the problem?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号