WP PLUGIN
・選択カスタムフィールドをオートコンプリートに変更
This commit is contained in:
@@ -7,25 +7,29 @@ if ( ! trait_exists( 'CUSTOMFIELD_SELECT' ) ) {
|
||||
|
||||
trait CUSTOMFIELD_SELECT {
|
||||
|
||||
public static function set_all_customfield_select( $name_meta_key, $meta_key ) {
|
||||
$selects = self::get_all_custom_fields();
|
||||
echo '<select class="custom_field_key_select" name="' . esc_attr( $name_meta_key ) . '" required>';
|
||||
foreach ( $selects as $select ) {
|
||||
if ( $select->meta_key == $meta_key ) {
|
||||
$selected = ' selected';
|
||||
} else {
|
||||
$selected = '';
|
||||
}
|
||||
echo '<option value="' . esc_attr( $select->meta_key ) . '"' . esc_attr( $selected ) . '>' . esc_html( $select->meta_key ) . '</option>';
|
||||
}
|
||||
echo '</select>';
|
||||
public function set_custom_field_ajax() {
|
||||
$handle = 'custom_field_ajax';
|
||||
wp_register_script( $handle, APOP_PLUGIN_URL . 'js/custom_field.js', [ 'jquery' ], '', true );
|
||||
$localize = [
|
||||
'ajax_url' => admin_url( 'admin-ajax.php' ),
|
||||
'action' => 'set_custom_field',
|
||||
];
|
||||
wp_localize_script( $handle, 'localize', $localize );
|
||||
wp_enqueue_script( $handle );
|
||||
}
|
||||
|
||||
private static function get_all_custom_fields() {
|
||||
public function set_custom_field() {
|
||||
$param = filter_input( INPUT_GET, 'param', FILTER_SANITIZE_STRING );
|
||||
echo json_encode( $this->get_custom_fields_by_param( $param ) );
|
||||
die();
|
||||
}
|
||||
|
||||
private function get_custom_fields_by_param( $param ) {
|
||||
global $wpdb;
|
||||
$stmnt = "SELECT DISTINCT meta_key AS value, meta_key as label FROM $wpdb->postmeta WHERE meta_key LIKE %s AND meta_key NOT LIKE %s";
|
||||
|
||||
//アンダーバーで始まるシステム系のパラメータは除外する
|
||||
return $wpdb->get_results( 'SELECT DISTINCT meta_key FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key NOT LIKE ' . "'\_%' ORDER BY meta_key ASC" );
|
||||
return $wpdb->get_results( $wpdb->prepare( $stmnt, $wpdb->esc_like( $param ) . '%', $wpdb->esc_like( '_%' ) ) );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user