Need to call function from another php page

sharingsunshine
sharingsunshine used Ask the Experts™
on
I have a php function that I want to call from another php file.  This function creates a button and places a message in Order Notes.  Also, the function isn't the only function in functions.php.

Here is the function
add_action ('woocommerce_before_order_notes', 'member_note', 10); 
function member_note (){
$code = <<< EOT
<button style= "background-color:green; color:white;" onclick="myFunctionMember()">MEMBERSHIP</button>
<script>
function myFunctionMember() {
    document.getElementById("order_comments").innerHTML = "I want membership";
}
</script>
EOT;
echo $code;
}

Open in new window


This function needs to be at the bottom of this yellow box.  https://gyazo.com/fad06970c1cec8be092987e46331a7c3 which is being created with this code.  

<?php if (($val >= 100) && ($val <= 300)) { ?>
<p style="color: black; font-weight:bold; text-align:center; margin:0 auto; background-color: yellow; padding-top:6px; padding-bottom:6px; font-size:16px; font-family:Verdana, Arial, Helvetica, sans-serif;">
		<span class="heading">You've Qualified For FREE SmartStart Membership!</span><br />
		<br />
  $15 product credit for future use, shipping promotions and much more.</span>
	  <br /><br />
	Click The Green Membership Button To Recieve Your Membership Benefits</p>


<?php include('functions.php');
	$returning_value = member_note();								   
echo $returning_value;
 } ?>

Open in new window


This code is at qv_contest.php

You can see my attempt that resulted in this error:

Fatal error: Cannot redeclare member_note() (previously declared in C:\xampp\apps\wordpress\htdocs\wp-content\themes\flatsome-child\functions.php:25) in C:\xampp\apps\wordpress\htdocs\wp-content\themes\flatsome-child\functions.php on line 33

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
David FavorFractional CTO
Distinguished Expert 2018

Commented:
Looks like your code is arranged so that the member_note() function is defined twice.

Just place a code guard around your function declaration to only allow one declaration, something like this...

if (! function_exists("member_note")) {
... define your member_note() function here ...
}

Open in new window

Author

Commented:
the error went away but it doesn't display the member button.
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
I would strongly recommend that you don't go including the functions.php file in your pages. That's not how the functions file is supposed to be used.

What you should do here is hook into the WordPress action system. Where you need the button, make a call to do_action():

Click The Green Membership Button To Receive Your Membership Benefits</p>
<?php do_action('show_my_button'); ?>

Open in new window

And then in your functions file, just hook into that action:

add_action ('show_my_button', 'member_note', 10);

Open in new window

This is hooking to the same function as before (member_note). Obviously if you're including this block twice on the same page, then you'll be including the JS myFunctionMember() function a second time, so you might need to address that.

FYI - you've spelt receive wrong: Button To Recieve Your Membership

Author

Commented:
Thanks for setting me straight and solving my issue.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial