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.
Related
I have been working on trying to get this shortcode to show correctly but everything I have tried does not work. Below is my function:
function monster_shortcode( $atts ) {
$monster = $atts['name'];
$query = new WP_Query( array(
'post_type' => 'monsters',
'name' => $monster,
));
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
$monster_title = the_title();
$monster_size = the_field('size');
$monster_type = 'type';
$monster_alignment = the_field( 'alignment' );
$monster_ac = the_field( 'armor_class' );
$monster_ac_type = the_field( 'ac_type' );
$monster_hp = the_field( 'hit_points' );
$monster_hd = the_field( 'hit_die' );
$monster_speed = the_field( 'speed' );
$monster_str = the_field( 'str' );
$monster_strb = monster_stats( get_field( 'str' ) );
$monster_dex = the_field( 'dex' );
$monster_dexb = monster_stats( get_field( 'dex' ) );
$monster_con = the_field( 'con' );
$monster_conb = monster_stats( get_field( 'con' ) );
$monster_int = the_field( 'int' );
$monster_intb = monster_stats( get_field( 'int' ) );
$monster_wis = the_field( 'wis' );
$monster_wisb = monster_stats( get_field( 'wis' ) );
$monster_cha = the_field( 'cha' );
$monster_chab = monster_stats( get_field( 'cha' ) );
$monster_saves = the_field( 'saving_throws' );
$monster_skills = the_field( 'skills' );
$monster_dmg = the_field( 'damage_adjustments' );
$monster_senses = the_field( 'senses' );
$monster_lang = the_field( 'languages' );
$monster_cr = the_field( 'cr' );
$monster_actions = the_field( 'actions' );
$monster_reactions = the_field( 'reactions' );
$monster_char = the_field( 'characteristics' );
$monster_content = the_content();
$monster_enviro = the_field( 'enviroments' );
$return = '<h1 class="entry-title">' . $monster_title . '</h1>';
$return .= '<div class="monster-meta">' . $monster_size . ' ' . $monster_type . ', ' . $monster_alignment . '</div>';
$return .= '<ul class="monster-stat">';
$return .= '<li><label>Armor Class</label> ' . $monster_ac . ' (' . $monster_ac_type . ')</li>';
$return .= '<li><label>Hit Points</label> ' . $monster_hp . ' (' . $monster_hd . ')</li>';
$return .= '<li><label>Speed</label> ' . $monster_speed . '</li>';
$return .= '</ul>';
$return .= '<ul class="monster-stat abilities">';
$return .= '<li><label>STR</label>' . $monster_str . ' (' . $monster_strb . ')</li>';
$return .= '<li><label>DEX</label>' . $monster_dex . ' (' . $monster_dexb . ')</li>';
$return .= '<li><label>CON</label>' . $monster_con . ' (' . $monster_conb . ')</li>';
$return .= '<li><label>INT</label>' . $monster_int . ' (' . $monster_intb . ')</li>';
$return .= '<li><label>WIS</label>' . $monster_wis . ' (' . $monster_wisb . ')</li>';
$return .= '<li><label>CHA</label>' . $monster_cha . ' (' . $monster_chab . ')</li>';
$return .= ' </ul>';
$return .= '<ul class="monster-stat">';
$return .= '<li><label>Saving Throws</label> ' . $monster_saves . '</li>';
$return .= '<li><label>Skills</label> ' . $monster_skills . '</li>';
$return .= '<li><label>Damage Adjustments</label> ' . $monster_dmg . '</li>';
$return .= '<li><label>Senses</label> ' . $monster_senses . '</li>';
$return .= '<li><label>Langauage</label> ' . $monster_lang . '</li>';
$return .= '<li><label>Challenge Rating</label> ' . $monster_cr . '</li>';
$return .= '</ul>';
$return .= '<p>' . $monster_traits . '</p>';
$return .= '<h4 class="monster-label">Actions</h2><p>' . $monster_actions . '</p>';
$return .= '<h4 class="monster-label">Reactions</h2><p>' . $monster_reactions . '</p>';
$return .= '<h4 class="monster-label">Characteristics</h2><p>' . $monster_char . '</p>';
$return .= '<h4 class="monster-label">Details</h2><p>' . $monster_content . '</p>';
$return .= '<p><label>Enviroments:</label> ' . $monster_enviro . '</p>';
endwhile;
}
return $return;
wp_reset_postdata();
}
add_shortcode( 'monster', 'monster_shortcode' );
I then put the shortcode [monster name="men-at-arms"] on a page. All the function $variables show up before the post content and all the function html shows where it should be. You can see the output here https://www.dropbox.com/s/xvuqofya1jylsfl/Screenshot%20%283%29.png?dl=0
You're using the display functions instead of the return functions.
the_title() will literally echo the title. If you want it as a variable, you need to use get_the_title() (or you can set the third argument to false in the_title() - but that's generally not desirable)
This applies to all the functions you're using that are outputting a value.
the_title() => get_the_title()
the_field() => get_field()
the_content() => get_the_content()
Since it's now pulling in the hosting post's content, you need to pass the ID from the shortcode to the functions. Either like so:
$monster = $atts['name'];
$monster_id = $atts['id'];
...
$monster_title = get_the_title( $monster_id );
$monster_size = get_field( 'size', $monster_id );
Or like so:
$monster = $atts['name'];
...
$monster_title = get_the_title( $post->ID );
$monster_size = get_field( 'size', $post->ID );
Alternatively, you can remove ALL the variable definitions and just modify the HTML returning portion.
while ( $query->have_posts() ) : $query->the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="monster-meta"><?php the_field('size'); ?> <?php the_field('type'); ?> <?php the_field('alignment'); ?>
<ul class="monster-stat">
...
</div>
<?php endwhile; ?>
My loop returns only one post instead all. I going through it 4th time and i do not see any reason why it act like that. Thank you for all your help.
localStorage has been cleaned, cache also - and yeah i have more then one post published in this category :)
function rtf_custom_grid( ) {
$args = array(
'post_type' => 'apartamenty',
'post_per_page' => -1,
'nopaging' => true,
'order' => 'date',
'orderby' => 'DESC'
);
$rtf_query = new WP_Query ( $args );
while($rtf_query->have_posts() ) : $rtf_query->the_post();
$cena_1 = get_field('cena_1');
$cena_2 = get_field('cena_2');
$short_dec = get_field('short_desc');
$output = '<div class="single-apartament">';
if ( has_post_thumbnail() ) :
$output .= '<div class="rtf-apartament-thumbnail">';
$output .= '' . get_the_post_thumbnail(get_the_id(), 'large') . '</div>';
endif;
$output .= '<div class="rtf-apartament-content">';
$output .= '' . get_the_title() . '';
$output .= '<div class="rtf-apartament-excerpt">' . $short_dec . '</div>';
$output .= '<div class="rtf-apartament-prize">';
$output .= '<span>' . $cena_1 . ' / ' . $cena_2 . '</span></div>';
$output .= 'Zobacz</div></div></div>';
endwhile;
wp_reset_query();
return $output;
}
add_shortcode('apartamenty', 'rtf_custom_grid');
$output = ' <div class="single-apartament"> ';
the above line is overwriting the previous content in the variable. So its showing only the last post.
Solution :
$output.= '<div class="single-apartament">';
This will append the html code into the variable instead of overwriting it.
Hope it works for you
I have this function connected to a short code that is supposed to return all stored social profiles. For now it only outputs the first social profile, even though it got two more. I think there is something wrong with the way this code returns the output, I just don't know how to solve it.
function ichi_social_profiles() {
if( $social_profiles = Youxi()->option->get( 'social_profiles' ) ):
?><div class="site-social-profiles">
<ul class="plain-list"><?php
foreach( $social_profiles as $profile ):
$profile = wp_parse_args( $profile, array(
'title' => '',
'url' => '#',
'icon' => 'socicon socicon-500px',
'new_tab' => 0
) );
$output = '<li>';
$output .= '<a href="' . esc_url( $profile['url'] ) . '" title="' . esc_attr( $profile['title'] ) . '"' . ( wp_validate_boolean( $profile['new_tab'] ) ? ' target="_blank"' : '' ) . '>';
$output .= '<span class="' . esc_attr( $profile['icon'] ) . '"></span>';
$output .= '</a>';
$output .= '</li>';
return $output;
endforeach;
?></ul>
</div><?php endif;
}
add_shortcode('some-ikon', 'ichi_social_profiles');
The return statement is too early, inside of the foreach loop, and it is only returning the contents of $output. Also, the div tag, the ul tag and rest of the HTML are being echoed on the page, instead of being returned by the function.
I would set $output = '' at the beginning of the function before the if statement, and append HTML to it as you go, then just return $output at the end, after the endif.
The WordPress Codex has some examples that may also help, though some are much more complex:
https://codex.wordpress.org/Shortcode_API
SOLUTION
I added ob_start() to the beginning of the function before any echo call.
at the end of the function I added:
$response = ob_get_contents();
ob_end_clean();
return $response;
I have got a comment from someone superior to me who wants me to escape some comment in two php snippets which I have posted below. The problem is I don't know how to do that. Can anyone help me by modifying the snippets.
Comment I got:
Comment #1:
Validate and/or sanitize untrusted data before entering into the database. All untrusted data should be escaped before output.
Code Snippet #1:
<?php
if ( get_header_image() && !('blank' == get_header_textcolor()) ) {
echo '<div class="site-branding header-background-image" style="background-image: url(' . get_header_image() . ')">';
} else {
echo '<div class="site-branding">';
}
?>
Code Snippet #2:
<?php
printf(
/* translators: %1$s = text link: sangeet, URL: http://wordpress.org/themes/sangeet/, %2$s = text link: Kiran Kumar Dash, URL: https://twitter.com/TheKiranDash */
__( 'Theme: %1$s by %2$s', 'sangeet' ),
'' . esc_attr( 'sangeet', 'sangeet' ) . '',
'' . esc_attr__( 'Kiran Kumar Dash', 'sangeet' ) . '' );
?>
Comment #2:
esc all get_permalink() in content.php
Code snippet #3
<?php
if ( !is_single() ) {
echo '<div class="index-box">';
if ( has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . get_permalink() . '" title="' . __('Read ', 'sangeet') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
My approach:
I used esc_url to esc the get_permalink() in the snippet. Shall I use esc_all? Or esc_url is just fine.
<?php
if ( !is_single() ) {
echo '<div class="index-box">';
if ( has_post_thumbnail()) {
echo '<div class="small-index-thumbnail clear">';
echo '<a href="' . esc_url(get_permalink()) . '" title="' . __('Read ', 'sangeet') . get_the_title() . '" rel="bookmark">';
echo the_post_thumbnail('index-thumb');
echo '</a>';
echo '</div>';
}
}
?>
As requested:
PS. I didnt write this, only advanced it.
Usage:
$user = sanctify($_GET['user']);
Function:
function sanctify($data){
// Fix &entity\n;
$data = str_replace(array('&','<','>'), array('&','<','>'), $data);
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
$data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);
// Remove javascript: and vbscript: protocols
$data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);
// Remove namespaced elements (we do not need them)
$data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);
$data = str_replace('"','',str_replace("'","",$data));
do{
// Remove really unwanted tags
$old_data = $data;
$data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
}
while ($old_data !== $data);
// we are done...
return $data;
}
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.