I'm trying to use do_shortcode in a shortcode function. The problem is that when I use [terms_and_conditions] on my page it doesn't parse the do_shortcode and I see [MM_Form_Field type='custom' id='1' isRequired='true' class='']. It isn't being executed.
This is what my function looks like:
function terms_and_conditions( $atts ) {
// Variables
$terms = get_field('terms_content', 'options');
$checkbox = get_field('terms_checkbox_msg', 'options');
// $smarttag = get_field('terms_smarttag', 'options');
$smarttag = do_shortcode(get_field('terms_smarttag', 'options'));
var_dump($smarttag);
$html_out = '';
$html_out .= '<section id="terms">';
$html_out .= '<div class="terms-content-wrapper">';
$html_out .= $terms;
$html_out .= '</div>'; // end terms-content-wrapper
$html_out .= '<div class="terms-checkbox-wrapper">';
$html_out .= '<div class="terms-checkbox">';
$html_out .= do_shortcode($smarttag);
$html_out .= '<p>' . $checkbox . '</p>';
$html_out .= '</div>'; // end terms-checkbox
$html_out .= '</div>'; // end terms-content-wrapper
$html_out .= '</section>'; // end tip-wrapper
return $html_out;
}
add_shortcode( 'terms_and_conditions', 'terms_and_conditions');
Using ob_start can clean up your shortcodes drastically. But you may prefer to avoid them. This should work...
<?php
function terms_and_conditions( $atts ) {
// Variables
$terms = get_field('terms_content', 'options');
$checkbox = get_field('terms_checkbox_msg', 'options');
$smarttag = get_field('terms_smarttag', 'options');
ob_start();
?>
<section id="terms">
<div class="terms-content-wrapper">
<?php
if($terms){
echo $terms; // <- is terms an array or a string?
}
?>
</div>
<div class="terms-checkbox-wrapper">
<div class="terms-checkbox">
<?php
echo do_shortcode($smarttag);
if(!empty($checkbox)){
echo '<p>' . $checkbox . '</p>'; // <- is checkbox an array or a string?
}
?>
</div>
</div>
</section>
<?php
return ob_get_clean();
}
add_shortcode( 'terms_and_conditions', 'terms_and_conditions');
Another thing you may want to try is changing the quotes you use in your shortcode:
eg, use this:
[MM_Form_Field type="custom" id="1" isRequired="true" class=""]
instead of this:
[MM_Form_Field type='custom' id='1' isRequired='true' class='']
Related
I'm trying to create a WP shortcode that would include both html and php. For example, something like that (that does not work):
function my_first_shortcode() {
$content = <<<EOT
<h1>Some title</h1>
<p><?php the_field('description'); ?></p>
EOT;
return $content;
}
add_shortcode('my_shortcode', 'my_first_shortcode');
The the_field('name_of_field'); normally outputs the content of the specified variable/field (Advanced Custom Fields).
Is the HEREDOC way the right way of doing that? If so, how would I do it? It'd be also great if I could pass variables to the shortcode.
Thank you
First, you can't write PHP tags inside HEREDOC.
You can use it like that:
$the_field = 'the_field';
$content = <<<EOT
<h1>Some title</h1>
<p>{$the_field('description')}</p>
EOT;
In order to pass attributes to a shortcode it's very simple.
for example we have the shortcode:
[my_shortcode att_1="some_value" att_2="some_value"]
function my_first_shortcode($atts)
{
$att_1 = $atts['att_1'];
$att_2 = $atts['att_2'];
}
add_shortcode('my_shortcode', 'my_first_shortcode');
I always prefer to use output buffering for my shortcodes, example below.
function my_first_shortcode() {
ob_start(); ?>
<h1>Some title</h1>
<p><?php echo the_field('description'); ?></p>
<?php
return ob_get_contents();
}
add_shortcode('my_shortcode', 'my_first_shortcode');
add_shortcode('location_start_your_application_group', 'start_your_application_group');
function start_your_application_group() {
ob_start();
$start_your_application_group = '';
$start_your_application_group .= '<section class="start-your-application">';
if ( have_rows( 'start_your_application_group', 'option' ) ) :
while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row();
$heading = get_sub_field( 'heading' );
$content = get_sub_field( 'content' );
if ( $heading !== '' ) {
$start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
}
if ( $content !== '' ) {
$start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
}
$image = get_sub_field( 'image' );
if ( $image ) {
$start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . esc_url($image['url']) .'" alt="' . $image['alt'] . '" /></div>';
}
endwhile;
endif;
$start_your_application_group .= '</section>';
$start_your_application_group = ob_get_clean();
return $start_your_application_group;
}
<?php
function ash_now_big(){
$debut = 0; //The first article to be displayed
query_posts('cat=45&showposts=1');
while(have_posts()) : the_post();//while loop
$myposts = get_posts('numberposts=-1&offset=$debut');
if (has_post_thumbnail( $post->ID ) ):
$output .= $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$output .=' <img src="'. $image[0] .'" style="width:100%">';
endif; // end of if
endwhile; //end of while
return $output;
}
add_shortcode("ash_now_big", "ash_now_big");//creating shortcode
This shortcode when used just displays [ash_now_big] what could be the problem
Where have you put this code in your project? Do you have any plugin or do you just want to put it in function file?
Please put below code in your theme functions.php file:
function ash_now_big(){
$debut = 0; //The first article to be displayed
query_posts('cat=1&showposts=1');
while(have_posts()) : the_post();//while loop
$myposts = get_posts('numberposts=-1&offset=$debut');
if (has_post_thumbnail( $post->ID ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
$output .=' <img src="'. $image[0] .'" style="width:100%">';
endif; // end of if
endwhile; //end of while
return $output;
}
add_shortcode("ash_now_big", "ash_now_big");//creating shortcode
I have a shortcode in WordPress which shows the last post by author, the problem is that I want to exclude a concrete author(ID=13). The shortcode is the next:
function latest_posts_c( $array ) {
global $post;
$defaults = array(
'show' => 3,
'excerpt' => 'false',
'post_type' => 'post',
);
extract( shortcode_atts( $defaults, $array ) );
$args = array(
'posts_per_page' => $show,
'post_type' => $post_type,
);
// Gets posts form database
$query = new WP_Query( $args );
// Displays posts if available
if( $query ) {
$i = 0;
while ( $query->have_posts()) : $query->the_post();
if ($i == 0)
$html = '<div class="column dt-sc-one-third first">';
else
$html .= '<div class="column dt-sc-one-third">';
$html .= '<article id="post-'.get_the_ID().'" class="'.implode(' ', get_post_class('blog-entry')).'">';
$html .= '<div class="blog-entry-inner">';
$html .= '<div class="entry-meta">';
$html .= ' ';
$html .= ' <div class="date">';
$html .= ' <p>'.get_the_date('M').' '.get_the_date('d').' <span>'. get_the_date('Y') .'</span> </p>';
$html .= ' </div>';
$html .= '</div><!-- .entry-meta -->';
if( has_post_thumbnail() ):
$html .= '<div class="entry-thumb">';
$html .= ' '.get_the_post_thumbnail(get_the_ID(), 'medium').'';
$html .= '</div><!-- .entry-thumb -->';
endif;
$html .= '<div class="entry-details">';
if(is_sticky()):
$html .= ' <div class="featured-post"> <span class="fa fa-trophy"> </span> Destacado</div>';
endif;
$html .= ' <div class="entry-title">';
$html .= ' <h4>'.get_the_title().'</h4>';
$html .= ' </div>';
$html .= ' <div class="entry-metadata">';
$html .= ' <p class="author">';
$html .= '<span class="fa fa-user"> </span>';
$html .= ' '.get_the_author().'</p>';
$categories = 0;/*get_the_category();*/
$separator = ', ';
$output = '';
if($categories){
$j = 0;
foreach($categories as $category) {
$output .= ''.$category->cat_name.'';
$j++;
if ($j < count($categories)) $output .= $separator;
}
$html .= ' <p class="categories"><span class="fa fa-folder-open"> </span>'.$output.'</p>';
}
$html .= ' </div><!-- .entry-metadata-->';
$html .= ' <div class="entry-body">';
$html .= ' '.dttheme_excerpt(50);
$html .= ' <p><a href="'.get_permalink().'" title="'.get_the_title().'" class="dt-sc-button small read-more">';
$html .= ' Leer más <span class="fa fa-angle-double-right"> </span></a></p>';
$html .= ' </div>';
$html .= '</div><!-- .entry-details -->';
$html .= '</div><!-- .blog-entry-inner-->';
$html .= '</article>';
$html .= '</div>';
$i++;
endwhile;
}
$html .= '<div class="dt-sc-clear"></div>';
// Resets Post Data
wp_reset_postdata();
// Returns the results
return $html;
}
add_shortcode('latestposts_c', 'latest_posts_c');
I added an If to block the code if there is a post from author ID=13, but the problem is that the shortcode doesn't show anything. I'm also using "authors_in" to allow certain authors ID inside the array $args and $default but nothing....Any idea?
Try this:
$args = array(
'posts_per_page' => $show,
'post_type' => $post_type,
'author' => '-13'
);
The author parameter can be set to include/exclude specific authors.
Reference http://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
Hello I would like to make part of the output translatable in my shortcode but I don't know how to do it.
I tried several times but even if I managed to add the code, it was displayed outside of the div that outputs the variables so won't work..
My code without translation string is:
add_shortcode('cv', 'vp_cv');
function vp_cv($atts, $content=null) {
extract(shortcode_atts(array(
'number' => 6
), $atts));
global $post;
$output .= '<div class="container">';
$query = new WP_Query('post_type=resume&posts_per_page=' . $number . '&cat=' . $categories);
while($query->have_posts() ) : $query->the_post();
$year = get_post_meta($post->ID, 'resume_year', true);
$title = get_the_title();
$client = get_post_meta($post->ID, 'resume_client', true);
$address = get_post_meta($post->ID, 'resume_address', true);
$output .= '<p class="year">' . $year . '</p>';
$output .= '<p class="cv-title">' . $title . '</p>';
$output .= '<p class="cv-client"> <strong> Client:</strong> ' . $client . '</p>';
$output .= '<p class="cv-address"> <strong> Address:</strong> ' . $address. '</p>';
endwhile;
$output .= '</div>
<div class="clearboth"></div>';
return $output;
}
I'd like to add to the client and address a translatable string like:
<?php _e('Client:','ikos');?>
And it must result inside the tags
Thanks!
Assuming that you're loading text domain correctly, try this:
<?php
// ....
$output .= '<p class="cv-client"> <strong> ' . __( 'Client: ', 'ikos' ) . ' </strong> ' . $client . '</p>';
$output .= '<p class="cv-address"> <strong> ' . __( 'Address: ', 'ikos' ) . ' </strong> ' . $address. '</p>';
// ....
?>
Using __( 'Translatable string', 'your-text-domain' ); return the string translated without echo.
Using _e( 'Translatable string', 'your-text-domain' ); echoes the translated string.
Try it, hope it helps! If something is not clear feel free to ask.
I want to remove the nofollow code from the latest posts displayed in the sidebar. I found that the code which adds rel=nofollow tag to latest post is located here
theme folder/example theme/lib/activity/plugin.php.
Here is the exact code:
private function get_latest_posts( $post_count ) {
// Get the latest posts
$latest_posts = get_posts(
array(
'numberposts' => $post_count,
'order' => 'desc',
'orderby' => 'date'
)
);
// Create the markup for the listing
$html = '<div class="tab-pane" id="recent">';
$html .= '<ul class="latest-posts">';
if( count( $latest_posts ) > 0 ) {
foreach( $latest_posts as $post ) {
$html .= '<li class="clearfix">';
// Add the small featured image
if( has_post_thumbnail( $post->ID ) ) {
$html .= '<a class="latest-post-tn fademe" href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_post_thumbnail( $post->ID, array( 50, 50 ) );
$html .= '</a>';
} // end if
$html .='<div class="latest-meta">';
// Add the title
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
$html .= get_the_title( $post->ID );
$html .= '</a>';
// Add date posted
// If there's no title, then we need to turn the date into the link
if( strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '<a href="' . get_permalink( $post->ID ) . '" rel="nofollow">';
} // end if
$html .= '<span class="latest-date">';
$html .= get_the_time( get_option( 'date_format' ), $post->ID );
$html .= '</span>';
// Close the anchor
if(strlen( get_the_title( $post->ID ) ) == 0 ) {
$html .= '</a>';
} // end if
$html .='</div>';
$html .= '</li>';
} // end foreach
} else {
$html .= '<li>';
$html .= '<p class="no-posts">' . __( "You have no recent posts.", 'standard' ) . '</p>';
$html .= '</li>';
} // end if/else
$html .= '</ul>';
$html .= '</div>';
return $html;
} // end get_latest_posts
Now please tell me how to remove the nofollow tag from this code using the child theme?
Since you have control of the child theme, you can wrap the call to display the widget zone for that widget with something that grabs the output, performs a regex search/replace on it, and outputs the result. I wrote a blog post about that recently:
Filtering the output of WordPress widgets
The basics are that you have a function that replaces dynamic_sidebar() with your own function, like this:
function theme_dynamic_sidebar($index = 1) {
// capture output from the widgets
ob_start();
$result = dynamic_sidebar($index);
$out = ob_get_clean();
// do regex search/replace on $out
echo $out;
return $result;
}
Seems that you are out of luck.
That's a private function and no filter hooks are offered by the theme author.
You may try to override the include('path/to/plugin.php'); and include your own modified version.