How to define and use helper functions in theme functions.php?

How to correctly perform the following actions in the functions.php file of a Wordpress theme?

I did not understand how to make the top function available to the bottom function in the theme functions.php file. I do not understand how to set up the hooks so that they can work together. Thanks.

Filter / Helper / whateveryoucallit function:

function poohToPee( $pooh_log )
{
  switch( $pooh_log )
  {
    case 'gross-poop':
      $pee_equivalent = 'Grossest of Pees';
    break;
    case 'ok-poop':
      $pee_equivalent = 'Bland Snack Pee';
    break;
    case 'shang-tsung-plop':
      $pee-equivalent = 'Random U-Stream';
    break;
  }
  return $pee_equivalent;
}

Ajax handler function:

function screw_loose()
{
  if( isset($_REQUEST['pooh_log']) )
  {
    echo poohToPee( $_REQUEST['pooh_log'] );
  }
}
add_action('wp_ajax_priv_screw_loose', 'screw_loose')
+4
source share
1 answer

add_action usually calls the function you pass with at the point where the hook is called.

- ajax, , ? , .

, functions.php, .

, , add_action( 'admin_init', array( $this, 'someFunction' ) ); add_action, , __construct .

, :

class helloWorld
{
    function __construct()
    {
        add_action( 'admin_init', array( $this, 'echoItOut' ) );
    }

    function echoItOut()
    {
        echo 'Hello World';
    }
}

$helloWorld = new helloWorld;

:

class helloWorld
{
    function echoItOut()
    {
        echo 'Hello World';
    }
}

$helloWorld = new helloWorld;

add_action( 'admin_init', array( $helloWorld, 'echoItOut' ) );

:

function echoItOut()
{
    echo 'Hello World';
}

add_action( 'admin_init', 'echoItOut' );

, functions.php, , , "Hello World" , ( ).

+1

All Articles