6. 1 サイトマップ プラグイン全体構成

 自作プラグイン“統合型サイトマップ”の全体構成です。

コード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 共通サブルーチン
require_once 'hk-sitemap_common.php';
$hk_map_com = new Hk_Sitemap_Common();

// 管理画面
if ( is_admin() ) {
    require_once 'hk-sitemap_admin.php';
    $hk_map_admin = new Hk_Sitemap_Admin();

    // 設定メニューの追加
    function hk_sitemap_add_admin_menu() {
        global $hk_map_admin;
        add_options_page( '統合型サイトマップ設定', '統合型サイトマップ', 'manage_options', 'hk_sitemap', array( $hk_map_admin, 'admin_main' ) );
        return;
    }
    add_action( 'admin_menu', 'hk_sitemap_add_admin_menu' );
}

// 閲覧画面
if ( ! is_admin() ) {
    require_once 'hk-sitemap_view.php';
    $hk_sitemap = new Hk_Sitemap();

    // セットアップ(閲覧用)
    function hk_sitemap_setup() {
        global $hk_sitemap;
        $hk_sitemap->setup();
        return;
    }
    add_action( 'wp_loaded', 'hk_sitemap_setup' );

    // 投稿一覧表示制限
    function hk_sitemap_limit_post_list( $wp_query ) {
        global $hk_sitemap;
        $hk_sitemap->limit_post_list( $wp_query );
        return;
    }
    add_action( 'pre_get_posts', 'hk_sitemap_limit_post_list' );

    // ローカルナビ(パンくず)
    function hk_local_navi() {
        global $hk_sitemap;
        return $hk_sitemap->local_navi();
    }

    // 投稿ナビゲーション 前後表示
    function hk_post_navi() {
        global $hk_sitemap;
        return $hk_sitemap->post_navi();
    }

    // リダイレクト 404
    function hk_sitemap_template_redirect() {
        global $hk_sitemap;
        $hk_sitemap->template_redirect();
        return;
    }
    add_action( 'template_redirect', 'hk_sitemap_template_redirect' );
}

// ショートコード実行 サイトマップ表示
function hk_sitemap_display_sitemap() {
    global $hk_sitemap;
    return $hk_sitemap->display_sitemap();
}
add_shortcode( 'hk-sitemap', 'hk_sitemap_display_sitemap' );

// ウィジェット データ連係型メニュー
class Hk_Widget_My_Menu extends WP_Widget {
    // 登録
    function __construct() {
        $widget_ops = array( 'description' => 'サイトマップデータ連係型メニュー' );
        parent::__construct( 'hk_widget_my_menu', 'データ連係型メニュー', $widget_ops );
    }

    // マイメニュー表示
    function widget( $args, $instance ) {
        extract( $args ); 
        $title = '';
        if ( isset( $instance['title'] ) )
            $title = $instance['title'];
        if ( $title == '' )
            $title = 'メニュー';
        echo $before_widget."\n";
        echo $before_title.$title.$after_title."\n";
        global $hk_sitemap;
        echo $hk_sitemap->my_menu();
        echo $after_widget."\n";
    }

    // 設定更新
    function update( $new_instance, $old_instance ) {
        return $new_instance;
    }

    // 設定画面
    function form( $instance ) {
        if ( isset( $instance['title'] ) )
            $title = htmlspecialchars( $instance['title'] );
        else
            $title = '';
        echo "<p>\n"; 
        echo '  <label for="'.$this->get_field_id('title').'">タイトル:'."</label>\n";
        echo '  <input class="widefat" id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$title.'" />'."\n";
        echo "</p>\n";
    }
}

// ウィジェット 登録
function hk_register_widget_my_menu() {
    register_widget( 'Hk_Widget_My_Menu' );
}
add_action( 'widgets_init', 'hk_register_widget_my_menu' );

利用しているフック

wp-admin/includes/menu.php
    do_action( 'admin_menu', '' );
wp-settings.php
    do_action( 'wp_loaded' );
wp-includes/query.php
function get_posts()
    do_action_ref_array( 'pre_get_posts', array( &$this ) );
wp-includes/template-loader.php
    do_action( 'template_redirect' );
wp-includes/default-widgets.php
function wp_widgets_init() {
    do_action( 'widgets_init' );

説明

 共通サブルーチンは、管理画面と閲覧画面で共通に利用するサブルーチンです。

 このプログラムをお使いになる場合は、お使いになる方の自己責任でお願いします。

更新日:2016/01/22
掲載日:2016/01/22