Drupal - Clean up exisiting code that converts text into an image - php

I've created the following snippet of code within a custom content.tpl.php template
<?php
$field = field_get_items('node', $node, 'field_youtube_video_link');
if(!empty($field)): ?>
<div class="youtube-popup">
<?php
print "<a class=youtube-play cboxElement href=".$content['field_youtube_video_link']['#object']->field_youtube_video_link['und'][0]['url'].">".$img."</a>";?>
</div>
<?php endif; ?>
I'm really wanting to clean this up and rather than do all of this in the content-type.tpl.php I would prefer to do this through a preprocess hook and then create a custom theme template for it, but I'm unsure of how to achieve this?

You can style by fields. Check out:
https://www.drupal.org/docs/7/theming/template-theme-hook-suggestions
Theme hook suggestions are made based on these factors, listed from
the most specific template to the least. Drupal will use the most
specific template it finds:
field--field-name--content-type.tpl.php
field--content-type.tpl.php
field--field-name.tpl.php
field--field-type.tpl.php

Related

Create entire content block in Advanced Custom Fields

I am new to ACF and am trying to understand how to create entire sections of content.
I can't seem to find any answers to this, though I am perhaps not phrasing the question correctly because of my limited understanding.
I have created my first website using ACF and Gutenberg where I have a separate php template for each block of content, which is then registered as a Gutenberg block. This works perfectly in that the content blocks can contain entire blocks of html/php and are always available but only appear on the page when they are chosen, making it very user friendly.
What I am struggling to understand is how you can insert an entire section WITHOUT Gutenberg - say using a shortcode.
At the moment if I have a structure like so:
<section class="reusable-block">
<div class="title-box">
<?php if( get_field('main_heading') ): ?>
<h1 class="hero-heading"><?php the_field('main_heading'); ?></h1>
<?php endif; ?>
</div>
</section>
If I use the shortcode [acf field="main_heading"] It will obviously output the contents of that field but how do I enter a shortcode that would enter the entire section on a needs basis across any page?
If I hard code it into the page template then the fields are always visible on the WordPress backend page, regardless fo wether they are being used, though if that's the way it has to be then I'll work with.
Unfortunately I need to use ACF with a page builder for a current project (I do not have a choice on this). This is the reason I cannot use Gutenberg and register blocks and will need to use shortcodes.
In short, is there a way to create an entire ACF block that would then contain html and acf fields, and even better that could be built in a separate PHP file as you would when using this in conjunction with Gutenberg.
I have watched about 3 courses online and read lots of resources, but this one thing still seems to allude me.
You already mentioned one possibility yourself: creating a custom shortcode for this.
Therefore, you either need to create a new plugin, or, append this to your (child) theme's functions.php:
function shortcodeHeadingBlock($attr, $content = null) {
if (! class_exists('ACF') ) { // will not work without ACF
return '';
}
$opts = shortcode_atts([
'field' => 'main_heading',
], $attr);
ob_start();
?>
<section class="reusable-block">
<div class="title-box">
<?php if( get_field($opts['field']) ): ?>
<h1 class="hero-heading"><?php the_field($opts['field']); ?></h1>
<?php endif; ?>
</div>
</section>
<?php
return ob_get_clean();
}
function setupShortcodes() {
add_shortcode('headingblock', 'shortcodeHeadingBlock');
}
add_action('plugins_loaded', 'setupShortcodes');
After this, you can use the shortcode
[headingblock]
anywhere across your page where shortcodes are interpreted. The shortcode first checks if the ACF-class is available which implies that the ACF functions should be available, too.
The shortcode also has an optional field attribute that defaults to 'main_heading', meaning you can also do
[headingblock field="another_acf_field_name"].
It then returns your code template and inserts the desired ACF field.
Just in case your .title-box CSS isn't available in the admin interface, you might have to enqueue it separately using the admin_enqueue_scripts hook. Regarding CSS, you will also have to make sure the full selector for .title-box doesn't contain additional classes/objects that might no longer apply when the shortcode places it into other places on the page.

WordPress Custom Template - Divi module shortcode displays as text

I have written a head navigation using the full-width code module in Divi. It is in my Divi library as a global module and works perfectly with most of my pages, but not the ones that are using my custom templates.
The way I am displaying the module is by using it's shortcode at the bottom of my child theme's header.php right before the closing header tag:
<?php echo do_shortcode('[showmodule id="XXXX"]'); ?>
where XXXX is the actual id. But on the pages using custom templates the module displays as plain text like so:
'[et_pb_section global_module="my modules id"][/et_pb_section]'
I have also tried changing the code to:
echo apply_filters('the_content','[showmodule id="XXXX"]');
however, the same issue occurs.
Any Ideas?
I found a good method for this in a thread in the Divi Support Forum, which this dude also explains in a video:
https://www.youtube.com/watch?v=PJqcfz5NyZs
Once you get the global Module number from the URL when viewing it in the Divi Library, the shortcode syntax to load the global module in php is:
<?php echo do_shortcode('[et_pb_section global_module="###"][/et_pb_section]'); ?>
One drawback is that loading a module through a php template file does not allow it to be editable through the Visual Builder.
So, I made a modification to this so I could have an easier place to edit the global footer by adding it to my site's homepage Divi layout, then adding this snippet in my footer.php template file to load on the rest of the pages:
<?php if ( !is_front_page() ) : ?>
<?php echo do_shortcode('[et_pb_section global_module="2310"][/et_pb_section]'); ?>
<?php endif; ?>
This way I can easily have a client use the Visual Builder on the homepage to edit the global footer, instead of having to find it in the Divi Library, which they would likely forget and takes extra click to get there.
Please try adding this code. It will solve your problem
<?php echo do_shortcode('[showmodule id="my modules id"]'); ?>

what is "do_shortcode" in wordpress and how it works?

I'm currently learning wordpress theme development and for better understanding I have downloaded couple of wordpress theme and seeing their code.
But a line of code I can't understand. here it is-->
<?php echo do_shortcode(sq_option( 'footer_text ' ' ')); ?>
maybe its for footer but how this works and how i can use this function when I will create my own.
do_shortcode is a function you have to use if you want to execute a shortcode inside a custom theme template page see more info on the function here: https://developer.wordpress.org/reference/functions/do_shortcode/
Generally do_shortcode(); is use to get data from shortcode or its attribute to your PHP code or to filter any shortcode returning data. Shortcode can be provided by any plugin.
Not sure about it but in your theme's code sq_option("footer_text"); could be a function which is filtering some text from footer.
The code could be as:
<?php echo do_shortcode( '[contact-form-7 id="91" title="quote"]' ); ?>
Reference Link
do_shortcode () is usefull function-> for excute shortcode in template file

How to use shortcode in Magento phtml page

I have a short code
{{block type="ibtheme/product_list_featured" category_id="51" random_products="" template="catalog/product/list/featured.phtml"}}
which is working fine in editor from backend. How can U call the same short-code from a PHTML page ?
When I put the same code, it is printing a simple text.
phtml is php code, not cms html passed through a filter to catch the short codes (macros) and expand them out.
The contents between "{{" and "}}" must interpreted by a template engine and is only valid inside emails, CMS pages/blocks and the wysiwyg editors in the backend.
You put their equivalent into layout and call them as in the following ->
Magento Shortcode CMS block not working on product pages
In Magento CMS or Static block, if you want to add PHP code then you can just call any custom .phtml file by using following code. Like here I am including my_custom.phtml.
{{block type="core/template" name="myCustom" template="cms/my_custom.phtml"}}
This is equivalent to following layout tag:
<block type="core/template" name="myCustom" template="cms/my_custom.phtml">
Hope you find it useful.
The above answers are both incorrect if I'm reading that you would like to use shortcodes in phtml pages. I use these frequently, since we have a tremendous amount of content, and breaking them down in to phtml blocks is the easiest way for us to keep our content fresh.
Anyhow, here's the correct way to use call blocks in phtml:
<?php echo $this->getLayout()->createBlock('core/template')->setTemplate('cms/my_custom.phtml')->toHtml(); ?>
For Example, to use the block in your original answer would be
<?php echo $this->getLayout()->createBlock('ibtheme/product_list_featured')->setTemplate('catalog/product/list/featured.phtml')->toHtml(); ?>
I think this is what you're actually looking for. The code is in the CMS module in the Magento code.
<?php
// Load the cms helper
$helper = Mage::helper('cms');
// get the cms static block processor
$processor = $helper->getBlockTemplateProcessor();
// run the content with the shortcode through the filter
// in this case $item->getAnswer() contains a shortcode
$html = $processor->filter($item->getAnswer());
// print it to the page
echo $html;
?>
Remember anything is possible, just dig deeper. If in doubt, copy the Magento core code.

Custom template or theme (header and footer) for PDP page in Magento

We're redoing our theme in Magento to use a grid system (semantic.gs), and I've been tasked with the duty setting up the PDP page to use this new grid system template. We eventually will want this template on all pages, but for now, it's been specifically requested ONLY on the PDP page. So basically I want to set a custom theme/template (including header and footer, not just columns) that I can load on certain pages. Can anyone point me in the direction of how to go about doing this? Extensive SO and google searching has come up pretty fruitless.
So far:
I created a new layout called 2columns-left-grid.phtml in app/design/frontend/[my_interface]/[my_theme]/template/page
I modified these lines:
<?php echo $this->getChildHtml('header') ?>
and
<?php echo $this->getChildHtml('footer') ?>
To be:
<?php echo $this->getChildHtml('header2') ?>
and
<?php echo $this->getChildHtml('footer2') ?>
I duplicated header.phtml and named it header2.phtml, and duplicated footer.phtml and named it footer2.html
My questions:
Will this work, and if so, what other code do I need to modify (xml files, etc)
How can I get this template to apply ONLY to the PDP page for now?
Thanks!
Hello you can override product tag
< catalog_product_view translate="label">
/app/design/frontend/base/default/layout/catalog.xml
from into your new module layout xml

Categories