using php tags inside opencart html module

Viewed 427

I need to call some php vars inside a html module on opencart, but when i use <?php ?> my page renders as <!--?php ?-->.

is there a way of doing this using the default HTML module of opencart 2.x

1 Answers

UPDATED

<!--?php ?--> this comment caused by CKEDITOR. But even without editor PHP will not run in standard OpenCart on client side. Here is a recipe howto deal with custom PHP in HTML module.

Adding extra field in OpenCart HTML module with PHP support.

admin/view/template/extension/module/html.tpl, find

<div class="form-group">
  <label class="col-sm-2 control-label" for="input-description<?php echo $language['language_id']; ?>"><?php echo $entry_description; ?></label>
  <div class="col-sm-10">
    <textarea name="module_description[<?php echo $language['language_id']; ?>][description]" placeholder="<?php echo $entry_description; ?>" id="input-description<?php echo $language['language_id']; ?>" data-lang="<?php echo $lang; ?>" class="form-control summernote"><?php echo isset($module_description[$language['language_id']]['description']) ? $module_description[$language['language_id']]['description'] : ''; ?></textarea>
  </div>
</div>

Add after

<div class="form-group">
  <label class="col-sm-2 control-label" for="input-description2<?php echo $language['language_id']; ?>"><?php echo $entry_description; ?> 2</label>
  <div class="col-sm-10">
    <textarea name="module_description[<?php echo $language['language_id']; ?>][description2]" placeholder="<?php echo $entry_description; ?> 2" id="input-description2<?php echo $language['language_id']; ?>" data-lang="<?php echo $lang; ?>" class="form-control" style="min-height: 400px;"><?php echo isset($module_description[$language['language_id']]['description2']) ? $module_description[$language['language_id']]['description2'] : ''; ?></textarea>
  </div>
</div>

This will be field for the second description.

catalog/controller/extension/module/html.php, find

$data['html'] = html_entity_decode($setting['module_description'][$this->config->get('config_language_id')]['description'], ENT_QUOTES, 'UTF-8');

Add after

$data['html2'] = html_entity_decode($setting['module_description'][$this->config->get('config_language_id')]['description2'], ENT_QUOTES, 'UTF-8');
      
if (preg_match('|<\?php.+?\?>|isu', $data['html2'])) {              
  ob_start();
  @eval('?>' . $data['html2']);
  $data['html2'] = ob_get_contents();
  ob_end_clean();           
}

This will add a second field and with PHP render.

catalog/view/theme/default(or YOUR_THEME)/template/extentsion/module/html.tpl

Add at the end

<?php if($html2) { ?>  
  <?php echo $html2; ?>  
<?php } ?>
Related