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:

  • Core plugin 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 plugin 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 core plugin to use the MB Settings Page extension.

Let’s get started!

Step 1: Create a Settings Page to Install the Banner

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, add the following code to the functions.php file:

// Register a theme options page
add_filter( 'mb_settings_pages', function ( $settings_pages ) {
    $settings_pages[] = array(
        'id'                   => 'banner',
        'option_name' => 'banner',
        'menu_title'     => 'Banner',
        'icon_url'         => 'dashicons-edit',
        'style'              => 'no-boxes',
        'columns'        => 1,
        'tabs'              => array(
            'design'      => 'Design Banner'
        ),
    );
    return $settings_pages;
} );

This is the settings page that I’ve created:

Create a Settings Page on the WordPress website

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:

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.

 

 

Display the created custom fields to the Banner settings page

Now the custom fields are ready, so you can start customizing the banner and uploading the image for it.

Customize the Banner setting page on WordPress back end

Next, we need to create a shortcode to display images in the desired positions.

Step 3: Create a Shortcode for the Banner

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, …

Step 4: Display the Banner on the Front End

For example, I want to add a product image to the sidebar so I head to Dashboard > Appearance > Widget and paste the shortcode:

Use shortcode to display the Banner on the Front End

However, the banner doesn’t look very well. Therefore, I’m going to style it a bit.

Style the banner created by Meta Box plugin

If you want your banner to be as beautiful as the below image, go to Customizer > Additional CSS to style the image.

Style the image of the banner created with Meta Box plugin

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!

Pick Tools for Your Case Studies

Other case studies you might be interested in

  1. Create A Dynamic Landing Page in WordPress Using Custom Field
  2. How to Create ACF Flexible Content Field with Meta Box
  3. How to Create a Product Page using Meta Box Plugin
  4. How to Add Related Posts to WordPress Using Meta Box
  5. How to Create a Classified Ads Website using Meta Box
  6. Create Posts Series in WordPress Using Meta Box
  7. How to Add Guest Author in WordPress using Meta Box (Part 1)
  8. How to Add Guest Author in WordPress using Meta Box (Part 2)
  9. Create Dynamic Favicon in WordPress using Meta Box plugin
  10. How to Build a Hotel Booking Website Using Meta Box - P1
  11. How to Build a Hotel Booking Website Using Meta Box - P2 - Booking Page in Backend
  12. How to Build a Hotel Booking Website Using Meta Box – P3 – Booking Page for Customer
  13. How to Create an FAQs Page Using Meta Box
  14. How to Manually Reorder Posts with Meta Box
  15. How to Add Custom JavaScript Actions Using Button Field with Meta Box
  16. Display a User List On the Frontend with Meta Box
  17. How to Build a Hotel Booking Website Using Meta Box - P4 - Booking Management Page
  18. How to Filter Posts by Custom Fields and Custom Taxonomies on Archive Pages
  19. How to Use Custom HTML Field to Output Beautiful Texts or Output Custom CSS
  20. Create an OTA Website Like Booking.com with Meta Box Plugin - P1: Create a Page to Introduce Hotel Rooms
  21. Create an OTA Website Like Booking.com with Meta Box Plugin - P2: Create Filters on the Archive Page
  22. Create an OTA Website Like Booking.com with Meta Box Plugin - P3: Create Filters for Single Hotel Pages
  23. How to Add Custom Fields to Display Banners using Meta Box Plugin
  24. How to Create Download Buttons Using Custom Fields with Meta Box Plugin
  25. How to Create a Recipe with Meta Box Plugin
  26. How to Create an Auto-Updated Cheat Sheet in WordPress
  27. How to Create Online Admission Form for School or University
  28. How to Create Online Reservation Form for Restaurants using Meta Box

Leave a Reply

Your email address will not be published. Required fields are marked *