1

I have written a function in functions.php that gets variables from a contact form and sends the inputs (and some other info like IP and location..) to my email.

I am starting to work on my first plugin and I want to store the parameters in a new table on the database.

How do I get the parameters from function.php right after the user clicks on the send button of the contact form?

2 Answers 2

2

There is three way to use function.php file variable into plugin file.

  1. Using global. Make sure you globalize it first.

    global $my_variable; echo $my_variable;

  2. I recommend is using WordPress built-in filter mechanism add_filter. You add the filter in your functions.php file and apply it where needed.

In functions.php:

add_filter( 'my_variable', 'return_my_variable' );
function return_my_variable( $arg = '' ) {
    return '111221122';
}

Now you can use in your plugin files:

echo apply_filters( 'my_variable', '' );
  1. Use an action hook add_action

In functions.php:

add_action( 'my_variable', 'echo_my_variable' );
function echo_my_variable() {
    echo '888998899';
}

In your plugin files:

do_action( 'my_variable' );

Again, I recommend a filter because it can return a value. This is far more flexible than injecting echo calls in your plugin file. Whether you use an action hook or a filter is entirely up to you.

1

Basic PHP:

functions.php:

function my_func() {
   //doing my staff
   $my_var = 'data'; //stored data after doing things
   return $my_var;
}

In plugin file:

//retrieved data from a function declared within a system
$got_data = my_func();

function another_func( $got_data ) {
   //doing staff with $got_data
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.