0

I would like to call some function from a plugin I am writing. I call the plugin code through a shortcode on my wordpress page. To start, I am simply trying to pop an 'alert' window, say, if I am successful in connecting to the Database. Eventually, the plugin will display drop downs one below the other, the later ones being populated based on the selection in previous one. Appreciate help! Code in my plugin is as follows:

<?php
/*
Plugin Name: Custom program graphs
Plugin URI: localhost/test/
Description: Creates graphs
Version: 0.1.0
Author: My Cars
*/

require_once("dbcontroller.php");       # Instantiate DB Controlling - WORKS WELL functions 
function vc_connect_to_database()
{
    $db_handle = new DBController();
    $connection = $db_handle->connectDB();
    if (!$connection)
    {
        return '<h6>' . "Unable to connect to database." . '</h6>';
    } 
    # WORKS WELL IN THE CONDITION ABOVE THIS
    else
    { 
        # FAILS HERE!! HOW CAN I CALL A FUNCTION LIKE alert FROM HERE?
        return window.alert("Connected to the database successfully.");  
    }   
}
add_shortcode('connect_database', 'vc_connect_to_database');
1
  • window.alert isn't a PHP function, it's a javascript function, you can't call Javascript functions in PHP and vice versa. I also wouldn't recommend shortcodes for things like connecting to the database. There is such a thing as being too granular
    – Tom J Nowell
    Commented Aug 6, 2017 at 0:48

1 Answer 1

0

You don't, alert is a javascript function that runs in the browser. PHP runs on the server, the two are different languages that run in different places.

Instead, consider using script tags:

return '<script>window.alert("Connected to the database successfully.")</script>';`

console.log("") may be a more appropriate method than alert("") for debugging

I would also note, that shortcodes embed content, and should represent something. Your plugin appears to create graphs, therefore I would assume your shortcode should be something such as [vivek_graph]. Inside that shortcode you can connect to the database, rather than requiring a connect to db shortcode happen before every use

2
  • Thank you Tom! Works perfectly embedding <Script> tags. Super. I also tried using return '<script>console.log("Connected to the database successfully.")</script>'; But that does not seem to be doing anything? I am definitely missing something. But what is the question.
    – Vivek
    Commented Aug 6, 2017 at 1:05
  • It prints a message to your JS console log, open the browser dev tools to see it. This way you can see a lot of messages without having to constantly click ok. Don't forget if it answers your question you can mark it as the accepted answer!
    – Tom J Nowell
    Commented Aug 6, 2017 at 1:14

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.