I am using jcart http://conceptlogic.com/jcart/ to create some cart functionality in wordpress.
I am trying to access a jcart function within another function in my functions.php file.
foreach($jcart->get_contents() as $item) {
$psitems[] = $item['id'];
}
This code works fine on functions.php itself or on any template page-- it creates the $psitems array so I can check if that array contains certain values.
Now, within the following function:
function gallery_shortcode_fancybox($attr) { ...
I have the following:
foreach($jcart->get_contents() as $item) {
$psitems[] = $item['id'];
}
foreach ( $attachments as $id => $attachment ) {
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_image($id, 'full', false, false) : wp_get_attachment_image($id, 'full', true, false);
$output .= "<div class='property'>";
$output .= "$link";
$output .= '
<div class="property-details">
<div class="property-details-inner">
<form method="post" action="" class="jcart">
<fieldset>
<input type="hidden" name="jcartToken" value="' . $_SESSION['jcartToken'] . '" />
<input type="hidden" name="my-item-id" value="' . $id . '" />
<input type="hidden" name="my-item-name" value="Photo #' . $id . '" />
<input type="hidden" name="my-item-url" value="' . wp_get_attachment_url( $id ) . '" />
<input type="hidden" name="my-item-qty" value="1" size="3" />';
if(in_array($id,$psitems)) {
$output .= '<span class="action terminal pull-sheet">Image in Pullsheet</span>'
} else {
$output .= '<input type="submit" name="my-add-button" value="Add to Pull Sheet" class="action terminal pull-sheet" />';
}
$output .= '</fieldset>
</form>
</div> <!-- property-details-inner -->
</div>';
$output .= "</div>";
}
return $output;
I get the following error:
Call to a member function get_contents() on a non-object
If I try placing the original foreach($jcart->get_contents() as $item) { ... code outside of the function, I get the following error:
Warning: in_array() expects parameter 2 to be array, null given in...
How can I access this array inside the function and avoid these errors?
Thanks!
Regarding the first error, it means that you have to initialize $jcart.
As for the warning, try this:
$array = $jcart->get_contents();
foreach ($array as $item) ...
Related
This is my code:
public function settings_inline_style_callback() {
$type = esc_html( $this->options['inline_style'] );
$temp0 = '<input type="radio" name="My_options[inline_style]" id="inline_style_';
$temp1 = '<label for="inline_style_';
$html = $temp0 . '0" value="0" ' . checked( $type, '0', false ) . ' />';
$html .= $temp1 . '0">External CSS style</label><br />';
$html .= $temp0 . '1" value="1" ' . checked( $type, '1', false ) . ' />';
$html .= $temp1 . '1">Inline CSS style</label>';
echo $html;
}
The WordPress plugin review team said the escape was not done properly. They said you have to escape WHEN you echo. Not when you save to the variable. (in this line: echo $html;)
I am new to WordPress and PHP. I do not understand the problem with this code.
How should I modify this code?
Thank you very much for your help.
So this is because the esc_html function only works when running during echo. Below is a way you can do your code.
printf(
'<input type="radio" name="My_options[inline_style]" id="inline_style_0" value="0" %s/>
<label for="inline_style_0">External CSS style</label><br />
<input type="radio" name="My_options[inline_style]" id="inline_style_1" value="1" %s/>
<label for="inline_style_1">Inline CSS style</label>',
checked(esc_html($this->options['inline_style']), '0', false),
checked(esc_html($this->options['inline_style']), '1', false),
);
In my jquery code I have:
$("#show").append("<img src=" +attachment.url+" alt="+attachment.alt+" title="+attachment.title+" description="+attachment.caption+" class='img-responsive img-thumbnail'/><input type='hidden' name='my_image_URL[]' value="+attachment.url+"></span>");
My jquery code adds fields for news selected images and fills inputs names my_image_URL[].
In PHP:
if ( isset( $_POST['my_image_URL'] ) ) {
$urls = $_POST['my_image_URL'];
echo '<input type="hidden" name="imagens_home" value="'.$urls.'"/>';
}
I trying to add $urls array in the hidden input.
And after, if its works ok:
<?php
if ($urls != '' ) {
foreach ($urls as $url) {
?>
<img src="<?php echo $url;?>" class="img-responsive img-thumbnail " />
<input name="my_image_URL[]" value="<?php echo $url;?>"/>
</div>
<?php
};
}
?>
But this part of code not fill the input:
if ( isset( $_POST['my_image_URL'] ) ) {
$urls = $_POST['my_image_URL'];
echo '<input type="hidden" name="imagens_home" value="'.$urls.'"/>';
}
---------- UPDATE --------
options.php
register_setting(
'tema-setting-group',//string $option_group
'imagens_home' //string $option_name
//calback $sanitize_calback
);
---
add_settings_field(
'home-imagens-top',//string $id
'Imagens',//String $title
'tema_home_imgs',//string $calback
'opcoes_do_tema',//string $page
'tema-home-options'//string $section
//string $args
);
//calback
function tema_home_imgs(){
$urlsImagens = esc_attr( get_option( 'imagens_home' ) ); // RETURN DB DATA
include( get_template_directory() . '/inc/templates/selecao_imagens.php');
if ( isset( $_POST['my_image_URL'] ) ) {
$urls = $_POST['my_image_URL'];
echo '<input name="imagens_home" value="'.$json_encode($urls).'" style="width:300px"/>';
}
}
selecao_imagens.php
<input id="my_upl_button" type="button" value="Escolher Imagens" /><br/>
<div class="row">
<div id="exibe" class="sortable">
<?php
$urls = json_decode($urlsImagens, true);
if ($urls != '' ) {
foreach ($urls as $url) {
?>
<img src="<?php echo $url;?>" class="img-responsive img-thumbnail " />
<input name="my_image_URL[]" value="<?php echo $url;?>"/>
<?php
};
}
?>
</div>
</div>
theme_options.php
<?php settings_errors();?>
<form method="post" action="options.php">
<?php settings_fields ('tema-setting-group'); ?>
<?php do_settings_sections (
'opcoes_do_tema'//string $page
); ?>
<?php submit_button ();
?>
</form>
-------- UPDATE 2 -------
I tried:
echo '<input name="imagens_home" value="' . htmlspecialchars(json_encode($urls)) . '" />';
but it is not yet filling in the input.
I also tried only:
If (isset ($ _POST ['my_image_URL'])) {
Print_r ($ _ POST ['my_image_URL']);
}
But after the submit does not appear anything on the screen, in the form correctly saves all other inputs except what I am trying to save the array, if I put some manual information goes ok. But I do not understand why it is not capturing the my_image_URL [] names of each image input. The action in form is like this:
<Form method = “post” action = “options.php”>
I’m using the Settings API
Thanks
I trying to add $urls array in the hidden input.
If I understand, you're trying to store an array value in a hidden input, so you can retrieve it later. Problem is, this doesn't work...
echo '<input type="hidden" name="imagens_home" value="'.$urls.'"/>';
...because when you echo a PHP array all you get is the string Array, not the actual array contents.
You could turn the array into a json string though:
echo '<input type="hidden" name="imagens_home" value="'.$json_encode($urls).'"/>';
Now your hidden input has a regular string. Later, when the form is POSTed, you could retrieve it:
$urls = json_decode($_POST['imagens_home'], true)
I am trying to achieve dynamic $row values from an array, and show them as output. It keeps showing nothing while the values are already there.
This is what I have so far, where:
$row["lg_".$val.""]; should return:
$lg_it
'it' is the $val from the array.
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row["lg_".$val.""];
<input type="text" name="lg_$val" value="$shrtKey">
}
Anyone an idea?
What you have should result in a syntax error. Try the following:
<?php
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row['lg_'.$val];
?>
<input type="text" name="lg_<?= $val ?>" value="<?= $shrtKey ?>">
<?php
}
You've missed to echo your input-field:
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row["lg_".$val.""];
echo '<input type="text" name="lg_' . $val .'" value="' . $shrtKey . '">';
}
Further, if you don't use the array key in the foreach-loop, you can omit the $key =>-part and just write
foreach($arrMapCookieToLang as $val) {
// ...
}
Instead, the
<input type="text" name="lg_$val" value="$shrtKey">
Maybe you should use the
echo "<input type=\"text\" name=\"lg_" . "$val\" value=\"$shrtKey\">";
I want to grab the value of a field using $_POST, manipulate it, then pass the value back to the same page to the same field before the PHP code manipulates it.
If I put the PHP code after the field, it manipulates the code, reloads the page but doesn't put the manipulated code back into the field.
if (!isset($input)) {
$input = '';
}
echo '<form id="testform" method="post" action="">';
echo '<input type="text" name="inputText" value="' . $input . '">';
echo '<button type="submit" name="button"> Button </button>';
echo '</form>';
$input = $_POST['inputText'];
if(isset($_POST['inputText'])) {
$input = $input . ' manipulated';
}
echo $input; //test
If I put the PHP code before the field, it can't find the field to manipulate the value...
if (!isset($input)) {
$input = '';
}
$input = $_POST['inputText'];
if(isset($_POST['inputText'])) {
$input = $input . ' manipulated';
}
echo $input; //test
echo '<form id="testform" method="post" action="">';
echo '<input type="text" name="inputText" value="' . $input . '">';
echo '<button type="submit" name="button"> Button </button>';
echo '</form>';
Obviously the first approach is more correct, but how do I pass the $input variable to the field before the rest of my PHP manipulation code executes?
I tried $_POST['inputText'] = $input as a desperate attempt but nothing..
Well, from what I've understood in your explanation, you want to change the input value to something else and show it in he same field. If that's correct, you may want to do this:
<form id="testform" method="post" action="">
<input type="text" name="inputText" value="<?php echo ( isset($_POST['inputText']) ) ? sprintf( '%s manipulated', $_POST['inputText'] ) : ''; ?>">
<button type="submit"> Send </button>
</form>
Let me know if that's what you wanted. Regards !
Try
$input = isset($_POST['inputText']) ?$_POST['inputText'] :'';
in the begining instead of
if (!isset($input)) {
$input = '';
}
I need to create a function that will merge an array into a template that I have created please see below :
My array:
$view_array = array();
$view_array['start.form'] = '<form method="POST" >';
$view_array['end.form'] = '</form>';
$view_array['name.input'] = '<label>Name : </label><input type="text" name="frm_name" />';
$view_array['name.input'] = '<label>E-mail: </label><input type="text" name="frm_email" />';
$view_array['submit.form'] = '<input type="submit" value="Submit" />';
My template:
$content = "
~error.messages~
~start.form~
~name.input~
~email.input~
~submit.from~
~end.form~";
foreach($view_array AS $key=>$value)
{
$content = str_replace('~'.$key.'~',$value,$content);
}
echo $content;