hiromasa.zone : o)

2006/2/4 Saturday 投稿時の月齢:6.3  月名:七日月  潮汐:小潮 Moon:6.3[七日月]今日の心技体 : 好調期好調期低調期

投稿内容を改変する WordPress プラグインのひな形 このエントリをはてなブックマークに追加このエントリをdel.icio.usに追加

日記 - ひろまさ @ 21:58

投稿内容を改変する WordPress プラグインのひな形です。

  1.  <?php
  2.  /*
  3.  Plugin Name:
  4.  Plugin URI: http://zone.maple4ever.net/blog/
  5.  Description:
  6.  Author: hiromasa
  7.  Version: 1.0
  8.  Author URI: http://zone.maple4ever.net/blog/
  9.  */
  10.  
  11.  $wppostfix = new WpPostFix();
  12.  
  13.  add_action('save_post', array($wppostfix, 'postfix'), 99);
  14.  add_action('edit_post', array($wppostfix, 'postfix'), 99);
  15.  add_action('publish_post', array($wppostfix, 'postfix'), 99);
  16.  
  17.  class WpPostFix {
  18.     
  19.      function postfix($postID) {
  20.         
  21.          global $wpdb;
  22.         
  23.          // DB から content を post_id から検索して持ってきて
  24.          $content = $wpdb->get_var("SELECT post_content FROM {$wpdb->posts} WHERE ID = '{$postID}' LIMIT 1");
  25.          // 適当に修正して
  26.          $content = $this->fixcontent($content);
  27.          // DB をなおした内容でアップデートする
  28.          $wpdb->query("UPDATE {$wpdb->posts} SET post_content = '{$content}' WHERE ID = '{$postID}'");
  29.         
  30.          // 次の人のために post_id 戻しておく
  31.          return $postID;
  32.         
  33.      }
  34.     
  35.      function fixcontent($content) {
  36.         
  37.          // 正規表現とかで置換する
  38.          $before = 'hogehoge';
  39.          $after  = 'mogemoge';
  40.          $content = preg_replace("/$before/i", $after, $content);
  41.          // 戻す
  42.          return $content;
  43.         
  44.      }
  45.     
  46.  }
  47.  ?>

投稿時に、add_action を使ってデータベースの内容をアップデートしてしまうコードです。 上の例はエントリ中に、hogehoge があったら mogemoge に置換します。

フィルターではないので、実データかきかえちゃいます。使うときはお気をつけください。 🙂

No Comments


このサイトはコンテンツをフリーズしました。トラックバック・コメントは閉じられています。

新しいサイトは、

hiromasa.another :o)

です。 :-)

このサイトについて

このブログは引っ越しを行いコンテンツはフリーズしています。hiromasa.anotherへどうぞ。

Powerd By WordPress

We (Heart) WordPress

WordPressME Logo
WordPress Plugins

ブログ内検索

Todays Popular

WordPress Ring

はてなリング - WordPress -

情報

29 queries. 0.339 seconds.

このページの先頭へ