I want testFunction()
to call check()
that i created in my plugin php when i click My Button. How do i make it work ?
<?php
function createButton()
{
echo '<button onclick="testFunction()">My Button</button>';
}
function check()
{
alert("Works");
}
function my_php_function()
{
echo '<script>
function testFunction() {
check();
}
</script>';
}
add_action( 'wp_footer', 'my_php_function' );
I want testFunction()
to call check()
that i created in my plugin php when i click My Button. How do i make it work ?
<?php
function createButton()
{
echo '<button onclick="testFunction()">My Button</button>';
}
function check()
{
alert("Works");
}
function my_php_function()
{
echo '<script>
function testFunction() {
check();
}
</script>';
}
add_action( 'wp_footer', 'my_php_function' );
Share
Improve this question
edited Jun 5, 2020 at 2:15
Kitiara
asked Jun 5, 2020 at 0:46
KitiaraKitiara
1114 bronze badges
2
|
2 Answers
Reset to default 0AJAX is the solution. For more info: https://codex.wordpress/AJAX_in_Plugins#Separate_JavaScript_File
I could not stand by and see that mangled code. Here is what I think you meant to do.
<?php
function createButton()
{
echo '<button onclick="testFunction()">My Button</button>';
}
function my_php_function()
{
echo '<script>
function check()
{
alert("Works");
}
function testFunction() {
check();
}
</script>';
}
add_action( 'wp_footer', 'my_php_function' );
check()
either needs to be a JavaScript function in a .js file or between<script></script>
tags, or if it needs to be a PHP function, you need to use AJAX to run the function on the server. Your code is just sample code so I can't tell what you need. – Jacob Peattie Commented Jun 5, 2020 at 1:19check()
needs to be a PHP function. So i need to use AJAX, how do i do that ? – Kitiara Commented Jun 5, 2020 at 1:29