Adding MailChimp Shortcode in Theme with PHP - php

I have the following website: https://www.ajagrawal.com
Currently in my website, I am trying to edit the hero section and replace the name, email and button fields with a custom wordpress form that was made with the plugin. Right now the emails are stored in Wordpress, but I want them in Mailchimp.
I know which code in my theme that form pertains to which is the following:
if(function_exists('newsletter_form')):
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= '<form method="post" action="'.home_url('/').'?na=s" onsubmit="return newsletter_check(this)">';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="text" name="nn" required="" placeholder="'.esc_html($name_placehodler).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-user"></span></div>';
$output .= '</div>';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="email" name="ne" required="" placeholder="'.esc_html($email_placeholder).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-envelope"></span></div>';
$output .= '</div>';
$output .= '<input class="newsletter-submit c-btn type-1 size-4 full" type="submit" value="'.esc_html($btn_text).'">';
$output .= '</form>';
$output .= '</div>';
$output .= '</div>';
endif;
Here is my mailchimp shortcode that represents the form.
[mc4wp_form id="2198"]
I've tried editing the code and inserting it multiple ways and even ended up crashing the site and still no luck. Considering my goal, is this the right approach (putting the shortcode in the PHP code)?
If so, how can this be done?

You can use the do_shortcode function.
Examples in the documentation:
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var;
So in your case something like:
echo do_shortcode( '[mc4wp_form id="2198"]' );
should do the trick.
EDIT:
Something like this should work:
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= do_shortcode( '[mc4wp_form id="2198"]' );
$output .= '</div>';
$output .= '</div>';
Then do whatever you want with output - probably will have to echo it...

Related

How to concatenate increment operator to PHP

$i = 0;
while ($slider_query->have_posts()) {
$slider_query->the_post();
$html ='<div class= "col-lg-3 order-lg-'.$i.' col-md-6 p-0">';
$html .= '<div class= "ss-pic">';
$html .= '<img src="'.get_the_post_thumbnail_url(get_the_ID(), 'full').'"/>';
$html .='</div>';
$html .='</div>';
$html ='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
$html .= '<div class= "ss-text">';
$html .= '<h3>'.get_the_title().'</h3>';
$html .= get_the_content();
$html .= 'Explore';
$html .='</div>';
$html .='</div>';
I want to pre-increment the $i. Can anyone please help me out? I know its a basic, but I have a bit of confusion over the concatenation
So firstly you can pre-increment an integer variable by prepending ++ to it, e.g.
++$i
. That will work for you, e.g.
'<div class= "col-lg-3 order-lg-'.++$i.' col-md-6 p-0">'
However, you also have another problem which is preventing you from seeing the results as you are expecting. Every time you write $html = - which is twice within your loop, you overwrite the contents of $html. The previous contents of the variable are destroyed. Therefore, at whatever time you come to echo $html to the output, you'll only ever see the last version.
You need to concatenate all the HTML together into a single string without overwriting it.
Put
$html = "";
just before the loop starts, and then change
$html ='<div class= "col-lg-3 order-lg-'.$i.' col-md-6 p-0">';
to
$html .='<div class= "col-lg-3 order-lg-'.++$i.' col-md-6 p-0">';
(note the .= there, and also the ++$i which you forgot)
and lower down, change
$html ='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
to
$html .='<div class= "col-lg-3 order-lg-"'.++$i.'"col-md-6 p-0">';
(again just changing the = to .=.)
Demo: http://sandbox.onlinephpfunctions.com/code/7e1ac7b039867eedd22d90dfcdc03e8990419a8f
$i = 0;
while ($slider_query->have_posts()) {
$slider_query->the_post();
$html ='<div class= "col-lg-3 order-lg-'.$i+1.' col-md-6 p-0">';
$html .= '<div class= "ss-pic">';
$html .= '<img src="'.get_the_post_thumbnail_url(get_the_ID(), 'full').'"/>';
$html .='</div>';
$html .='</div>';
$html ='<div class= "col-lg-3 order-lg-"'.$i+2.'"col-md-6 p-0">';
$html .= '<div class= "ss-text">';
$html .= '<h3>'.get_the_title().'</h3>';
$html .= get_the_content();
$html .= 'Explore';
$html .='</div>';
$html .='</div>';
I dont think you can increase a value in one loop. you need to add it with numeric value since you want a different value for each div in one loop

div is not on the code

I'm generating html code with a string this way
foreach ($busquedas as $busqueda) {
$checked = $busqueda->porDefecto ? "checked" : ' ';
$radio_html = "<input type='radio' radio-id='".$busqueda->id."' name='default' class='radio-default' value='Por defecto' checked =".$checked." > Por defecto";
$html .= "<div class='col-md-12 search-div'>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'>".$busqueda->nom."</div>";
$html .= "<div class='col-md-6'>".$_SESSION['user_rol'] == 0?$radio_html:''."</div>";//if $radio_html is shwon the paren div col-md-6 is not shown
$html .="</div>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'><button class='btn btn-default load_search_btn' search_id='".$busqueda->id."'>Cargar</button></div>";
$html .= "<div class='col-md-6'><button class='btn btn-default delete_search_btn' search_id='".$busqueda->id."'>Eliminar</button></div>";
$html .="</div>";
$html .="</div>";
}
When $radio_tml is shown the parent div with class col-md-6 is not on the code but if $radio_html is shown the div is shown too,I thought some tag is not closed but I can't see it
For me your code implies that when you have
$_SESSION['user_rol'] == 0
the div section after that is not closed
You should add brackets as IncredibleHat was saying or add the </div> in the if statement (and not only in the else)

wordpress blog title url

I'm trying to make my blog titles link to the full post from the preview area so the title should have the same function as the read more button. The blogs are in a masonry layout and I'm using a themeforest theme.
This is the blog page.
I believe this to be the php code that controls the layout - hope it helps.
Sorry php newbie here.
I have tried using <h5 class="post-title">'. get_the_title() .'</h5>'; but all this did was generate a broken url containing '.get_permalink().'" in it.
Thank you
<?php if ( '' != get_the_title() ): ?>
<h5 class="post-title"><?php the_title(); ?></h5>
<?php endif ?>
<?php if (has_post_format('link')): ?>
<?php echo __("Read more", TEMPNAME); ?><span class="icon-arrow-right9"></span>
<?php else: ?>
<?php echo __("Read more", TEMPNAME); ?><span class="icon-arrow-right9"></span>
<?php endif ?>
<?php endif; ?>
You just need to wrap the h5 title in an anchor tag <a> on line 37 of your snippet. The specific code to change is:
New Answer
<a href="<?php get_permalink(); ?>">
<h5 class="post-title"><?php the_title(); ?></h5>
</a>
or from you code, try:
<a href="<?php echo $nz_link_url; ?>" title="<?php echo __("Go to", TEMPNAME).' '.$nz_link_url; ?>">
<h5 class="post-title"><?php the_title(); ?></h5>
</a>
Old Answer
if ( '' != get_the_title() ){
$output .= '<h5 class="post-title">'. get_the_title() .'</h5>';
}
You may have to update your CSS to reflect the anchor tag in front of the H5
Full Code
while($recent_posts->have_posts()) : $recent_posts->the_post();
$output .= '<div class="post format-'.get_post_format().'" data-grid="ninzio_01">';
$output .= '<div class="post-wrap nz-clearfix">';
if (get_post_format() == 'image') {
$values = get_post_custom( $post->ID );
$nz_image_url = isset($values["image_url"][0]) ? $values["image_url"][0] : "";
if (!empty($nz_image_url)) {
$output .='<a class="nz-more" href="'.get_permalink().'">';
$output .= '<div class="nz-thumbnail">';
$output .= '<img src="'.$nz_image_url.'" alt="'.get_the_title().'">';
$output .= '<div class="ninzio-overlay"></div>';
$output .= '<div class="post-date"><span>'.get_the_date("d").'</span><span>'.get_the_date("M").'</span></div>';
$output .='</div>';
$output .='</a>';
}
} else {
if (has_post_thumbnail()) {
$output .='<a class="nz-more" href="'.get_permalink().'">';
$output .= '<div class="nz-thumbnail">';
$output .= get_the_post_thumbnail( $post->ID, $size );
$output .= '<div class="ninzio-overlay"></div>';
$output .= '<div class="post-date"><span>'.get_the_date("d").'</span><span>'.get_the_date("M").'</span></div>';
$output .='</div>';
$output .='</a>';
}
}
$output .= '<div class="post-body">';
if ( '' != get_the_title() ){
$output .= '<h5 class="post-title">'. get_the_title() .'</h5>';
}
if ($excerpt == "true") {
$output .= '<div class="post-excerpt">'.nz_excerpt(95).'</div>';
}
$output .=''.__("Read more", TEMPNAME).' <span class="icon-arrow-right9"></span>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';

Conditional Joomla module position and span

I am having trouble setting up a joomla conditional module position and span.
Although I understand HTML, I have trouble with php. According to the tutorials, I have to add
<?php if ($this->countModules( 'user1' )) : ?>
<div class="user1">
<jdoc:include type="modules" name="user1" style="rounded" />
</div>
<?php endif; ?>
Although when I add something like it, I receive an error.
The code that I want to change is:
<?php
//no direct accees
defined ('_JEXEC') or die('resticted aceess');
class Helix3FeatureTitle {
private $helix3;
public function __construct($helix){
$this->helix3 = $helix;
$this->position = 'title';
}
public function renderFeature() {
$app = JFactory::getApplication();
$menuitem = $app->getMenu()->getActive(); // get the active item
if($menuitem) {
$params = $menuitem->params; // get the menu params
if($params->get('enable_page_title', 0)) {
$page_title = $menuitem->title;
$page_title_alt = $params->get('page_title_alt');
$page_subtitle = $params->get('page_subtitle');
$page_title_bg_color = $params->get('page_title_bg_color');
$page_title_bg_image = $params->get('page_title_bg_image');
$style = '';
if($page_title_bg_color) {
$style .= 'background-color: ' . $page_title_bg_color . ';';
}
if($page_title_bg_image) {
$style .= 'background-image: url(' . JURI::root(true) . '/' . $page_title_bg_image . ');';
}
if( $style ) {
$style = 'style="' . $style . '"';
}
if($page_title_alt) {
$page_title = $page_title_alt;
}
$output = '';
$output .= '<div class="sp-page-title"'. $style .'>';
$output .= '<div class="container" id="pagetitle">';
$output .= '<div class="row">';
$output .= '<div class="col-md-6 col-sm-8">';
$output .= '<h1>'. $page_title .'</h1>';
if($page_subtitle) {
$output .= '<p class="lead">'. $page_subtitle .'</p>';
}
$output .= '</div>';
$output .= '<div class="col-md-6 col-sm-4 hidden-xs">';
$output .= '<jdoc:include type="modules" name="datetime" style="none" />';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
return $output;
}
}
}
}
and what I want to do is tell the template that when the module position 'datetime' is empty, the "title" position should have 12span and be "text-center".
Otherwise if module position 'datetime' has a module, the 'title' position should have 6span and be "text-left".
I have tried different methods and I receive different errors each time. This is why I did not mention a spcecific error. For example, if I use this method:
if($this->countModules('datetime')) { $output .= '<div class="col-md-6 col-sm-4 hidden-xs">'; $output .= '<jdoc:include type="modules" name="datetime" style="none" />'; $output .= '</div>'; };
I receive the following error:
Fatal error: Call to undefined method Helix3FeatureTitle::countModules() in ...
I would really appreciate some assistance. Thank you.
The problem is that you are trying to us countModules() in a place where it isn't going to work because that method is from a different class.
From the looks of it what this method is doing is generating the Joomla template.
So what you can do is
$output .="<?php if ($this->countModules( 'user1' )) : ?>";
$output .= '<div class="col-md-6 col-sm-4 hidden-xs">';
$output .= '<jdoc:include type="modules" name="datetime" style="none" />';
$output .= '</div>';
$output .= '<?php endif; ?>';
So that it will generate the code for the condition. I haven't tested because I don't have the class you are using but it should work.
Updated to fix quoting.

wordpress ignores formatting when using template tags in functions.php

I am using the following code to create a shortcode for use in the WYSIWYG.
function consultants( $atts, $content = null ) {
$output = '';
$consultant_query = new WP_Query('post_type=consultants&showposts=10');
if ($consultant_query->have_posts()) :
$output .= '<div class="col-md-12">';
while ($consultant_query->have_posts()) : $consultant_query->the_post();
$output .= '<div class="col-xs-12 col-sm-5 col-md-4 kam-tile-own-bg"><h1>' .the_title(). '</h1> ';
if(has_post_thumbnail())
{
$output .= get_the_post_thumbnail($post->ID,'wpbs-featured-avatar');
} else {
$output .= '
<img class="kam-avatar" width="62" height="62" src="'.get_template_directory_uri().'/images/avatar#2x.jpg" class="attachment-featured_image wp-post-image" alt="" />'; }
$output .= '</div>';
endwhile;
$output .= '</div>';
endif;
wp_reset_postdata();
return $output;
}
The code works fine - HOWEVER it fails on the .the_title(). it throws this at the top of the page it has no respect for the tags or the in which it is contained.
Many thanks
instead of the_title(); use get_the_title();

Categories