2014-03-01 45 views
0

我們的網站上開發商在/wp-admin/includes/dashboard.phpWordPress的Dashboard widget工具

這個小工具創建核心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

這是Plugins的用途。只需創建一個PHP文件,把基本的插件頭和移動所有的自定義代碼到它:

<?php 
/** 
* Plugin Name: My dashboard widget 
*/ 

上傳到wp-content/plugins文件夾,並激活它在一個單一的網站或網絡激活多站點。

您可以使用頁面Dashboard Widgets API中的Codex示例代碼開始創建一個非常簡單的插件。此外,您還可以在WordPress Answers找到許多Dashboard小部件的示例。