Call php function in content in custom page in wp-admin

I editing custom page in wp-admin. Currently in editor i see that

[vc_column_text]Titile1[/vc_column_text]

After each [/vc_column_text] i need to display some info from database. How i can call php function with some parameters in wp-admin editor ?
Like this or so:

[vc_column_text]Titile[/vc_column_text]
[getInforFromDatabase('Titile1')]

Related posts

Leave a Reply

1 comment

  1. This will take quite some doing. You just may not directly call a PHP function from within the WP-Admin Editor but you can create a simple, rudimentary Plugin to do the heavy-lifting for you. And then expose a short-code that you can use from within the WP Admin Editor.

    To do this, first create a a Directory in your wp-content/plugins directory. For the purposes of demonstration, we will call this directory: dbInfoSifter but the name is completely up to you. In the end, the path to this directory would be: wp-content/plugins/dbInfoSifter

    Now inside of this Folder (dbInfoSifter), create a PHP File with the same name again so that you have: wp-content/plugins/dbInfoSifter/dbInfoSifter.php. Now add the following Code inside the dbInfoSifter.php File:

        <?php
            /*
            Plugin Name: DB Info Sifter
            Plugin URI: your-domain.tld
            Description: Simple Plugin to sift data From Database.
            Author: Your Name
            Author URI: http://your-domain.tld
            Version: 1.0.0
             */
    
            // THE COMMENTED LINES ABOVE INFORMS WORDPRESS THAT THIS IS A PLUGIN.
            // SO IT KNOWS TO ADD IT TO THE PLUGINS LIST...
            // WE SHALL REVISIT THIS SOONER THAN LATER...
    
    
            // IN THIS FILE YOU SHOULD PUT ALL THE LOGIC
            // FOR GETTING DATA FROM THE DATABASE OR DOING ANYTHING AT ALL
            // HOWEVER, THE MOST IMPORTANT THING IS TO EXPOSE THE SHORT-CODE 
            // SO THAT WE CAN USE IT INSIDE THE WP-ADMIN EDITOR.
            // WE CAN DO THAT WITH THE FOLLOWING LINES OF CODE...
            add_shortcode('getInfoFromDatabase', 'dbsGetInfoFromDatabase');
    
    
            // THE LINE ABOVE EXPOSES THE SHORT-CODE SO THAT YOU CAN CALL IT
            // FROM YOUR WP-ADMIN EDITOR... THE ARGUMENTS ARE SPECIFIC:
            // THE 1ST ARGUMENT IS THE NAME OF THE SHORT-CODE
            // THE 2ND IS THE NAME OF THE FUNCTION TO RUN WHEN THIS SHORT-CODE IS CALLED.
            // SO NOW, WE WRITE OUT THE FUNCTION ITSELF:
            //THE $atts PARAM IS AN ARRAY OF PARAMETERS PASSED TO THE SHORT-CODE.
            function dbsGetInfoFromDatabase($atts){
                extract( shortcode_atts( array(
                    'title1'     => "default_value", /*<= SET DEFAULT VALUE*/
                    'param2'     => "default_value", /*<= SET DEFAULT VALUE*/
                ), $atts ));
                // WITH THE extract FUNCTION YOU CAN NOW USE title1 AND param2
                // AS NORMAL VARIABLES IN YOUR PROGRAMS LIKE $title1, $param2.
                // SO THIS IS WHERE YOU BUILD YOUR LOGIC TO GET DATA FROM THE 
                // DATABASE & YOU CAN USE THE PARAMETERS TOO...
                // YOU ARE NOT LIMITED TO THE NUMBER OF PARAMETERS TO USE
                // AS WELL AS THE NAME OF THE PARAMETERS... 
                // THOSE ARE COMPLETELY UP TO YOU...
    
                /* AND SO; YOUR LOGIC CONTINUES...*/   
    
    
                // IT IS HIGHLY IMPORTANT THAT THIS FUNCTION RETURNS A VALUE.
                // MOSTLY LIKELY, THE TYPE WOULD BE A STRING
                // (ESPECIALLY IF YOU WANT TO DISPLAY IT AUTOMATICALLY) 
                return $stringValueResultingFromDBTransactions;
            }
    

    That’s all… nothing really special to it… But you can also have other Functions within this File that does something, anyways. However, the most important parts of this File (in your case) are: 1.) The function: dbsGetInfoFromDatabase($args) and 2.) The Comments at the Top of the File.

    Now, inside the WP-Admin Editor; you can just simply reference this function using the short-code we created like so:

              // WP-ADMIN EDITION (BACKEND)
              [vc_column_text]Titile[/vc_column_text]
              [getInfoFromDatabase title1='Titile1']  //<== CALL YOUR SHORT-CODE 
    

    Alternatively, You can do it like so:

              //WP-ADMIN EDITION (BACKEND)
              [vc_column_text]Titile[/vc_column_text]
              [getInfoFromDatabase title1='Titile1'][/getInfoFromDatabase]         
    

    Both will achieve the same Result, but the First one seems more concise (to me). Take your pick.

    Finally, you need to activate the Plugin at the Backend for this to work. So; navigate to your Plugins Section (at the Backend of WordPress). You will notice a new Plugin called DB Info Sifter. Simply activate it and you are finally ALL DONE. Your short-code would now work as if you actually called a function and passed it the $title1 parameter.

    I hope this helps you a little bit and gives you a head-start…
    Good-Luck to you, my Friend…