Wordpress 插件示例之一

本插件允许用户通过WordPress的设置菜单配置插件功能,实现对博客文章中的特定字符串进行替换,以显示版权信息。用户可以自定义原始字符串和替代字符串,并通过插件在文章中自动应用这些更改。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    这个 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值