寄稿しました。詳しくは上記を(ㆁᴗㆁ✿)
作成
# vi /var/www/html/wp/wp-content/plugins/sgwidget/sgwidget.php
<?php
/**
* @package SgAdWidget
* @version 0.1.1
*/
/*
Plugin Name: SgAdWidget
Plugin URI: http://example.com/plugins/sgwidget/
Description: Plugin development tutorial for making plugin by using widget
Author: Kanehiro Yuu
Version: 0.1.1
Author URI: https://sys-guard.com/
*/
//実行するよ!
$SgAdWidgetObj = new Sg_Ad_Widget();
class Sg_Ad_Widget extends WP_Widget
{
private $widget_ops;
private $control_ops;
public $id = "";
public $name = "";
public $title = "";
public $text = "";
private $instance;
private $new_instance;
private $old_instance;
private $args;
public function __construct()
{
//ウィジェットスペースのアクション実行
add_action( 'widgets_init', array($this, 'sg_add_widget') );
//ウィジェットオプション
$this->widget_ops = array('description' => 'Google Adsense');
$this->control_ops = array('width' => 200, 'height' => 350);
//ウィジェットを登録
parent::__construct(
ad_widget, // Base ID
'Adsense', //name
$this->widget_ops,
$this->control_ops
);
}
//ウィジェット、ウィジェットスペースの設定と登録
public function sg_add_widget()
{
//ウィジェットをWordPressに登録する ※PHP5.3以上
register_widget( 'Sg_Ad_Widget' );
//ウィジェットスペースの設定
register_sidebar( array(
'name' => '広告スペース',
'id' => 'ad_sidebar',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => ''
) );
}
// ウィジェット 入力フォーム出力 ==========================================
/**
* バックエンドのウィジェットフォーム
*
* @see WP_Widget::form()
*
* @param array $instance データベースからの前回保存された値
*/
public function form( $instance )
{
//タイトルエリア
$this->instance = $instance;
if(isset($this->instance['title']) == true ){
$this->title = $this->instance['title'];
}else{
$this->title = '';
}
$this->id = parent::get_field_id('title');
$this->name = parent::get_field_name('title');
echo '<p>';
echo 'タイトル:<br/>';
printf(
'<input rows="16" cols="45" type="text" id="%s" name="%s" value="%s">',
$this->id,
$this->name,
esc_attr($this->title)
);
echo '</p>';
//テキストエリア
if(isset($this->instance['text']) == true ){
$this->text = $this->instance['text'];
}else{
$this->text = '';
}
$this->id = parent::get_field_id('text');
$this->name = parent::get_field_name('text');
echo '<p>';
echo 'スクリプトタグ:<br/>';
printf(
'<textarea rows="16" cols="45" id="%s" name="%s" >%s</textarea>',
$this->id,
$this->name,
$this->text
);
echo '</p>';
}
// ウィジェット 入力フォーム出力 ここまで ==========================================
/**
* ウィジェットフォームの値を保存用にサニタイズ
*
* @see WP_Widget::update()
*
* @param array $new_instance 保存用に送信された値
* @param array $old_instance データベースからの以前保存された値
*
* @return array 保存される更新された安全な値
*/
public function update( $new_instance, $old_instance ) {
$this->new_instance = $new_instance;
return $this->new_instance;
}
/**
* ウィジェットのフロントエンド表示
*
* @see WP_Widget::widget()
*
* @param array $args ウィジェットの引数
* @param array $instance データベースの保存値
*/
public function widget($args, $instance)
{
$this->args = $args;
$this->instance = $instance;
echo $this->args['before_widget'];
echo $this->args['before_title'];
echo esc_html( $this->instance['title'] );
echo $this->args['after_title'];
echo $this->instance['text'];
echo $this->args['after_widget'];
}
}//Sg_Ad_Widget
任意の場所に貼り付ける
# vi /var/www/html/wp/wp-content/themes/twentyseventeen/sidebar.php
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
↓変更
if ( ! is_active_sidebar( 'ad_sidebar' ) ) {
return;
}
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar('ad_sidebar'); ?>
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside><!-- #secondary -->
次はDB関連のプラグインで一旦終わりにしよう。



