Php: how to create variables on the fly - php

I am testing this sample of php code and I wonder why isn't giving the same result:
Head Title
. $theTitle .
instead of:
Head Title
Head Title
$theTitle = "Head Title";
define( "TEMPLATE", "<div class=\"well\">{{theTitle}}</div>");
$Template = str_replace("{{", '" . $', TEMPLATE);
$Template = str_replace("}}", ' . "', $Template);
echo $theTitle;
echo $Template;
How to achieve it?
Regards,
Marco

Use str_replace and replace "{{theTitle}}" with $theTitle.
$Template = str_replace("{{theTitle}}", $theTitle, TEMPLATE);

Related

Display images based of tags in wordpress blog posts

What I want to do is have images display on each blogposts, the images that will be displayed will be what the post was tagged with, for example I will tag 1 post with 2 tags, design and print, I would like 2 small images to appear on that posts page.
This is the code that I have managed to scrape together, the else statement is displaying so a post will have the same amount of default-author.jpg showing as there are tags.
This makes me think that something in the foreach is not working but I'm still trying to learn and this is puzzling me, any help would be much appreciated.
<div class="image_wrap">
<?php
$posttags = get_the_tags(); // Get articles tags
$home = get_bloginfo('url'); // Get homepage URL
// Check if tag-slug.png exists and display it.
if ($posttags) {
foreach($posttags as $tag) {
$upload_dir = wp_upload_dir();
$image_relative_path = "/wp-content/uploads/2017/06/" . $tag->slug .".png";
$image_path = $upload_dir['basedir'] . $image_relative_path;
$image_url = $upload_dir['baseurl'] . $image_relative_path;
if (file_exists($image_path)) {
echo '<a href="' . $home . '/' . $tag->slug . '" /><img title="' . $tag->name . '" alt="' . $tag->name . '" src="' . $image_url . '" /></a>';
// If no image found, output something else.
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/img/default-author.jpg" title="image missing"/>
<?php }
}
}
?>
Does anyone have any ideas on how I can achieve the effect I want, images to be displayed on a blogpost [in wordpress] depending on what tag it has?

My $link doesn't work in a href='mailto'

This is how i get my link
$link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
And this is how it looks when i echo it echo 'My link : ' . $link;
http://www.mylink.com/myfile.php?int=&int1=ASD++
But when i put my link in
echo "<a href='mailto:$mail&subject=Aktiviteter&body=$link'>Press on the link</a>";
I only get
http://www.mylink.com/myfile.php
So i miss my ?int=&int1=ASD++ , how can i get it to? Sorry if im unclear.
Your link is being considered part of the string that builds the email.
Your email client is seeing the request as:
subject = Aktiviteter
body = http://www.mylink.com/myfile.php?int=
int1 = ASD++
Because the email client doesn't know what to do with "int1", it ignores it.
To get around this, you'll want to encode your $link variable into the mailto URL using PHP's urlencode function.
Try this:
<?php
$link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$encoded = urlencode($link);
echo "<a href='mailto:$mail?subject=Aktiviteter&body=$encoded'>Press on the link</a>";
?>
echo "<a href='mailto:$mail?subject=Aktiviteter&body=$link'>Press on the link</a>";
after the email address you need to put ? not &
EDIT
this shold be the entire script, it works for me...
<?php
$mail = 'df#me.com';
$link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
echo "<a href='mailto:$mail?subject=Aktiviteter&body=$link'>Press on the link</a>";
?>
Try string concatenation.
echo "<a href='mailto:" . $mail . "?subject=Aktiviteter&body=" . $link . "'>Press on the link</a>";

convert url to hyperlink on text as formatted

I am using following code for convert url to hyperlink on text.But the problem is i want to use shorten title for hyperlink for example this is url http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted and after convert like this :
http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted
I want to this :
http://stackoverflow.com/...
This is my code :
$stringdata = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 $2', $stringdata);
Title should be shorten but url should be same of original.
Thankyou.
You could always use parse_url found here
Here's an example:
$url = 'http://www.stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted';
$splitUrl = parse_url($url);
echo '<a href="' . $url . '"/>' . $splitUrl['scheme'] . '://' . $splitUrl['host'] .'/... </a>';
parse_urlcreates an array from the URL provided.
UPDATE:
Using Mikael Roos answer at that link I came up with what you needed it to do.
function make_clickable($text) {
$regex = '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#';
return preg_replace_callback($regex, function ($matches) {
$splitUrl = parse_url($matches[0]);
return "<a href='{$matches[0]}'>{$splitUrl['scheme']}://{$splitUrl['host']}/..</a>";
}, $text);
}
echo make_clickable('Some odd text here that makes https://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted clickable');
try this
$var='http://stackoverflow.com/questions/ask?title=convert%20url%20to%20hyperlink%20on%20text%20as%20formatted';
$array=explode('/', $var);
$url=$array[0]."//".$array[2]."/...";

joomla getMailer() not working as expected

im trying to use joomla mailer to send emails but it doesent work, any suggestions? what am i missing? i've searched around the web and SO, but it didnt help.
the only error i get is 500, and i cant understand why...
this is the actual code:
<?php
//framework joomla
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', realpath(dirname(__FILE__) . "/../"));
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
$mainframe =& JFactory::getApplication('site');
//get vars
$session =& JFactory::getSession();
$num1 = $session->get('variable1');
$num2 = $session->get('variable2');
$val= $session->get('variable3');
$uq= $session->get('unique');
$sendto= $session->get('mail');
//mail
$mailer =& JFactory::getMailer();
$mailer->setSender('some1#domain.com');
$recipient = array($sendto, 'some1else#domain.com');
$mailer->addRecipient($recipient);
$body = '<h2>sometext</h2>'
. '<div>sometext</div>'
. '<div> blabla' echo $num1 'blabla </div>'
. '<div> texttext' echo $num2 'texet </div>'
. '<div> texttext' echo $val 'text </div>';
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$mailer->AddEmbeddedImage("$uq".".gif", "image_0", "", "base64", "image/gif");
$mailer->addAttachment("$uq".".gif");
//send
$send =& $mailer->Send();
if ( $send !== true ) {
//Elimina .gif
$mask = "*.gif";
array_map( "unlink", glob( $mask ) );
unset($mailer);
echo 'error: ' . $send->message;
} else {
unset($mailer);
//Elimina .gif
$mask = "*.gif";
array_map( "unlink", glob( $mask ) );
echo 'done';
}
?>
forgot to add specs about the platform:
PHP Version 5.3.22
Joomla! Version 2.5.9
if you need any other info just ask.
update:
i tried another joomla! version, a simplified version of the code without variables and with a single email, plain text emails... nothing seems to work.
internal server error (500) with a blank error log... (wierd, but the installation it's inside a subdirectory with many other installation, and my webhosting allow me to see only the "root" log, so i think this is the problem of the blank error log...)
Not quite sure whether this is the root of your problem. But $mail->setSender() accepts an array. Example : $mail->setSender(array('SENDER EMAIL', 'SENDER NAME'));
AND
$mail->addRecipient() accepts a string OR an array. Use an array if you are sending the email to multiple recipients and the string when you are sending the email to only 1 recipient.
Hope this helps
ok, solved!
this might help someone else so, here is how:
$config =& JFactory::getConfig();
this line grabs the email configuration of Joomla, without this Jmail will not send emails.
//body email
$body .= "<h2>Texttext</h2>" . "\n";
$body .= "<div>TexttextTexttextTexttext" . $blockh . " Texttext " . $blockd . "\n";
$body .= "<div> TexttextTexttext <span> " . $num1 . " </span> Texttext </div>" . "\n";
$body .= "<div>TexttextTexttextTexttext <span> " . $val . " </span> Texttext </div>";
previus concatenation of the body was not working, it seems that double quotes are needed to work...
$mailer->isHTML();
the "true" inside is not necessary since true is the default value..
In answer to KentaS comment of
previus concatenation of the body was not working, it seems that double quotes are needed to work...
It has nothing to do with single or double quotes.
In your original post you had:
$body = '<h2>sometext</h2>'
. '<div>sometext</div>'
. '<div> blabla' echo $num1 'blabla </div>'
. '<div> texttext' echo $num2 'texet </div>'
. '<div> texttext' echo $val 'text </div>';
the part:
'<div> blabla' echo $num1 'blabla </div>'
causes a syntax error (string, followed, without concatenation, by a call to "echo", followed, without concatenation, by a string...!
Somthing which works with single quotes:
$body = '<h2>sometext</h2>'
. '<div>sometext</div>'
. '<div> blabla' . $num1 . 'blabla </div>'
. '<div> texttext' . $num2 . 'texet </div>'
. '<div> texttext' . $val . 'text </div>';
And with double quotes you can do even shorter:
$body = "<h2>sometext</h2>"
. "<div>sometext</div>"
. "<div> blabla $num1 blabla </div>"
. "<div> texttext $num2 texet </div>"
. "<div> texttext $val text </div>";
Cheers,
Dom

Adding code to all <img> tags on wordpress

I'm looking to add rel="image_src" to all of the image tags that appear on a post in wordpress. I have tried editing /wp-includes/media.php from:
$html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />';
to
$html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" rel="image_src" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />';
but to no avail. Am I in the right spot, or is there something else that I should be editing?
Much thanks
It's probably VERY bad to do it this way, as HTML regular expressions are generally frowned upon, but it's the first thing that jumps to mind. Untested, but it should get you started.
add_filter('the_content', 'add_img_src', 20);
function add_img_src($content)
{
preg_match_all('/<img(.*?)>/', get_the_content(), $matches);
if(count($matches[1]) && is_single())
{
foreach($matches[1] as $count => $match)
{
str_replace($match, $match.' rel="image_src"', $content);
}
}
return $content;
}
I would go for Jquery rather modification in wordpress core files. I would probably use below js:
<script>
jQuery(document).ready(function ($) {
$("img").attr("rel","image_src");
});
</script>

Categories