easy php array of images, from a WordPress custom field - arrays

I'm trying to use 1 custom field for a bunch of images - to do the same thing to all the images. I can store them in the custom field however is advisable, but I thought this format would be best, since I think that's what a PHP array goes into:
'http://images.domain.com/image1-Th.jpg',
'http://images.domain.com/image1-Th.jpg',
'http://images.domain.com/image3-Th.jpg'
So, once I have my custom field values entered for a post, here's my non-working PHP code:
<?php //og images
$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
global $wp_query; $postID = $wp_query->post->ID;
$photosfull = array(get_post_meta($postID, 'custom_field_name', true));
echo $ogimagepre.$photosfull.$ogimagepost
?>
You can see I'm trying to get this result:
<meta property="og:image" content="http://images.domain.com/image1-Th.jpg"/>
<meta property="og:image" content="http://images.domain.com/image2-Th.jpg"/>
<meta property="og:image" content="http://images.domain.com/image3-Th.jpg"/>
That's Step1. Ideally, I'd be able to do other things using the same array. Such as replace "-Th.jpg" with "-X3.jpg", since that's a larger size of the same image. And other stuff; need to get past Step1 first.
Thanks!

I had a similar problem where I wanted images under a single meta key to be returned as unique elements. Try this:
$ogimagepre = '<meta property="og:image" content="';
$ogimagepost = '"/>';
global $wp_query; $postID = $wp_query->post->ID;
$photos =get_post_meta($postID, 'custom_field_name', true);
foreach ($photos as $photo){
echo $ogimagepre.$photo.$ogimagepost
}

Related

What is a "template" good for in CakePHPs E-Mailcomponent?

I'm a bit confused using the Cake's (2.3) Email-Class. It appears that we are able to define a "template", a "layout" and a "theme" whereas I only understand the usage of Layouts (located in /app/View/Layouts/Emails).
It seems that everything can be defined in the Layout, but the Template seems do be necessary (at least an empty file) but I don't understand the context because to me it seems that it does not matter what I put in there.
The concept of the theme is even more nebulous to me. Maybe someone can give me a hint here. I found a discussin in a mailing-list which was not really enlightning. The documentation does not reveal this too.
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
--
Edit: Fixed confusing typo.
Edit2: CakeEmail is used directly - not the component.
template is view (in terms of ordinary pages)
layout for emails is as layout for views (in terms of ordinary pages)
layout should contain some common elements like logo and etc
and you can push data to templates like push data to view from controller
Please, check following example:
from custom EmailComponent
public function restore_password($user_to_send_restore_link) {
$email = new CakeEmail('default');
$email->emailFormat('both');
$email->template('restore_password', 'emaillayout');
$email->to(array($user_to_send_restore_link['User']['email']));
$email->from(array(GENERAL_FROM_EMAIL => 'seqrd support team'));
$subject = 'Restore password link';
$email->subject($subject);
$email_data = array(
'hash' => $user_to_send_restore_link['User']['hash']);
$email->viewVars($email_data);
return $email->send();
}
app/View/Emails/html/restore_password.ctp
<p> Please, follow link <?php echo $this->Html->link('restore password link', Router::url(array('controller' => 'users', 'action' => 'restore_password_form', $hash), true)); ?> to restore password</p>
app/View/Layouts/Emails/html/emaillayout.ctp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title><?php echo $title_for_layout;?></title>
</head>
<body>
<?php echo $this->fetch('content');?>
</body>
</html>
Theme it's next step of abstraction, there you can fast change whole styles of all emails, but not change code significantly.
Notes: viewVars method pass variables not only into template, but in email layout too.

Trouble getting Cake PHP JsHelper to work

I seem to be beaten at the very first hurdle on this one, I can't seem to get a basic "Hello world" going on in cake PHP.
in /app/Controller/MyController.php I have:
public $helpers = array('Js' => array('Jquery'), 'Html', 'Form');
public $components = array('RequestHandler');
in /app/View/Layouts/default.ctp I have:
echo $this->Html->script('//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');
echo $this->fetch('script');
echo $this->Js->writeBuffer();
?></head>
in /app/View/My/index.ctp I have:
$this->Js->alert('HelloWorld');
but I get no alerts! If I try:
echo $this->Js->alert('HelloWorld');
it prints out to the browser: (double quotes instead of the typed single quotes!?)
alert("HelloWorld");
but not wrapped in < script > tags or even a $(document).ready(function(){});
Have I missed something out?
JsHelper is pretty useless to be honest. I don't even bother with it properly, I just include JavaScript in my site as I would normally, but using Cake's methods to keep it within the framework.
Example layout would have jQuery included in the <head> like this:
app/View/Layouts/default.ctp
<head>
<?php
// Include jQuery
echo $this->Html->script('jquery-1.8.2.min');
// Want to send some glabal values to your scripts?
$this->Js->set(array(
'TEST' => 'Hello World',
'ROOT' => $this->Html->url( '/', true)
));
echo $this->Js->writeBuffer(array('onDomReady' => false));
// Include any other scripts you've set
echo $this->fetch('script');
?>
</head>
Then in your view, you might want to include a specific script for that page:
app/View/Pages/test.ctp
<?php echo $this->Html->script('test.js'); ?>
And you just keep all your JavaScript as usual in an external script:
app/webroot/js/test.js
$(document).ready(function() {
/**
* Alert the value we set in our layout. All JS vars that have been
* set are available in your JavaScript via the window.app object.
*/
alert(window.app.TEST + ' sent from ' + window.app.ROOT);
});
The general consensus is don't bother using the JsHelper, and is likely being removed in Cake 3 in any case.
I would put echo $this->Js->writeBuffer(); before </body>
I am not sure on the exact workings but I think your JS is working with echo because it is being output in the middle of your view, so when the page loads, it alerts. (The JS will be in the middle of your HTML output) rather than in the <head> or before </body>
The writeBuffer is being called before the view file is processed; so your view js does not get added to the buffer. I could be incorrect however.
Set the write buffer at the end of each ctp file and it should work

Pass JSP Input with href link

I want to pass input data with a href instead of a button. The problem is I am sending an array, my for loop the input data is being stored so It creates multiple links. What is the course of action to take to fix this.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Firstjsp</title>
</head>
<body>
<% String locations[] = {"Loan 1", "33.890542", "151.274856", "Address 1","true", "-35404.34"};
for (int i =0; i<locations.length; i++)
{
%>
<form name="submitForm" method="POST" action="Mapper.jsp">
<Input type = "Hidden" name = "loc" value = "<%= locations[i] %>">
View Map
</form>
<%
}
%>
</body>
</html>
The HTTP request query string takes the form of name1=value1&name2=value2&name3=value3. So all you need to do is converting the String[] to a String in exactly that format. Additional requirement is to use URLEncoder to encode the names and values so that any special characters are been converted to %nn format for proper usage in URLs.
This should do:
StringBuilder builder = new StringBuilder();
for (String location : locations) {
if (builder.length() > 0) builder.append("&");
builder.append("loc=").append(URLEncoder.encode(location, "UTF-8");
}
String locationsQuery = builder.toString();
Then you can specify it in the link as follows:
View Map
How to obtain it in the other side has already been answered in your previous question.
Unrelated to the concrete problem, writing raw Java code in JSPs is officially discouraged since a decade. You can achieve the same on a more easy manner with JSTL <c:url>, <c:param> and <c:forEach>. Here's a kickoff example assuming that you've done a request.setAttribute("locations", locations) in your preprocessing servlet or in top of JSP:
<c:url value="Mapper.jsp" var="mapperURL">
<c:forEach items="${locations}" var="loc">
<c:param name="loc" value="${loc}" />
</c:forEach>
</c:url>
View Map

Pass Array .jsp to .jsp

I am wondering how to pass an array or a list from one .jsp page to another. I want to then take the values from this array and assign them to a javascript array. I think I have the source jsp page configured correctly, but was wondering how to get the values in the second .jsp page.
This is my source .jsp file:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Firstjsp</title>
</head>
<body>
<Form Method = "Post" Action = "Mapper.jsp">
<% String locations[] = {"Loan 1", "33.890542", "151.274856", "Address 1","true", "-35404.34"};
for (int i =0; i<locations.length; i++)
{
%>
<Input type = "Hidden" name = "loc" value = "<%= locations[i] %>">
<%
}
%>
</Form>
</body>
</html>
You can get them using HttpServletRequest#getParameterValues(). This returns a string array of all parameter values which have the same parameter name. In your case, you have generated several hidden HTML input elements with the same name loc in the first JSP, so the following in the second JSP (or, preferably, a servlet) should do:
String[] locations = request.getParameterValues("loc");
The easiest way i think is to put the variable (it works even as a pointer) as a session variable.This way you can access it everywhere as long as the code is running under the same session.
<%
String name = request.getParameter( "username" );
session.setAttribute( "theName", name );
%>
This example also uses request.The difference is that session stands out.For example if you set a variable in session then even if you close the browser and restart it , it will still be there.Think of it as a global variable.Request is best used when you send data from one jsp/servlet to another jsp/servlet.It basically has a lifespan of 1.The moment when you redirect a page it disappears.
You can use session.setParameter(name_of_the_variable,the_variable) and session.getParameter(name_of_the_variable). As a hint that was useful to me,always make sure that you test the returned parameter, in the case above "name" if it's not NULL.If there is no variable on the session with that name, it will return NULL, and most likely crash.Hope this helps!

HTML Input field force numbers

Is it possible to create an input field that sets the default input character set to numbers on a mobile phone (so the NUMERICAL KEYBOARD POPS UP)?
For example to make it easier type in a telephone number into a HTML form.
To make inputing numbers easier, use <input type="number">. To make entering phone numbers easier, use <input type="tel">. Not all phone will support them, but the iPhone at least will give you a numeric keypad by default instead of the normal keyboard. See the spec and Dive Into HTML5 for more information.
It is possible to limit entry on a "mobile phone"
The mobile phone form entry uses
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
"http://www.wapforum.org/DTD/wml_1.1.xml">
input here can be limited using format="*N"
You can use <input type='tel'>. This is a new HTML5 feature. Older browsers will simply default to a text input field.
So you can use either type="tel" or type="numbers".
The difference is that one tries to bring up your phone dial keyboard and other simply switches to the numbers input of your mobile keyboard.
Please see my project of the cross-browser filter of value of the text input element on your web page using JavaScript language: Input Key Filter . Code example:
<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Input Key Filter Test</title>
<meta name="author" content="Andrej Hristoliubov anhr#mail.ru">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<!-- For compatibility of IE browser with audio element in the beep() function.
https://www.modern.ie/en-us/performance/how-to-use-x-ua-compatible -->
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<link rel="stylesheet" href="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.css" type="text/css">
<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/Common.js"></script>
<script type="text/javascript" src="https://rawgit.com/anhr/InputKeyFilter/master/InputKeyFilter.js"></script>
</head>
<body>
<h1>Phone number</h1>
Please type a phone number in the +**(***)***-**-** format. Example: +1(23)456-78-90
<br/>
<input id="PhoneNumber" value="+()--">
<script>
function getArrayPhoneNumber(value){
if (typeof value == 'undefined')
value = document.getElementById("PhoneNumber").value;
return value.match(/^(\+?\d*)\((\d*)\)(\d*)-?(\d*)-?(\d*)$/);
}
function getPhoneNumber(){
var arrayPhoneNumber = getArrayPhoneNumber();
if(!arrayPhoneNumber)
return "";
var phoneNumber = arrayPhoneNumber[1] + arrayPhoneNumber[2] + arrayPhoneNumber[3] + arrayPhoneNumber[4] + arrayPhoneNumber[5];
return phoneNumber;
}
inputKeyFilter.Create("PhoneNumber", function(event){//onChange event
inputKeyFilter.RemoveMyTooltip();
var arrayPhoneNumber = getArrayPhoneNumber();
if(!arrayPhoneNumber || (arrayPhoneNumber.length != 6)){
document.getElementById("NewPhoneNumber").innerHTML = "Incorrect format of the phone number";
return;
}
var elementNewPhoneNumber = document.getElementById("NewPhoneNumber");
var phoneNumber = getPhoneNumber();
if(inputKeyFilter.isNaN(phoneNumber, this)){
elementNewPhoneNumber.innerHTML = "";
return;
}
elementNewPhoneNumber.innerHTML = phoneNumber;
}
, function(elementInput, value){//customFilter
var arrayPhoneNumber = getArrayPhoneNumber(value);
if(arrayPhoneNumber == null){
inputKeyFilter.TextAdd(isRussian() ?
"Недопустимый формат телефонного номера. Например: +1(234)56-78-90"
: "Incorrect format of the phone number. Example: +1(234)56-78-90"
, elementInput);
if(elementInput.value == "")
elementInput.value = elementInput.defaultValue;
return false;
}
return true;
}
//onblur event. Use this function if you want set focus to the input element again if input value is NaN. (empty or invalid)
, function(event){ inputKeyFilter.isNaN(parseInt(getPhoneNumber()), this); }
);
</script>
New phone number: <span id="NewPhoneNumber"></span>
</body>
</html>
Also see my page "Custom filter:" example of the input key filter
Here's an example with Javascript. This will only allow numbers from the numpad/numbers on top of the keypad, and formatters (shift/backspace/etc). You may also consider adding a setTimeout(), with a couple seconds timeout, for the onchange event to check in case someone pastes non numbers into the field as well as server side validation.
Example
http://jsfiddle.net/vce9s/
for my testing of "you can use either type="tel" or type="numbers"."
on iPhone type="tel" brings up the numbers only keypad (like phone) and type="numbers" brings up the numeric keypad switched to numbers and symbols, so both work, just depends on your application. For me I only need numbers so I used type="tel" for ease of use and it worked great!

Resources