我们网站的前任开发人员通过在/wp-admin/includes/dashboard.php中的核心wordpress文件中创建小部件而采用了非常奇怪的方法
这个小部件“热门句子”是从仪表板询问句子和URL并将其发布在主页的标题中。
如何创建此仪表板小部件,但不要将其写入WordPress核心文件并与Wordpress 3.8.1和Wordpress Multisite兼容,以便某些网站也可以拥有自己的“顶级句子”?
<div id="dashboard-widgets" class="metabox-holder<?php echo $columns_css; ?>">
<div id="top_sentence" class="postbox" style='margin-left: 5px; width: 98%;'>
<?php
require_wp_db();
global $wpdb;
$topSentenceTitle = $wpdb->get_var( $wpdb->prepare( "SELECT settings_value FROM wp_settings WHERE settings_name like 'top sentence title';" ) );
$topSentenceUrl = $wpdb->get_var( $wpdb->prepare( "SELECT settings_value FROM wp_settings WHERE settings_name like 'top sentence url';" ) );
$topSentenceResult = "";
if ( $_SESSION['top-sentence-updated'] == 'true' )
{
$_SESSION['top-sentence-updated'] = '';
$topSentenceResult = 'Updated';
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#top-sentence-result').fadeIn(1000).delay(1000).fadeOut(1000);
});
</script>
<?php
}
if ( is_blog_admin() && current_user_can('edit_posts') )
{
?>
<h3 class='hndle'><span>Top sentence</span></h3>
<div class="inside">
<form action='action.php' method='post' style='margin:0;padding:0;'>
//action.php doesn't exist in WordPress 3.8.1
<div style='float: left; width: 100%; margin-bottom: 3px;'>
<h4 id="top-sentence-title" style='float: left;font-size: 12px; font-weight: normal; padding-top: 5px; text-align: right; width: 5.5em;'>
<label for="top-sentence-title-input" style='font-family: "Lucida Grande",Verdana,Arial;margin-right: 10px;vertical-align: middle;cursor: pointer;'>Sentence:</label>
</h4>
<div style='margin: 0 0 1em 5em;'>
<input style='float:right;margin:0;width:99%;' id="top-sentence-title-input" type="text" value="<?= $topSentenceTitle ?>" autocomplete="off" name="top-sentence">
</div>
<div style='clear: both;margin-bottom:5px;'></div>
<h4 id="top-sentence-url" style='float: left;font-size: 12px; font-weight: normal; padding-top: 5px; text-align: right; width: 5.5em;'>
<label for="top-sentence-url-input" style='font-family: "Lucida Grande",Verdana,Arial;margin-right: 10px;vertical-align: middle;cursor: pointer;'>URL:</label>
</h4>
<div style='margin: 0 0 1em 5em;'>
<input style='float:right;margin:0;width:99%;' id="top-sentence-url-input" type="text" value="<?= $topSentenceUrl ?>" autocomplete="off" name="top-sentence-url">
</div>
</div>
<div id='top-sentence-result' style="display: none;margin-left:6em;float: left;margin-top: 5px;color: red;"><span><?= $topSentenceResult ?></span></div>
<div id="publishing-action" style='float:right; width: 100px;'>
<input id="publish_sentence" class="button-primary" type="submit" value="Publish" tabindex="5" accesskey="p" name="do_publish_sentence">
</div>
</form>
<div style='clear: both;'></div>
</div>
<?php
}
?>
</div>
答案 0 :(得分:0)
这是Plugins的用途。只需创建一个PHP文件,放入一个基本的插件标题并将所有自定义代码移动到它:
<?php
/**
* Plugin Name: My dashboard widget
*/
上传到wp-content/plugins
文件夹并在单个网站上激活它,或在Multisite上激活网络。
您可以使用页面Dashboard Widgets API中的Codex示例代码开始创建一个非常简单的插件。此外,您还可以在WordPress Answers找到许多仪表板小部件示例。