Skip to main content

Adding paginations & searching box to custom fields

Rendering all the pre-made data of a custom field at once on the screen in the backend can cause the post or page editor to look very confusing. Adding pagination to separate those values into pages, and also providing a search box to search any value right in the field will come in handy.

Here is an example.

An example of adding Paginations & Searching Box to Custom Fields

The fields include various images that you can choose from. Instead of showing all of them on this screen, I use this pagination and a search box to find a reasonable one easier.

Video version

Preparation

In this post, we will focus on pagination and we'll build a search box that allows users to look up any value in the field.

To get started, we need some tools:

1. Creating custom fields

I will create two fields. One is to show some image options to choose from. The other field allows adding text to search any image that is in the below field.

The custom fields that I created

Go to Meta Box, and add fields as usual.

Go to Meta Box, and add fields

The Search box should be a Text field that allows typing characters inside.

Choose a Text field for the Search box

Move to the Advanced tab, and add a class for this field since we will use JavaScript later to add the search feature to this field.

Add a class for the field

The next field is to provide some images. So I’ll add it as an Image Select field.

Insert an Image Select field

In the Choices box of this field’s settings, add some images.

In the Choices box of this field’s settings, add some images.

This is the URL of the image.

The URL of the image

This is the name of the image that you set on your own. The name will be the value saved in the custom field.

The name of the image that you set on your own

In the Advanced settings section of this field, I add a Text to display after the field. It is to list the pagination numbers later which are regulated by a class named 'pagination'.

Add a Text to display after the field

After creating all the fields, move to the Settings tab, choose Location as Post type, and select a post type to which you want to assign these fields to.

Move to the Settings tab, choose Location as Post type, and select a post type to which you want to assign these fields to

Then, go to the post editor, you will see the fields displayed. All the images in the field are displayed without any pagination since we haven’t created them. The Search box also hasn’t worked yet.

All the images in the field are displayed without any pagination

2. Adding functions to the fields

We should add code to the theme’s files to add some functions for the fields. They help to create the pagination and search features.

Go to the functions.php file, and add some lines of code.

function estar_add_list_js_library() {
wp_enqueue_script( 'listjs', '//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js' );
wp_enqueue_script( 'estar-custom-scripts', get_stylesheet_directory_uri() . '/js/scripts.js' );
}
add_filter( 'rwmb_enqueue_scripts', 'estar_add_list_js_library' );

add_action('admin_head', 'my_custom_css');
function my_custom_css() {
echo '<style>
.pagination li {
display:inline-block;
padding:5px;
}
< /style>';
}

First, since I will use JS, I declared the JS online library that I will use in these lines as well as a new file to add code:

function estar_add_list_js_library() {
wp_enqueue_script( 'listjs', '//cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js' );
wp_enqueue_script( 'estar-custom-scripts', get_stylesheet_directory_uri() . '/js/scripts.js' );
}
add_filter( 'rwmb_enqueue_scripts', 'estar_add_list_js_library' );

Add some codes to declare the JS online library

estar is the theme that I’m using for my website (you can download this theme here for free).

The theme that I’m using for my website

These lines of code are used to display the pagination and stipulate its style. But we haven’t created it yet.

These lines of code are used to display the pagination and stipulate its style

Then, go to the created JS file. Add the git.

jQuery( function( $ ) {
$( document ).ready( function() {
var options = {
valueNames: [
{ attr: 'value', name: 'rwmb-image_select' }
],
pagination: true,
page: 3,
};
var featureList = new List( 'background-for-post', options );
} );

$( '#background-for-post .rwmb-image_select-wrapper' ).find( '.rwmb-input' ).addClass( 'list' );
} );

Go to the created JS file and add the git

These are to create the pagination.

These are to create the pagination

In this example, I divided the options in the Image Select field into 3 pages. rwmb-image_select also regulated that the pagination is used for the field that is in the type as Image Select only.

The code regulated that the pagination is used for the field that is in the type as Image Select only

And, the function var featureList = new List( 'background-for-post', options ); handles the search function for the field that you’ve created above. In there, background-for-post is the ID of the field group I’ve created.

The function handles the search function for the field that you’ve created above

Now, back to the post editor, you'll see the paginations as page numbers 1, 2, and 3. Click on each page, you will see different images. Also, the search box worked well.

The paginations as page numbers 1, 2, and 3. Also, the search box worked well.

We’ve just finished this practice.