【问题标题】:Wordpress Dashboard widgetWordpress 仪表板小部件
【发布时间】:2014-04-02 13:05:41
【问题描述】:

我们网站的前任开发人员通过在 /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>

【问题讨论】:

    标签: php wordpress-theming wordpress dashboard


    【解决方案1】:

    这就是Plugins 的用途。只需创建一个 PHP 文件,放置一个基本的插件标头并将所有自定义代码移至其中:

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

    上传到wp-content/plugins文件夹并在单个站点上激活或在多站点上网络激活。

    您可以使用页面Dashboard Widgets API 中的 Codex 示例代码开始创建一个非常简单的插件。此外,您还可以在WordPress Answers 找到许多仪表板小部件示例。

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 2018-12-14
      • 2012-01-29
      • 1970-01-01
      • 2011-03-24
      • 2014-04-01
      • 2013-11-05
      • 2011-02-18
      • 1970-01-01
      相关资源
      最近更新 更多