Saturday, October 16, 2021

Directorist | Template Override | Use Multiple iFrames

 


Directorist is a plugin with full of functionalities, anyone can create almost any type of website with this, depends on the user's imagination.

Template override is a feature that allows you to customize it in many ways to fulfill your requirements. Here, we are going to check how we can create a Textarea field into a multiple iFrame field by overriding the templates.

We have to follow some simple steps to achieve this functionality on our website.


1. Enabling the key field:

Please copy and paste the following code at the end of the "functions.php" file of the currently activated theme folder. 

Directorist | Enable Field Key



  1. add_filter('directorist_custom_field_meta_key_field_args', function ($args) {
  2.     $args['type'] = 'text';
  3.     return $args;
  4. });


2. Create a custom Textarea field:

Please create a custom Textarea field and make sure the key ( meta key ) has the word "iframe" in it.

Directorist | Custom Field Key


3. Single listing template:

Place the custom Textarea field on the single listing templates. 

Directorist | Single Listing Template


If you are using a pricing plan extension please make sure you have allowed this field in the pricing plan. 


Directorist | Allow Custom Fields On Pricing Plan


4. Template Override:

Please read the template override documentation before you start this process. This will help you to understand how to override the templates properly.

Template Override Documentation

a) Override the single listing template: 

Please create a file "directorist/single/custom-fields/textarea.php" in your currently activated theme folder with the following code -

Directorist | Single Listing | Textarea Template Override



  1. <?php
  2.  
  3. /**
  4.  * @author  wpWax
  5.  * @since   6.7
  6.  * @version 7.0.5.2
  7.  */
  8.  
  9. if (!defined('ABSPATH')) exit;
  10. ?>
  11.  
  12. <div class="directorist-single-info directorist-single-info-textarea">
  13.  
  14.     <?php if (isset($data['field_key']) && strpos($data['field_key'], 'iframe') !== false) : ?>
  15.         <div class="directorist-single-info__value"><?php echo $value; ?></div>
  16.     <?php else : ?>
  17.         <div class="directorist-single-info__label">
  18.             <span class="directorist-single-info__label-icon"><?php directorist_icon($icon); ?></span>
  19.             <span class="directorist-single-info__label--text"><?php echo esc_html($data['label']); ?></span>
  20.         </div>
  21.         <div class="directorist-single-info__value"><?php echo esc_html($value); ?></div>
  22.     <?php endif; ?>
  23.  
  24. </div>


b) Override the listing form template:

Please create a file "directorist/listing-form/custom-fields/textarea.php" in your currently activated theme folder with the following code -

Directorist | Listing Form | Textarea Template Override


  1. <?php
  2.  
  3. /**
  4.  * @author  wpWax
  5.  * @since   6.6
  6.  * @version 6.7
  7.  */
  8.  
  9. if (!defined('ABSPATH')) exit;
  10.  
  11. $max = !empty($data['max']) ? 'max="' . esc_attr($data['max']) . '"' : '';
  12. ?>
  13.  
  14. <div class="directorist-form-group directorist-custom-field-textarea">
  15.  
  16.     <?php $listing_form->field_label_template($data); ?>
  17.  
  18.     <?php if (isset($data['field_key']) && strpos($data['field_key'], 'iframe') !== false) : ?>
  19.         <textarea <?php echo $max; ?> name="<?php echo esc_attr($data['field_key']); ?>" id="<?php echo esc_attr($data['field_key']); ?>" class="directorist-form-element" rows="<?php echo (int) $data['rows']; ?>" placeholder="<?php echo esc_attr($data['placeholder']); ?>" <?php $listing_form->required($data); ?>><?php echo esc_html($data['value']); ?></textarea>
  20.     <?php else : ?>
  21.         <textarea <?php echo $max; ?> name="<?php echo esc_attr($data['field_key']); ?>" id="<?php echo esc_attr($data['field_key']); ?>" class="directorist-form-element" rows="<?php echo (int) $data['rows']; ?>" placeholder="<?php echo esc_attr($data['placeholder']); ?>" <?php $listing_form->required($data); ?>><?php echo wp_kses_post($data['value']); ?></textarea>
  22.     <?php endif; ?>
  23.  
  24.     <?php $listing_form->field_description_template($data); ?>
  25.  
  26. </div>

5. Insert iFrames:

Now you can insert the iFrames in the custom Textarea field you have created in the backend or frontend.

a) Backend:

Directorist | Backend Listing Form


b) Frontend:

Directorist | Frontend Listing Form


6. Output:

If everything goes well this should be the output on the frontend single listing page -

Directorist | Template Override | Use Multiple iFrames

Thank you for following this instruction. Please comment below if you face any difficulties or you have any queries regarding this.


No comments:

Post a Comment