Gettext doesnt work what I'm doing wrong? - php

I'm making small plugin to share the post in my word-press
Everything work but I can't get a string to be translatable throw wpml.
Bear with me it's not a question about wpml,
My problem is that I can't figure how to add Get text to my code in the right way.
Here is my code:
function add_social_share_icons($content)
{
$html = "<div class='social-share-wrapper'><div class='share-on'>Share on: </div>";
global $post;
$url = get_permalink($post->ID);
$url = esc_url($url);
if(get_option("social-share-facebook") == 1)
{
$html = $html . "<div class='facebook'><a target='_blank' href='http://www.facebook.com/sharer.php?u=" . $url . "'>Facebook</a></div>";
}
I want to translate this "Share on:" the wpml use the Gettext to detect the string.
Like this <th><?php echo __('Due date:', 'themedomain'); ?></th>
I tried something like this:
$html = "<div class='social-share-wrapper'><div class='share-on'><?php _e('Share on:'); ?></div>";
But it doesn't work!
Can someone help me please?

You can't embed a function like that into a string, only simple things can be done that way. Instead, you want to concatenate which can be done in a couple of ways, but this is probably the easiest. Also, instead of _e() which echos the text, I'm using __() to just translate
$html = "<div class='social-share-wrapper'><div class='share-on'>" . __('Share on:') . "</div>";
The second parameter of __() is the text domain, same as _e()

Related

PHP script inside of string?

Is this possible?
What I am trying to accomplish:
Create a html template in the form of a string
Inside the string, add a script, something like include 'php/countries.php';
Echo entire string to html page
Everything but the 2nd step works. I would like to see a php file echo the question being asked, onto the html page, including an echo from another php file.
EXAMPLE
echo "<div id=\"first\"><?php include \'countries.php\'; ?></div>";
I have tried the above, as well as the below:
EXAMPLE
echo "<div id=\"first\">".include 'countries.php'."</div>";
Would this require eval?
Any and all help is appreciated.
Seems a bit silly, but you could do the following:
echo "<div id=\"first\">" . file_get_contents('countries.php') . "</div>";
Or...
echo "<div id=\"first\">";
include "countries.php";
echo "</div>";
Or...
$externalfile = compileexternal('countries.php');
function compileexternal($file) {
ob_start();
require $file;
return ob_get_clean();
}
echo "<div id=\"first\">" . $externalfile . "</div>";
If none of these are what you need, please update the question. There are a dozen ways.
You can use
eval()
But it is not a good practice.
You can use a regular expression.
For example, your string could be;
<div id="first">{{countries.php}}</div>
You'd then do;
$string = "<div id='first'>{{test2.php}}</div>";
echo preg_replace_callback("/(\{\{.+\}\})/", function($matches) {
include_once( str_replace(array("{", "}"), "", $matches[0]));
}, $string);
Check the file exists if( file_exists() )
Check the file can be included (we don't want to include ../../../../../etc/passwd

having difficulty echo iframe with advanced custom fields wordpress

Hi guys this is a pretty dumb question im really bad with php and this is basically not working and outputting the wrong thing can someone please help me.
if (get_field("gallery")){
while (has_sub_field("gallery")){
if (get_row_layout() == "video"){
echo '<ul class="bxslider">';
echo '<li>';
echo '<iframe src="'.the_sub_field("videos").'" frameborder="0"></iframe>';
echo '</li>';
}
heres a screenshot of whats happening as you can see the URL is not in the iframe src
Load the_sub_field("videos") into a variable first then include that in the string you want to echo out.
$myVar = the_sub_field("videos");
echo '<iframe src="' . $myVar . '"....
Of course this will not work if you are not correctly return a string from the_sub_field("videos") so make sure that function actually returns a string.

Wordpress: PHP Function inside a Shortcode. How?

Here's what I'd like to do. This function which grabs the content of a custom Wordpress post field:
get_post_meta($post->ID, 'society_twitter', true);
Needs to run in place of the string 'yourTwitterUsername' below,
[twitter-widget username="yourTwitterUsername"]
Looking something like:
[twitter-widget username="get_post_meta($post->ID, 'society_twitter', true);"]
Is this possible? If so, what is the syntax? A little more detail: I'm trying to modify a Twitter widget to display the username listed in a custom post field in my database rather than just one string value. I've tried editing the plugin php itself but this seems like an easier solution.
Use string concatenation?
$string = '[twitter-widget username="' . get_post_meta($post->ID, 'society_twitter', true) . '"]';
Edit: Updated based on the comments:
$string = '[twitter-widget username="' . get_post_meta($post->ID, 'society_twitter', true) . '"]';
echo do_shortcode( $string);

Passing DIV through $data variable, but with an Echo Statement in String

Confused by the title? hehe. Not sure how to explain this one, but I think my snippet of code should explain things a little easier.
This is what I'm trying to pass through the $data variable. div is displayed as it should be, but the echo statement inside (which I need) is NOT displayed.
Where am I messing up?
$data['packagename'] = '<div class="somedoodoo"> echo $row->subscription </div>';
You can just use string concatenation:
$data['packagename'] = '<div class="something">' . $row->subscription . '</div>';
you can't execute code inside a string like that, plus, it's the wrong quotes:
$data['packagename'] = <<<EOL
<div class="somedoodoo">{$row->subscription}</div>
EOL;
relevant docs on heredocs: http://php.net/heredoc

Adding A Dynamic Link In Php

I have been using the following to add a dynamic link on a page I am writing, it works ok and appears how it should on the page but I cant help but think that I am going a bit backwards with the way its written as it looks messy. What is the correct way to write it, as if I put it all in one line it doesn't work ?..
echo '<a href="./customer-files/';
echo $customerID;
echo '/';
echo $filename->getFilename();
echo '">';
echo $filename->getFilename();
echo '</a>';
Try with
echo "{$filename->getFilename()}";
Here there is the documentation with a lot of examples of how to concatenate output.
I'd approach it like this:
$safe_customer_id = htmlspecialchars(urlencode($customerID));
$safe_filename = htmlspecialchars(urlencode($filename->getFilename()));
$safe_label = htmlspecialchars($filename->getFilename());
echo "$safe_label";
I would go with this:
$fn = $filename->getFilename();
$link = $customerID . '/' . $fn;
echo ''.$fn.'';
If you're using a template layer, it is even better to break out into PHP only when you need to:
<a href="./customer-files/<?php
echo $customerID . '/' . $filename->getFilename()
?>">
<?php echo $filename->getFilename() ?>
</a>
This way, your IDE will correctly highlight your HTML as well as your PHP. I've also ensured that all PHP is in single-line blobs, which is the best approach for templates (lengthy statements should be banished to a controller/script).
Concatenation is your friend. Use a . to combine multiple string expression into one.
echo ''.$filename->getFilename()/'';
Even better way would be
$filename = $filename -> getFilename(); //cache the filename
echo "<a href='/$customerId/$filename'>$filename</a>";
// ^ On this echo NOTICE that variables can be DIRECTLY placed inside Double qoutes.

Categories