WordPress 外掛程式樣本之一

來源:互聯網
上載者:User

    這個 WP 外掛程式,除過能夠在外掛程式管理面板管理外(可以被開啟和禁用),還能夠在“設定”菜單下對外掛程式進行配置,使外掛程式的功能可以得到擴充。本外掛程式,可以實現對部落格文章中的任一字元串進行替換。

    以下代碼中,why100000_keyword 和 why100000_replace 作為選項文本域標識變數,其值被 option.php 取到並儲存到資料庫的 wp_options 表中。

    get_option("why100000_keyword") 和 get_option("why100000_replace") 語句用於從 wp_options 表中取出資料。

    <?php
    /*
      Plugin Name: MyCopyright
      Plugin URI: http://www.why100000.com
      Version: 0.1
      Author: 網眼(張慶-陝西西安-開開工作室-http://www.myopenit.cn)
      Author URI: http://blog.why100000.com
      Description: 把字串“&lt;!--mycopyrigth--&gt;”替換為著作權資訊:show copyright once there are letters match “&lt;!--mycopyrigth--&gt;”.
      you should config your copyright information in this file,with this verison.
    */

    //加到“設定”主菜單下
    add_action('admin_menu', 'why100000_add_options_page');
    function why100000_add_options_page()
    {
      add_options_page('MyCopyright', 'MyCopyright-Config', 8, 'mycopyright.php', 'why100000_myCopyright_mainpage');
    }
    function why100000_myCopyright_mainpage()
    {
    ?>
      <form name="formamt" method="post" action="options.php">
        <?php wp_nonce_field('update-options') ?>
        <div class="wrap">
          MyCopyright is active.<br><br>
          <h2>MyCopyright Config (著作權外掛程式配置)</h2>

          <div id="msgall">
            <fieldset class="options">
              <legend>keyword(原字串):</legend>
              <p>
              <textarea style="width: 80%;" rows="5" cols="40" name="why100000_keyword"><?php echo get_option("why100000_keyword");?></textarea>
              </p>
            </fieldset>

            <fieldset class="options">
              <legend>replacement(代替的字串):</legend>
              <p>
              <textarea style="width: 80%;" rows="5" cols="40" name="why100000_replace"><?php echo get_option("why100000_replace");?></textarea>
              </p>
            </fieldset>
          </div>

          <p class="submit">
            <input type="submit" value="<?php _e('Update Options &raquo;') ?>" name="update_message"/>
          </p>
          <input type="hidden" name="action" value="update">
          <input type="hidden" name="page_options" value="why100000_keyword,why100000_replace">
          </div>
          </form>
    <?php
    }

    add_action('activate_mycopyright/mycopyright.php', 'why100000_copyright_install');
    //外掛程式第一次被“啟用”時執行,作為初始值儲存到表wp_options中
    function why100000_copyright_install()
    {
      add_option("why100000_keyword", "<!--mycopyright-->");
      add_option("why100000_replace", "我的版本");
    }

    //WP執行the_content函數時,調用外掛程式自訂函數why100000_showcopyright,對文章內容進行過濾
    add_filter('the_content', 'why100000_showcopyright');
    function why100000_showcopyright($content)
    {
      $search = get_option("why100000_keyword");  //"<!--mycopyright-->";
      $replace= get_option("why100000_replace");  //代替的字串;
      $content= str_replace($search, $replace, $content);
      return $content;
    }
    ?>

    作者:張慶(網眼) 2010-4-4
    來自“網眼視界”:http://blog.why100000.com
    “十萬個為什麼”電腦學習網:http://www.why100000.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.