2013年01月

Wordpress laptop
Wordpress laptop / Titanas


組合の方から(コンピュータ利用促進協同組合)からフラッシュで作成されたバナーをいただきました。このフラッシュは読み込む度に画像を差し替えてくれるというステキ仕様で、ただの画像ファイルとは段違いにカッコよいのだ。(試しにリロードしてみてください。画面右側のCCPの画像が変るのがわかりますか?)

そこで、早速、ウェブサイトに表示させてみようと思った...、のだが既存のウィジェットではフラッシュファイルの表示ではできない。画像を貼るのはJetpackのウィジェットで可能なのですが、フラッシュファイルを表示させる方法は標準ではないようです。

そこで再利用の事も考慮して、SWFファイルを表示させるウィジェットを追加する事にしました。

ウィジェットを追加するには、テーマファイルの中のfunctions.php(うちの環境では、wp-content/themes/cordobo-green-park-2/functions.php)に以下のコードを追加するだけでOKなようです。

でも、このやり方だと、テーマが更新された時に消えてしまうかもしれないので、あんまり良い方法じゃないかもしれません。

[sourcecode language="php" title="functions.php"]
<?php
/*
ウィジェットにSWFファイルを置く
*/
class SwfWidgetItem extends WP_Widget {
function SwfWidgetItem() {
parent::WP_Widget(false, $name = 'SWF貼り付けウィジェット');
}
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$url = apply_filters( 'widget_url', $instance['url'] );
$width = apply_filters( 'widget_width', $instance['width'] );
$height = apply_filters( 'widget_height', $instance['height'] );
?>

<li <?php echo 'id="swfWidget"'; ?> class="widget swfWidget" >
<ul>
<li>
<div class="sb-title widgettitle"><?php echo $title ?></div>
</li>
<li>
<object data="<?php if ( $url ) echo $url ?>" width="<?php if ( $width ) echo $width ?>" height="<?php if ( $height ) echo $height ?>" type="application/x-shockwave-flash">
<param name="movie" value="<?php if ( $url ) echo $url ?>" />
<param name="FlashVars" value="value" />
</object>
</li>
</ul>
</li>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['url'] = strip_tags($new_instance['url']);
$instance['width'] = strip_tags($new_instance['width']);
$instance['height'] = strip_tags($new_instance['height']);
return $instance;
}
function form($instance) {
$title = esc_attr($instance['title']);
$url = esc_attr($instance['url']);
$width = esc_attr($instance['width']);
$height = esc_attr($instance['height']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">
<?php _e('名称:'); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />

<label for="<?php echo $this->get_field_id('url'); ?>">
<?php _e('SWFファイルのURL:'); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" type="text" value="<?php echo $url; ?>" />

<label for="<?php echo $this->get_field_id('width'); ?>">
<?php _e('幅:'); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" type="text" value="<?php echo $width; ?>" />

<label for="<?php echo $this->get_field_id('height'); ?>">
<?php _e('高さ:'); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id('height'); ?>" name="<?php echo $this->get_field_name('height'); ?>" type="text" value="<?php echo $height; ?>" />
</p>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("SwfWidgetItem");'));

?>
[/sourcecode]

my first piece of hand-written HTML in 1997!
my first piece of hand-written HTML in 1997! / laihiu


ワードプレスでソースコードを表示させるには、「SyntaxHighlighter Evolved」を使うと便利です。(注 本来は大括弧の前後のスペースは削除しないと動作しません)

[text]
[ text ]
対応言語
html,actionscript3,bash,coldfusion,cpp,csharp,css,delphi,
erlang,fsharp,diff,groovy,javascript,java,javafx,
matlab (keywords only),objc,perl,php,text,powershell,
python,r,ruby,scala,sql,vb,xml
[ /text ]
[/text]

対応している言語もいろいろある。

試しに先程のテキストを実際に表示させるとこうなる。

[text]
対応言語
html,actionscript3,bash,coldfusion,cpp,csharp,css,delphi,
erlang,fsharp,diff,groovy,javascript,java,javafx,
matlab (keywords only),objc,perl,php,text,powershell,
python,r,ruby,scala,sql,vb,xml
[/text]

SQLの場合

[sql]
select * from table1 where id = 1;
[/sql]

↑このページのトップヘ