WordPress.org

Support

Support » Plugins and Hacks » Hacks » Passing variable into a function that is only "called" by a hook?

Passing variable into a function that is only "called" by a hook?

  • Normally in PHP a function call accepts parameters and thus you can pass outside variables into the function:

    //variable is declared
    $foo = 'bar';
    
    //function is created
    function foobar($parameter) {
       echo $parameter;
    }
    
    //function is called with previously declared variable
    foobar($foo);
    
    Outputs: bar

    However, when using WordPress Hooks the callback doesn’t accept parameters and thus I’m wondering if there’s another way to pass variables declared outside of the function’s scope into that function?

    (‘global’ variables doesn’t seem to work: probably because all theme/plugin-functions are nested functions thus the parent level isn’t truly global)

Viewing 7 replies - 1 through 7 (of 7 total)
  • You could plop it into the database into a temporary storage area and then when wordpress “shutsdown” delete all the data from this temporary table. (or create a temporary table with MySQL).

    Or, use cookies 😮

    -Zack

    Exhaustive but doable workarounds. Good ideas!

    I’m hoping for something more native and less labor-intensive though.

    You can also wrap your plugin in a class too. That’s what I’m doing right now with the plugin I’m writing for work.

    -Zack

    I am having trouble passing variables into a function I am attaching to the admin_head hook. Is there something I’m missing here? I keep getting “Invalid argument supplied for foreach()”

    $array = array('posts', 'links', 'comments');
    add_action('admin_head','hides_menus');
    <?php
    function hides_menus() { ?>
    	<style type="text/css">
    	<?php
    	foreach ($array as $value) {
    		echo '#menu-' . $value . ', ';
    		} ?> { display:none; } </style>
    	<?php } ?>

    Or do I have to do something with classes to fix this?

    esmi

    @esmi

    Forum Moderator

    add_action('admin_head','hides_menus');
    <?php
    function hides_menus() {
    	$array = array('posts', 'links', 'comments');?>
    	<style type="text/css">
    	<?php
    	foreach ($array as $value) {
    		echo '#menu-' . $value . ', ';
    		} ?> { display:none; } </style>
    	<?php } ?>

    perhaps?

    Thanks esmi, that’s perfect (except for the “<?php” on the second line, but that was my fault). The only thing is it outputs the last comma, which breaks the CSS:
    #menu-posts, #menu-links, #menu-comments, { display:none;}

    In terms of best practices, what’s the best/most efficient way to remove the trailing comma? It could be done with a conditional, or with substr, like this:

    substr($thelistofvalues, 0, -2);

    Everything that I can think of makes the code a lot longer and harder to understand. Any suggestions?

    esmi

    @esmi

    Forum Moderator

    How about:

    add_action('admin_head','hides_menus');
    function hides_menus() {
    	$array = array('posts', 'links', 'comments');?>
    	$output = '<style type="text/css">';
    	foreach ($array as $value) {
    		$output .= '#menu-' . $value;
    		if( $value != 'comments' ) $output .= ', ';
    	}
    	$output .= '{ display:none; } </style>';
    	echo $output;
    }
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Passing variable into a function that is only "called" by a hook?’ is closed to new replies.
Skip to toolbar