使用div类创建Wordpress插件的最佳方法

时间:2017-12-06 13:01:32

标签: php wordpress

目前我正在为自己的网站开发一个新的Wordpress插件。我想要实现的目标是从外部api调用获取信息,将其保存到数据库中,然后在Wordpress的表中显示这些数据。

这是我目前的代码:

<?php
/*
Plugin Name: API Plugin
Plugin URI:
Description: A first plugin
Author: 
Version: 1.0
Author URI:
*/

add_action('admin_menu', 'myfirstplugin_admin_actions');

function myfirstplugin_admin_actions(){
    add_options_page('MyFirstPlugin', 'MyFirstPlugin', 'manage_options', __FILE__, 'myfirstplugin_admin');
}

function myfirstplugin_admin(){


?>
<div class="wrap">
<h4>A more interesting hello world</h4>
<table class="widefat">
<thead>
<tr>
<th> POST TILE </th>
<th> POST ID </th>
</tr>
</thead>
<tfoot>
<tr>
<th> POST TILE </th>
<th> POST ID </th>
</tr>
</tfoot>
<tbody>
<?php
    global $wpdb;

    $mytestdrafts = $wpdb->get_results(
        "
        SELECT ID, post_title
        FROM $wpdb->posts
        WHERE post_status = 'inherit'
        "
        );
?>
<?php
    foreach ($mytestdrafts as $mytestdraft) {
?>
    <tr>
<?php
    echo "<td>".$mytestdraft->post_title."</td>";
    echo "<td>".$mytestdraft->ID."</td>";
?>
    </tr>
<?php   
    }
?>
</tbody>
</div>
<?php
}

add_shortcode( 'test', 'myfirstplugin_admin' );

?>

这非常复杂,将来难以维护。当此代码包含更多代码行时。所以我的问题是哪些替代方案在Wordpress中实现了同样的目标?

我的目标总结如下:

  - get data from api and save this in Wordpress database
  - read data from Wordpress database
  - backend php function to read data from database and put it in div class
  - show it in the frontend

0 个答案:

没有答案