Translating a theme - wordpress

I am tryign to make my theme translatable so I am using the following command to output text. But its not outputting a default value i thought the __ did that automartically.
<?php __('PLAYER POINTS AT A GLANCE.','gogreensoccer');?>
I am using the above to display a translatable string to wordpress buts its empty.
<div class="skill-title">
<h3><?php __('PLAYER POINTS AT A GLANCE.','gogreensoccer');?></h3>
</div>
<div class="col-md-5 col-sm-12">
<div class="kids-dashboard-skill">
<div class="skill-show">
<div class="points"><h3><span><?php echo $player->display( 'points' ); ?></span>POINTS</h3></div>
<div class="circle-skill"><div id="circle" data-size="<?php echo $player->display( 'points' ); ?>" data-thickness="35"></div></div>
</div>
<div class="skill-button">
<center>
<button><?php __('VIEW MY TEAM MATES.','gogreensoccer');?></button>
<button><?php __('Player ID','gogreensoccer');?><?php echo $playerId;?></button>
</center>
</div>
</div>
But im not getting any text outputed obv I want a default value here if no translation exists i though __(string,themename) would achieve this.

You don't use the right function: __() returns the text or the translation if it exists. If you want to display it, you must use echo like this: echo __('some text', 'textdomain').
As an alternative, if you just want to print the text, you can use the _e() function which will echo the text (the one in parentheses or the translation if it exists). You can use it like this for example: _('some text', 'textdomain').

Use esc_html_e() function to escape your translatable text and output it.
_e() is a shorthand notation for echo __(), while __() just returns the translated string (not echoing it out).
Anyways, the best practice is to escape all things.

Related

I can't debug whats wrong with my ACF get_field() to display an image array's url value

So my code is pretty straightforward. I am just getting a field value from the global fields (Options). So this field is on Dashboard -> Options (I have ACFpro) and I am trying to echo out a logo which is an image array.
This is how my code looks like
<ul class="text-white p-0">
<a class="brand " href="/">
<?php
$image = get_field('logo');
echo '<p id="debug">'.($image ['url']).'</p>';
?>
</a>
</ul>
al i am trying to do is pull the url value and insert it to a <p> tag just to make sure it pulls the right value. but instead I am getting this error.
error when trying to echo $image
Any idea on what am I doing wrong?
It is one of two things. You need to add the ID or options to the get_field() as a parameter or you need to adjust the return format. By default it tries to pull the ID of the page get_field() is on.
I would change it to
get_field( 'logo', 'options' );
Or change options to whatever you called your options page.
https://www.advancedcustomfields.com/add-ons/options-page/

How can I add support for client-provided alt text in my custom WordPress theme?

I am building a theme and I have images displaying on the site via ACF. I am trying to get the alt text that the client enters when they upload the image. I understand that the image return format should be set to image array or ID, but not URL. Mine were set to URL quite some time ago and I am having trouble revising my code to reflect the alt text when inspected. Here is my current function that displays staff repeater.
function meetOurTeamListing() {
if(have_rows('meet_our_team')):
while(have_rows('meet_our_team')): the_row();
$staffImage = get_sub_field('staff_image');
$staffName = get_sub_field('staff_name');
$staffTitle = get_sub_field('staff_title');
$staffBio = get_sub_field('staff_bio');
$html =
'
<div class="myBtn staff-item">
<img src="'.$staffImage.'" />
<p class="staff-name">'.$staffName.'</p>
<p class="staff-title">'.$staffTitle.'</p>
</div>
<!-- The Modal -->
<div class="myModal modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="modal-header-text">
<p class="modal-name">'.$staffName.'</p>
<p class="modal-pipe">|</p>
<p class="modal-title">'.$staffTitle.'</p>
</div>
</div>
<img src="'.$staffImage.'" />
<div class="modal-body">'.$staffBio.'</div>
</div>
</div>
';
echo $html;
endwhile;
endif;
}
Obviously the sub-field would need to be converted to an image array, however simply switching the radio selector from "image url" to "image array" breaks the images on the front end. Given this context, how can I implement alt text from the WP database dynamically?
Problem solved. Thanks Nicole!
My problem was that when set up the custom fields that took images, I set them to Image URL. This is good if you just want to quickly display an image because it just returns a string. It should be set to Image Array. This results in an itemized list of key/value pairs (ie. alt text) that are accessible in the theme template.
To display your image that is set to an Image Array, here's the syntax:
<img src="'.esc_url($image['url']).'" alt="'.esc_attr($image['alt']).'" />
This works only inside of my PHP function. Alternatively, the following can be used inside a template file among the HTML:
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />

inner nested shortcodes not working - eg: columns not showing inside of row

I wanted to create specially styled columns, but make it easy for the client to still edit the content of the column.. so I created my own shortcodes to setup the row and columns inside. (This is a custom wordpress template I created for a specific client).
Here is the code in my function.php for the two shortcodes 'member-row' & 'member':
add_shortcode('member-row', function ($content = null) {
return '<div class="row">
<br />'.do_shortcode($content).'</div>';
});
add_shortcode('member', function ($atts, $content = null) {
extract(shortcode_atts(array(
'color' => 'white',
), $atts));
return '<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-'.$color.'">'.do_shortcode($content).'</div>
</div>';
});
I've also added the .do_shortcode($content). on the inner-nested 'member' column shortcode - just in case the client wants to add other shortcodes inside of each column. (Note: The problem still occurs even if I change the 'member' shortcode to just use .$content.)
Here is the code I entered in the text side of the wordpress page editor
[member-row]
[member color="gold"]
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo1.jpg" alt="logo" />
<strong>Company 1</strong>
Address
Telephone
www.example1.com
[/member][member color="yellow"]
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo2.jpg" alt="logo" />
<strong>Company 2</strong>
Address
Telephone
www.example2.com
[/member]
[/member-row]
When I save, and click on 'view page', the section where this has been added doesn't display anything.
This is the html code that is produced:
<div class="row">
<br /></div>
So it is only executing the 'member-row' shortcode, and not the nested 'member' shortcode columns.
This is the correct html code I was expecting:
<div class="row">
<br />
<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-gold">
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo1.jpg" alt="logo" />
<strong>Company 1</strong>
Address
Telephone
www.example1.com
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-yellow">
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo2.jpg" alt="logo" />
<strong>Company 2</strong>
Address
Telephone
www.example2.com
</div>
</div>
</div>
When I just remove the surrounding [member-row] and [/member-row] shortcodes from the editor window, then the 'member' column shortcode does show up properly on the webpage.. so it appears it is just being inside of the 'member-row' shortcode that causes the problem.
Note: I did try adding the add_filter('the_content', 'do_shortcode', 10); line to the bottom of my function.php file, but it didn't seem to make any difference, so I removed it.
Hopefully I have just made some typo error.. Any help will be greatly appreciated!
Thanks.
I figured out the problem.
I had to add $atts to the function for the member-row shortcode as follows:
add_shortcode('member-row', function ($atts, $content = null) {
return '<div class="row">
<br />'.do_shortcode($content).'</div>';
});
It now works as expected!!
I am not sure why I needed to add $atts, as this shortcode is not passing attributes or setting default ones, but it appears you need it if you are going to use enclosing shortcode with $content. (Maybe it is needed because $content is defined as the 'second' parameter???)
I just read in the WP Codex for shortcode api that
"Three parameters {$atts, $content, $tag} are passed to the shortcode
callback function. You can choose to use any number of them including
none of them."
So that seems to me that you could use the $content by itself... but as I just proved that $content can't be by itself - I am still unsure of the reason that my solution worked for me. (Just glad it did!)
If anyone knows the rule or reason for why the $atts is needed to make this work, I would appreciate the comment - to clarify this for myself, and anyone else that has the same problem.
Thanks!

wordpress apply_filter values are not coming inline

I am using follwing code in my wordpress custom theme template
<p id="news"> <?php echo apply_filters('the_content',$myNews)?></p>;
And the desired output should be like this
<p id="news"> Herer goes my news content from $myNewsvariable </p>
But i am getting output like this
<p id="news"></p>Here goes my news content from $myNewsvariable
Please tell me how i can fix this
the_content function prints out content by default. So there is no need to do a duplicate echo before apply_filters.
Also you can apply your filter to get_the_content:
<?php echo apply_filters('get_the_content', $myNews); ?>

Drupal search block customizing

I have my block template block.tpl.php
<div class="block">
<?php print $content ?>
</div>
And I want to change block wrapper for search form
<div class="block search">
<?php print $content ?>
</div>
I'm trying to use block-search.tpl.php but it doesn't work
There is a feature in Drupal - you can override specific template(block-search.tpl.php) in theme when you override general template(block.tpl.php).
Other thing is that probably your cache is not cleared - try
And here you have some nice description how to check other things that could failed.

Resources