searchwp\sources
4.0.0以降
SearchWP によってインデックス作成および検索されるコンテンツを制御します。\SearchWP\Source も参照してください。
パラメータ
| タイプ | パラメータ | デフォルト | 提供開始 |
|---|---|---|---|
| 配列 | $sources |
コアソース | 4.0.0 |
例
All hooks should be added to your custom SearchWP Customizations Plugin.
SearchWP にカスタムソースを追加
このサンプルソースは、カスタムデータベーステーブルに基づいています。この特定のフックの使用については、46〜50行目を参照してください。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Adds custom SearchWP\Source for MyCustomSearchWPSource which uses a custom database table. | |
| */ | |
| add_action( 'plugins_loaded', function() { | |
| class MyCustomSearchWPSource extends \SearchWP\Source { | |
| // Unique name for this Source. | |
| protected $name = 'mycustomsource'; | |
| // Database column name that provides a unique ID for each entry of the Source. | |
| protected $db_id_column = 'id'; | |
| function __construct() { | |
| global $wpdb; | |
| // The database table storing entries. | |
| $this->db_table = $wpdb->get_blog_prefix() . 'my_db_table'; | |
| // Labels for this Source. | |
| $this->labels = [ | |
| 'plural' => 'My Custom Source Entries', | |
| 'singular' => 'My Custom Source Entry', | |
| ]; | |
| // Attributes for this Source. Array of attributes, can be expanded. | |
| $this->attributes = [ [ | |
| 'name' => 'content', | |
| 'label' => 'Entry Content', | |
| 'default' => \SearchWP\Utils::get_min_engine_weight(), | |
| 'data' => function( $entry_id ) { | |
| // Note: MyWidget is an pseudo-class meant to represent | |
| // a class you're using that represents something like | |
| // WP_Post in that it models the data you are working with. | |
| // Long story short: this method should return the content | |
| // you want to index and make searchable in SearchWP. | |
| return MyWidget::get_content( $entry_id ); | |
| }, | |
| ], ]; | |
| } | |
| // Returns a native object e.g. formatted in a way you expect. | |
| public function entry( \SearchWP\Entry $entry, $query = false ) { | |
| // Note: MyWidget is a pseudo-class meant to represent a class | |
| // you're using that represents something like WP_Post in that it | |
| // models the data you're working with. | |
| // Long story short: this method should return whatever it is you | |
| // expect when working 'natively' with your data e.g. for WordPress | |
| // pages a WP_Post is returned. Note that $entry->get_id() returns | |
| // the value in the $db_id_column for this Source. | |
| return new MyWidget( $entry->get_id() ); | |
| } | |
| } | |
| // Append this Source to the list of available Sources in SearchWP. | |
| add_filter( 'searchwp\sources', function( $sources ) { | |
| $sources[] = new MyCustomSearchWPSource(); | |
| return $sources; | |
| } ); | |
| } ); |

