Are you getting stuck on placing a banner on all pages of the website as well as customizing the size, title, image, ... on the banner? Don’t worry, this can be done easily by creating a settings page on the back end, and then add some custom fields. Each custom field is a parameter to customize the banner. After that, just use a shortcode to display the banner on the page you want.
To do so, we use two following plugins:
- Meta Box: a framework that helps you create custom fields easily and quickly. It’s free and available on wordpress.org.
- MB Settings Page: it’s a premium extension of Meta Box that helps you create settings pages with a lot of options right on the back end effortlessly. Note that it’s required to install Meta Box to use the MB Settings Page extension.
Let’s get started!
You have to create a settings page for each banner. Then, when you want to edit any banner, you just need to go to the corresponding settings page.
To create a settings page, go to Meta Box > Settings Page > New Settings Page. This is the UI provided by MB Settings Page. Just fill in the option you want for your settings page.
This is the settings page that I’ve created:
This page is still empty. Therefore, you have to add custom fields to create some options for the banner’s content and parameter.
Step 2: Add Custom Fields for the Settings Page
We need to add the following fields to the above settings page for the banner:
Field Name | Field Type | Content |
Show | Checkbox | Display / Don’t display the banner |
Image | Single image | Choose an image for the banner |
Title | Text | Choose a title for the banner |
Description | Textarea | Add a description for the banner |
Color | Color | Add colors for the banner |
Color’s description | Color | Add colors for the description |
Title’s Position | Radio | Choose a position for the title |
Description’s Position | Radio | Choose a position for the description |
To add these above fields, we can use one of these two methods:
- Use the Online Generator tool (free)
- Use the Meta Box Builder extension (paid)
Method 1: Create custom fields using the Online Generator tool
To get more details about how to use the Online Generator tool, refer to this documentation. Upon creating fields with Online Generator UI, you will have the following code. You need to add this code to the functions.php
file:
// Register meta boxes and fields for settings page add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) { $meta_boxes[] = array( 'id' => 'colors', 'title' => 'Colors', 'settings_pages' => 'banner', 'tab' => 'design', 'fields' => array( array( 'name' => 'Show', 'id' => 'show_banner', 'type' => 'checkbox', 'std' => 0, ), array( 'name' => 'Image', 'id' => 'image', 'type' => 'single_image', ), array( 'name' => 'Title', 'id' => 'title', 'type' => 'text', ), array( 'name' => 'Description', 'id' => 'description', 'type' => 'textarea', ), array( 'name' => 'Color’s title', 'id' => 'color_title', 'type' => 'color', ), array( 'name' => 'Color’s description', 'id' => 'color_description', 'type' => 'color', ), array( 'name' => 'Title’s Position', 'id' => 'position_title', 'type' => 'radio', 'options' => array( 'left' => 'left', 'center' => 'center', 'right' => 'right', ), 'inline' => true, ), array( 'name' => 'Description’s Position', 'id' => 'position_description', 'type' => 'radio', 'options' => array( 'left' => 'left', 'center' => 'center', 'right' => 'right', ), 'inline' => true, ), ), ); return $meta_boxes; } );
Method 2: Create Custom Fields Using Meta Box Builder
After creating fields, move to the Settings tab, choose Settings Pages on the Show for section, and choose Banner for the Settings Page section to display the created fields to the Banner settings page.
Read more: Create custom fields with Meta Box Builders.
Now the custom fields are ready, so you can start customizing the banner and uploading the image for it.
Next, we need to create a shortcode to display images in the desired positions.
Next, add the following code to the functions.php
file to create the shortcode for displaying the banner on the front end:
function short_code_banner() { // Banner $settings = get_option( 'banner' ); $image_ids = $settings['image']; $image_attributes = wp_get_attachment_image_src( $image_ids, 'full'); $title = rwmb_meta( 'title', ['object_type' => 'setting'], 'banner' ); $description = rwmb_meta( 'description', ['object_type' => 'setting'], 'banner' ); $position_title = rwmb_meta( 'position_title', ['object_type' => 'setting'], 'banner' ); $position_description = rwmb_meta( 'position_description', ['object_type' => 'setting'], 'banner' ); $color_title = rwmb_meta( 'color_title', ['object_type' => 'setting'], 'banner' ); $color_description = rwmb_meta( 'color_description', ['object_type' => 'setting'], 'banner' ); $width_content = rwmb_meta( 'width_content', ['object_type' => 'setting'], 'banner' ); if ( !is_admin() ) { echo '<div class="banner" style="background-image: url(' . $image_attributes[0] . ' )">'; echo '<div class= "content-banner" style="width: ' . $width_content . ' "> '; echo '<h2 style="color: ' . $color_title . ' " class="title-banner ' . $position_title . ' ">' . $title . '</h2>'; echo '<div style="color: ' . $color_description . ' " class="description-banner ' . $position_description . ' ">' . $description . '</div>'; echo '</div>'; echo '</div>'; } } add_shortcode( 'banner-shortcode', 'short_code_banner' );
This time, just paste the shortcode [banner-shortcode]
anywhere you want to display the banner such as footer, header, posts, …
For example, I want to add a product image to the sidebar so I head to Dashboard > Appearance > Widget and paste the shortcode:
However, the banner doesn’t look very well. Therefore, I’m going to style it a bit.
If you want your banner to be as beautiful as the below image, go to Customizer > Additional CSS to style the image.
This is the CSS code that I’ve used to customize the image:
.banner { position: relative; height: 500px; background-repeat: no-repeat!important; background-size: cover; background-position: center; } .banner .left { text-align: left; } .banner .right { text-align: right; } .banner .center { text-align: center; } .content-banner { width: 50%; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
Done! The banner has displayed on all pages that you placed the shortcode.
Last Words
Not every theme supports areas for advertising, introduction, ... on the website. Therefore, creating a custom field to display the banner is extremely useful. Especially, this method allows WordPress users to customize the size, font, color, background image, ... and place banners on all the pages on the website with just one click. If you have any questions about adding a custom field to display the banner, leave a comment!
Other case studies you might be interested in
- Create A Dynamic Landing Page in WordPress Using Custom Field
- Create a Filter to Find Hotels by Location
- Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
- Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
- Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
- Create Dynamic Favicon in WordPress using Meta Box plugin
- Create Posts Series in WordPress Using Meta Box
- Display a User List On the Frontend with Meta Box
- Display The Latest Products Section - P2 - Using Meta Box and Elementor
- Display The Latest Products Section - P3 - Using Meta Box And Oxygen
- How to Add Custom Fields to Display Banners using Meta Box Plugin
- How to Add Guest Author in WordPress using Meta Box (Part 1)
- How to Add Guest Author in WordPress using Meta Box (Part 2)
- How to Add Related Posts to WordPress Using Meta Box
- How to Build a Hotel Booking Website Using Meta Box - P1
- How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
- How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
- How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
- How to Create a Classified Ads Website using Meta Box
- How to Create a Product Page - P2 - Using Meta Box and Oxygen
- How to Create a Product Page - P3 - Using Meta Box and Bricks
- How to Create a Product Page - P4 - Using Meta Box and Elementor
- How to Create a Product Page - P5 - Using Meta Box and Gutenberg
- How to Create a Product Page - P6 -Using Meta Box and Breakdance
- How to Create a Product Page using Meta Box Plugin
- How to Create a Recipe - P2 - Using Meta Box and Oxygen
- How to Create a Recipe - P3 - Using Meta Box and Elementor
- How to Create a Recipe - P4 - Using Meta Box and Bricks
- How to Create a Recipe - P5 - Using Meta Box and Zion
- How to Create a Recipe - P6 - Using Meta Box and Brizy
- How to Create a Recipe - P7 - Using Meta Box and Breakdance
- How to Create a Recipe with Meta Box Plugin
- How to Create a Simple Listing - P2 - Using Meta Box and Bricks
- How to Create a Team Members Page - P1- Using Meta Box and Elementor
- How to Create a Team Members Page - P2 - Using Meta Box and Oxygen
- How to Create a Team Members Page - P3 - Using Meta Box and Bricks
- How to Create a Team Members Page - P4 - Just Meta Box
- How to Create a Team Members Page - P6 - using Meta Box and Breakdance
- How to Create a Video Gallery Page - P2 - Using Meta Box + Bricks
- How to Create a Video Gallery Page - P3 - Using Meta Box and Breakdance
- How to Create a Video Gallery Page Using Meta Box + Oxygen
- How to Create ACF Flexible Content Field with Meta Box
- How to Create an Auto-Updated Cheat Sheet in WordPress
- How to Create an FAQs Page - P1 - Using Meta Box and Elementor
- How to create an FAQs page - P2 - Using Meta Box and Oxygen
- How to create an FAQs page - P4 - Using Meta Box and Bricks
- How to Create an FAQs Page -P3- Using Meta Box
- How to Create Buttons with Dynamic Link using Custom Fields
- How to Create Category Thumbnails & Featured Images Using Custom Fields
- How to Create Download Buttons Using Custom Fields with Meta Box Plugin
- How to Create Menus for Restaurants - P1 - Using Meta Box and Elementor
- How to Create Menus for Restaurants - P2- Using Meta Box and Bricks
- How to Create Online Admission Form for School or University
- How to Create Online Reservation Form for Restaurants using Meta Box
- How to Create Relationships - P1 - Using Meta Box and Oxygen
- How to Create Taxonomy Thumbnails & Featured Images - P2 - Using Meta Box and Oxygen
- How to Display Images from Cloneable Fields - P1 - with Gutenberg
- How to Display Images from Cloneable Fields - P2 - with Oxygen
- How to Display Images from Cloneable Fields - P3 - with Elementor
- How to Display Images from Cloneable Fields - P4 - with Bricks
- How to Display Opening Hours for Restaurants - P1 - Using Meta Box + Gutenberg
- How to Display Opening Hours for Restaurants - P2 - Using Meta Box and Oxygen
- How to Display Product Variations - P1 - Using Meta Box and Gutenberg
- How to Display Product Variations - P2 - Using Meta Box and Oxygen
- How to Display Product Variations - P3 - Using Meta Box and Bricks
- How to Display The Latest Products - P5 - Using Meta Box and Bricks
- How to Display the Latest Products - P6 - using Meta Box and Breakdance
- How to Display the Latest Products Section - P4 - Using Meta Box + Zion
- How to Display the Most Viewed Posts - P1 - using MB Views
- How to Display the Most Viewed Posts - P2 - using Meta Box and Oxygen
- How to Filter Posts by Custom Fields - P2 - using Meta Box and FacetWP
- How to Manually Reorder Posts with Meta Box
- How to Show Featured Restaurants on Homepage - P1 - Meta Box + Elementor + WP Grid Builder
- How to Show Posts With a Specific Criteria - P3 - Using MB Views
- How to Show Posts with Specific Criteria - P1 - Using Meta Box and Bricks
- How to Show Posts with Specific Criteria - P2 - Using Meta Box and Oxygen
- How to Show the Featured Restaurants - P3 - using Meta Box and Oxygen
- How to Show the Featured Restaurants - P4 - Using MB Views
- How to Show the Featured Restaurants Section - P2 - Using Meta Box and Bricks
- How to Use Custom HTML Field to Output Beautiful Texts or Output Custom CSS