Support » Fixing WordPress » Problems with actions and filters

  • I’m having some problems with the wp_head hook and the the_content filter in a plugin I’m writing. I have some values stored in an array, and I have written a function that includes checking if one of the keys in that array has a certain value. One function then hooks into wp_head, and another is filtered by the_content except that it doesn’t work as it is.

    Hopefully the code will help explain more, this is the function that hooks into wp_head:

    function my_function() {
    if(is_single() && $MyOptions['mykey'] == 'true') {
    echo "blah blah" . $MyOptions['myotherkey'] . "blah";
    } else {}

    If I remove the && $MyOptions... and leave it with just is_single, it works fine, except that this time $MyOptions['myotherkey'] isn’t echoed, just the blah will show. I know that these keys have values in the array as I have used print_r/echo on the plugin admin page to make sure.

    My plugin is kind of dependent on this, and I have no idea why including $MyOptions['mykey'] == 'true' would stop anything, and I’ve checked other plugins and seen this work.

    What is it that I need to do to get this to work?

    Thanks in advance, just ask if you need more info.

Viewing 6 replies - 1 through 6 (of 6 total)
  • OK, I’ve done more digging, and it appear that my problem is that I have a variable defined in a local scope in a function and class, that I need available in a global scope, as it is in a function being used with wp_head.

    So, how do I make this variable global when using classes?


    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Declare it global at the top of your functions.

    function foo() {
    global $bar;
    $bar = 'blah';

    If only it were that simple. That’s what I did initially, but it wasn’t working. I think it has something to do with the variable being used in a class structure, and when it is passed through wp_head, it no longer works.

    If I use the function on the plugin’s admin page, it works fine, but that is because it is still in the same class there…I think!

    I read another post in the forum that had the same problem, and the guy posted his solution, but I don’t really understand it, and can’t get it to work.

    global $icono_plugin;
    	$settings = $icono_plugin->getAdminOptions();

    Any ideas?
    Thanks again

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Well, using global is the correct answer, so I don’t know what more to tell you. I can’t help you with vague questions. Post the code or something extremely similar to it, and we can tell you why it doesn’t work. That’s the best offer I can make.

    OK, fair point, the full code is in the pastebin, and I’ve highlighted the lines where the code is giving me problems.

    If its any help, I used the devlounge plugin writing series to write the plugin.


    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    In the add_css and wordcount functions, you don’t set the $EpicTMOptions to anything anywhere. You don’t reget the option value back out of the system.

    Add $EpicTMOptions = get_option($this->epic_t_m_admin_options_name); to the top of both of those functions.

    Functions start with no variables set to anything, unless you set them yourself or declare them as being the ones from the global variable space.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Problems with actions and filters’ is closed to new replies.