WP_Customize_Nav_Menus::ajax_search_available_items()publicWP 4.3.0

Ajax handler for searching available menu items.

Method of the class: WP_Customize_Nav_Menus{}

No Hooks.

Return

null. Nothing (null).

Usage

$WP_Customize_Nav_Menus = new WP_Customize_Nav_Menus();
$WP_Customize_Nav_Menus->ajax_search_available_items();

Changelog

Since 4.3.0 Introduced.

WP_Customize_Nav_Menus::ajax_search_available_items() code WP 6.6.2

public function ajax_search_available_items() {
	check_ajax_referer( 'customize-menus', 'customize-menus-nonce' );

	if ( ! current_user_can( 'edit_theme_options' ) ) {
		wp_die( -1 );
	}

	if ( empty( $_POST['search'] ) ) {
		wp_send_json_error( 'nav_menus_missing_search_parameter' );
	}

	$p = isset( $_POST['page'] ) ? absint( $_POST['page'] ) : 0;
	if ( $p < 1 ) {
		$p = 1;
	}

	$s     = sanitize_text_field( wp_unslash( $_POST['search'] ) );
	$items = $this->search_available_items_query(
		array(
			'pagenum' => $p,
			's'       => $s,
		)
	);

	if ( empty( $items ) ) {
		wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
	} else {
		wp_send_json_success( array( 'items' => $items ) );
	}
}