WordPress.org

Ready to get started?Download WordPress

Forums

Passing variable into a function that is only "called" by a hook? (8 posts)

  1. Carl
    Member
    Posted 3 years ago #

    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)

  2. Genesis2001
    Member
    Posted 3 years ago #

    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 :o

    -Zack

  3. Carl
    Member
    Posted 3 years ago #

    Exhaustive but doable workarounds. Good ideas!

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

  4. Genesis2001
    Member
    Posted 3 years ago #

    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

  5. ericperson
    Member
    Posted 3 years ago #

    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?

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    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?

  7. ericperson
    Member
    Posted 3 years ago #

    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?

  8. esmi
    Forum Moderator
    Posted 3 years ago #

    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;
    }

Topic Closed

This topic has been closed to new replies.

About this Topic