在wp-admin的自定义页面中调用内容中的php函数

时间:2016-05-25 17:45:03

标签: php wordpress custom-wordpress-pages

我在wp-admin中编辑自定义页面。目前在编辑器中我看到了

[vc_column_text]Titile1[/vc_column_text]

每个[/vc_column_text]后我需要显示数据库中的一些信息。我怎么能用wp-admin编辑器中的一些参数调用php函数? 像这样左右:

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

1 个答案:

答案 0 :(得分:2)

这需要做一些事情。你可能不会直接从WP-Admin编辑器中调用PHP函数,但你可以创建一个简单的基本插件来为你做繁重的工作。然后公开一个可以在WP Admin Editor中使用的短代码。

为此,首先在wp-content / plugins 目录中创建一个目录。出于演示的目的,我们将调用此目录: dbInfoSifter ,但名称完全取决于您。最后,此目录的路径为: wp-content / plugins / dbInfoSifter

现在在此文件夹( dbInfoSifter )内,再次创建一个具有相同名称的PHP文件,以便您拥有: wp-content / plugins / dbInfoSifter / dbInfoSifter.php 。现在在dbInfoSifter.php文件中添加以下代码:

    <?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;
        }

这就是......没有什么特别的东西......但是你也可以在这个文件中有其他功能,无论如何。但是,此文件的最重要部分(在您的情况下)是:1。)函数:dbsGetInfoFromDatabase($ args)和2.)文件顶部的注释。

现在,在WP-Admin编辑器中;你可以使用我们创建的短代码来简单地引用这个函数:

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

或者,你可以这样做:

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

两者都会达到相同的结果,但第一个看起来更简洁(对我来说)。随便挑选。

最后,您需要在后端激活插件才能使用。所以;导航到您的插件部分(在Wordpress的后端)。您会注意到一个名为 DB Info Sifter的新插件。只需激活它,您终于完成了所有操作。你的短代码现在就像你实际调用一个函数并传递$ title1参数一样。

我希望这会对你有所帮助,让你先行一步...... 祝你好运,我的朋友......