How to Add Custom Fields to Display Banners using Meta Box Plugin

Tags: wordpress, customfield
Published: 2021-01-05 04:49:38

<!-- wp:paragraph -->

<p>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 <a href="https://metabox.io/series/custom-fields/">custom fields</a>. 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.</p>

<p><!--more--></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>To do so, we use two following plugins:</p>

<!-- /wp:paragraph -->

<!-- wp:list -->

<ul>

<li>Core plugin <a href="https://metabox.io/">Meta Box</a>: a framework that helps you create custom fields easily and quickly. It's free and available on <a href="https://wordpress.org/plugins/meta-box/">wordpress.org</a>.</li>

<li><a href="https://metabox.io/plugins/mb-settings-page/">MB Settings Page</a>: it's a premium <a href="https://metabox.io/plugins/">extension of Meta Box plugin</a> 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.</li>

</ul>

<!-- /wp:list -->

<!-- wp:paragraph -->

<p>Let's get started!</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Step 1: Create a Settings Page to Install the Banner</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>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.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>To create a settings page, add the following code to the <code>functions.php</code> file:</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<pre>// Register a theme options page

add_filter( 'mb_settings_pages', function ( $settings_pages ) {

    $settings_pages[] = array(

        'id'                   =&gt; 'banner',

        'option_name' =&gt; 'banner',

        'menu_title'     =&gt; 'Banner',

        'icon_url'         =&gt; 'dashicons-edit',

        'style'              =&gt; 'no-boxes',

        'columns'        =&gt; 1,

        'tabs'              =&gt; array(

            'design'      =&gt; 'Design Banner'

        ),

    );

    return $settings_pages;

} );

</pre>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>This is the settings page that I've created:</p>

<p><img class="aligncenter" src="https://i.imgur.com/Qa2M9Ev.png" alt="Create a Settings Page on the WordPress website" width="998" height="753" /></p>

<!-- /wp:embed -->

<!-- wp:paragraph -->

<p>This page is still empty. Therefore, you have to add custom fields to create some options for the banner's content and parameter.</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Step 2: Add Custom Fields for the Settings Page</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>We need to add the following fields to the above settings page for the banner:</p>

<!-- /wp:paragraph -->

<!-- wp:table -->

<figure class="wp-block-table">

<table style="width: 59.25%; height: 180px;">

<tbody>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;"><strong>Field Name</strong></td>

<td style="height: 20px; text-align: left;"><strong>Field Type</strong></td>

<td style="height: 20px; text-align: left;"><strong>Content</strong></td>

</tr>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;">Show</td>

<td style="height: 20px; text-align: left;">Checkbox</td>

<td style="height: 20px; text-align: left;">Display / Don't display the banner</td>

</tr>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;">Image</td>

<td style="height: 20px; text-align: left;">Single image</td>

<td style="height: 20px; text-align: left;">Choose an image for the banner</td>

</tr>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;">Title</td>

<td style="height: 20px; text-align: left;">Text</td>

<td style="height: 20px; text-align: left;">Choose a title for the banner</td>

</tr>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;">Description</td>

<td style="height: 20px; text-align: left;">Textarea</td>

<td style="height: 20px; text-align: left;">Add a description for the banner</td>

</tr>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;">Color</td>

<td style="height: 20px; text-align: left;">Color</td>

<td style="height: 20px; text-align: left;">Add colors for the banner</td>

</tr>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;">Color's description</td>

<td style="height: 20px; text-align: left;">Color</td>

<td style="height: 20px; text-align: left;">Add colors for the description</td>

</tr>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;">Title's Position</td>

<td style="height: 20px; text-align: left;">Radio</td>

<td style="height: 20px; text-align: left;">Choose a position for the title</td>

</tr>

<tr style="height: 20px;">

<td style="height: 20px; text-align: left;">Description's Position</td>

<td style="height: 20px; text-align: left;">Radio</td>

<td style="height: 20px; text-align: left;">Choose a position for the description</td>

</tr>

</tbody>

</table>

</figure>

<!-- /wp:table -->

<!-- wp:paragraph -->

<p>To add these above fields, we can use one of these two methods:</p>

<!-- /wp:paragraph -->

<!-- wp:list -->

<ul>

<li>Use the <a href="https://metabox.io/online-generator/">Online Generator</a> tool (free)</li>

<li>Use the <a href="https://metabox.io/plugins/meta-box-builder/">Meta Box Builder</a> extension (paid)</li>

</ul>

<!-- /wp:list -->

<!-- wp:paragraph -->

<p><strong>Method 1: Create custom fields using the Online Generator tool</strong></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>To get more details about how to use the Online Generator tool, refer to <a href="https://docs.metabox.io/online-generator/">this documentation</a>. Upon creating fields with Online Generator UI, you will have the following code. You need to add this code to the <code>functions.php</code> file:</p>

<pre>// Register meta boxes and fields for settings page

add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {

    $meta_boxes[] = array(

        'id'             =&gt; 'colors',

        'title'          =&gt; 'Colors',

        'settings_pages' =&gt; 'banner',

        'tab'            =&gt; 'design',

        'fields' =&gt; array(

        array(

    'name' =&gt; 'Show',

    'id'   =&gt; 'show_banner',

    'type' =&gt; 'checkbox',

    'std'  =&gt; 0,

),

        array(

        'name' =&gt; 'Image',

        'id'   =&gt; 'image',

    'type' =&gt; 'single_image',

),

            array(

                'name' =&gt; 'Title',

                'id'   =&gt; 'title',

                'type' =&gt; 'text',

            ),

            array(

                'name' =&gt; 'Description',

                'id'   =&gt; 'description',

                'type' =&gt; 'textarea',

            ),

            array(

                'name' =&gt; 'Color's title',

                'id'   =&gt; 'color_title',

                'type' =&gt; 'color',

            ),

            array(

                'name' =&gt; 'Color's description',

                'id'   =&gt; 'color_description',

                'type' =&gt; 'color',

            ),

            array(

                'name' =&gt; 'Title's Position',

                'id'   =&gt; 'position_title',

                'type' =&gt; 'radio',

                'options' =&gt; array(

        'left' =&gt; 'left',

        'center' =&gt; 'center',

        'right' =&gt; 'right',

    ),

    'inline' =&gt; true,

            ),

            array(

                'name' =&gt; 'Description's Position',

                'id'   =&gt; 'position_description',

                'type' =&gt; 'radio',

                'options' =&gt; array(

        'left' =&gt; 'left',

        'center' =&gt; 'center',

        'right' =&gt; 'right',

    ),

    'inline' =&gt; true,

            ),

        ),

    );

    return $meta_boxes;

} );

</pre>

<!-- /wp:paragraph -->

<!-- wp:paragraph --><!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><strong>Method 2: Create Custom Fields Using Meta Box Builder</strong></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>After creating fields, move to the <strong>Settings </strong>tab, choose <strong>Settings Pages</strong> on the <strong>Show for</strong> section, and choose <strong>Banner </strong>for the <strong>Settings Page</strong> section to display the created fields to the <strong>Banner </strong>settings page.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><span style="text-decoration: underline;"><strong>Read more</strong></span>: <span style="text-decoration: underline;"><a href="https://metabox.io/add-configure-custom-fields-meta-box-builder/">Create custom fields with Meta Box Builders</a>.</span></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><img class="aligncenter" src="https://i.imgur.com/19tcUzi.png" alt="Display the created custom fields to the Banner settings page" width="1060" height="882" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

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

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><img class="aligncenter" src="https://i.imgur.com/HulJnQ9.png" alt="Customize the Banner setting page on WordPress back end" width="1366" height="1070" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>Next, we need to create a shortcode to display images in the desired positions.</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Step 3: Create a Shortcode for the Banner</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>Next, add the following code to the <code>functions.php</code> file to create the shortcode for displaying the banner on the front end:</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<pre>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' =&gt; 'setting'], 'banner' );

$description = rwmb_meta( 'description', ['object_type' =&gt; 'setting'], 'banner' );

$position_title = rwmb_meta( 'position_title', ['object_type' =&gt; 'setting'], 'banner' );

$position_description = rwmb_meta( 'position_description', ['object_type' =&gt; 'setting'], 'banner' );

$color_title = rwmb_meta( 'color_title', ['object_type' =&gt; 'setting'], 'banner' );

$color_description = rwmb_meta( 'color_description', ['object_type' =&gt; 'setting'], 'banner' );

$width_content = rwmb_meta( 'width_content', ['object_type' =&gt; 'setting'], 'banner' );

if ( !is_admin() ) {

echo '&lt;div class="banner" style="background-image: url(' .  $image_attributes[0]  . ' )"&gt;';

echo '&lt;div class= "content-banner" style="width: ' . $width_content . ' "&gt; ';

echo '&lt;h2 style="color: ' . $color_title . ' " class="title-banner ' . $position_title . ' "&gt;' . $title . '&lt;/h2&gt;';

echo '&lt;div style="color: ' . $color_description . ' " class="description-banner ' . $position_description . ' "&gt;' . $description . '&lt;/div&gt;';

echo '&lt;/div&gt;';

echo '&lt;/div&gt;';

}

}

add_shortcode( 'banner-shortcode', 'short_code_banner' );

</pre>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>This time, just paste the shortcode <code>[banner-shortcode]</code> anywhere you want to display the banner such as footer, header, posts, ...</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Step 4: Display the Banner on the Front End</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>For example, I want to add a product image to the sidebar so I head to <strong>Dashboard </strong>&gt; <strong>Appearance </strong>&gt; <strong>Widget </strong>and paste the shortcode:</p>

<p><img class="aligncenter" src="https://i.imgur.com/BZkuFlt.png" alt="Use shortcode to display the Banner on the Front End" width="1617" height="919" /></p>

<!-- /wp:embed -->

<!-- wp:paragraph -->

<p>However, the banner doesn't look very well. Therefore, I'm going to style it a bit.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><img class="aligncenter" src="https://i.imgur.com/dM83Wlf.png" alt="Style the banner created by Meta Box plugin" width="1169" height="880" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>If you want your banner to be as beautiful as the below image, go to <strong>Customizer </strong>&gt; <strong>Additional CSS</strong> to style the image.</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p><img class="aligncenter" src="https://i.imgur.com/qnPcDfI.png" alt="Style the image of the banner created with Meta Box plugin" width="1600" height="813" /></p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>This is the CSS code that I've used to customize the image:</p>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<pre>.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%);

}

</pre>

<!-- /wp:paragraph -->

<!-- wp:paragraph -->

<p>Done! The banner has displayed on all pages that you placed the shortcode.</p>

<!-- /wp:paragraph -->

<!-- wp:heading -->

<h2>Last Words</h2>

<!-- /wp:heading -->

<!-- wp:paragraph -->

<p>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!</p>