PHP variable not returned in array - php

I am trying to place a variable inside a PHP array which will work with a Wordpress plugin. Here is the code I have:
function custom_list( $lists ) {
$fil = "Dump";
$new_lists = array(
'ddl-list-block' => array(
'name' => __( 'Download Block', 'delightful-downloads' ),
'format' => "
<article class=\"ddl-list-block ddl-list-item\" id=\"ddl-%id%\">
<div class=\"download-wrap\" style=\"background-image: url('fi');\">
<div class=\"download-item\">
<div class=\"download-details\">
<h2>%title%</h2>
".$fil." /* VARIABLE SHOULD APPEAR HERE */
<div class=\"download-meta\">
<div class=\"download-meta-data\"></div>
Download
</div>
</div>
</div>
</div>
</article>
"
),
'ddl-list-plain' => array(
'name' => 'Flain List',
'format' => '<i class="fa fa-download"></i>%title% - %date%'
)
);
return $new_lists;
}
add_filter( 'dedo_get_lists', 'custom_list' );
I have tried using ., {, single quotes and double quotes, but I can not get the word "Dump" to display where it is supposed to. The reason I am trying to get it to show the word "Dump" is just to make sure that variables can be passed through into an array, because I am using another Wordpress plugin (Simple Fields) to manage extra fields in my post types and I want to pull out one particular aspect of the field (the URL of an uploaded image), so I will be inserting something like $my_field['url']. But none of the options I am aware of have even inserted a simple string into my array. Is there something I am missing?

I am posting this solely because it answers my problem. Not necessarily because it answers the question. I guess that's because of the plugin that I used which answers to that function.
The variable I was using called a featured image, but it could not be fed into the function. As a solution, I used their dedo_search_replace_wildcards filter and created another wildcard (I think I used %fi% or something like that), which gave me the outcome I wanted. Shame it didn't feed native $ variables into it.

Related

Wordpress page title fxn in echo p

This is probably super simple but I just cant seem to figure it out.
How can I pass the current Wordpress page title into this code?
Below is a snippet from Formidable Forms WP plug-in which basically prints statistics from forms within my website.
In this case, the # of entries for a specific form (55jqi) and a specific field(50) are displayed on the pages, showing how many other people also submitted that form.
Im trying to skip needing to update each page (4,380 pages) with the stats output snippet.. and instead call the current page into the stats display code, to show stats for that particular page being viewed.. using an elementor custom post type template.
I need this:
echo FrmProStatisticsController::stats_shortcode(array('id' => '55jqi', 'type' => 'count', 50 => 'runtz'));
To work like this:
echo FrmProStatisticsController::stats_shortcode(array('id' => '55jqi', 'type' => 'count', 50 => 'single_post_title();'));
Replace the input area ‘Runts’ with the current page title, using
single_post_title()
Or similar.
Any help would be amazing!!
There is also a short code available which works the similarly.
[frm-stats id=55jqi type=count 50="Runtz"]
Formidable Forms Plugin shortcode and php page for reference: https://formidableforms.com/knowledgebase/add-field-totals-and-statistics/#kb-field-filters
You are using single_post_title function in a wrong way. By default, this function will echo the output but in the shortcode, you need to return the output.
This function accepts 2 param: 1: $prefix 2: $display
You need to pass $display as false, which will tell the function to return the value.
so you'll have to make a call like this `single_post_title( '', false )``
and your shortcode call will be like this:
echo FrmProStatisticsController::stats_shortcode(
array(
'id' => '55jqi',
'type' => 'count',
'50' => single_post_title( '', false ),
)
);

What php to use to be able to pick an image from the Wordpress library

This is the original php code:
function lovethemes_pricing_shortcode($atts)
{extract(shortcode_atts(array(
"heading" => '',
"price" => '',
"link" => '',
"name" => '',
), $atts ) );
return '<div class="pricing animated bounceIn">
<section class="head">'.heading.'
<section class="content">
<ul><li class="price">'.$price.'</li>
<li>'.$name.'</li>
</ul></section> </div>';}
add_shortcode('pricing', 'lovethemes_pricing_shortcode');
This is what is written on my page in the backend:
[pricing heading="7 februari 2015"
price="Strak bekleden van een taart"
link="/agenda/workshops/strak-bekleden-van-een-fondant-taart"
name="Info"]
Resulting in this:
http://www.alexandra-van-zandbergen.be/wp-content/uploads/2015/08/Schermafbeelding-2015-08-08-om-13.23.38.png
And what I would like to have is this:
http://www.alexandra-van-zandbergen.be/wp-content/uploads/2015/08/Schermafbeelding-2015-08-08-om-13.22.47.png
I can read and write html and css but no php... Can anyone help me with this please? I've tried to add several codes and nothing is working.
I have to be able to change the image in the backend, I would like to be able to just choose an image from the Wordpress image gallery.
Thanks in advance!
Alexandra
I'm not very familiar with it either, and would need to try it to see if it works, so pls don't shoot me if it does not. However, this function makes use of the shortcode API.
The code tells to create a [pricing] shortcode, currently with 4 attributes. So, if all the rest works, then you most likely can simply add another one.
What I do not understand - and I'm not taking the time to look it up - is why all variables in your code start with $, but not the heading. I'll ignore that since it doesn't seem to be the problem.
Note:
It's best to add:
the image size to the HTML for performance. I'm assuming that you'll
use images of the same size. If you would want to set the image
size, you can create attributes for it too.
an alt text for SEO. Below I use the price attribute.
While I'm at it I would suggest to add a link to the image and title too. People sometimes click on it instead of on the info button. This would make it more user friendly.
So, it then would look like this:
function lovethemes_pricing_shortcode($atts) {
extract(shortcode_atts(array(
"heading" => '',
"price" => '',
"link" => '',
"name" => '',
"img" => '',
), $atts ) );
return '<div class="pricing animated bounceIn">
<section class="head">'.heading.'
<section class="content">
<ul><li class="price">'.$price.'</li>
<li><img src="'.$img.'" alt="'.$price.'" width="247" height="186" /></li>
<li>'.$name.'</li>
</ul></section></div>';
}
add_shortcode('pricing', 'lovethemes_pricing_shortcode');
and your shortcode would be:
[pricing heading="7 februari 2015"
price="Strak bekleden van een taart"
link="/agenda/workshops/strak-bekleden-van-een-fondant-taart"
img="/your/image/url.jpg"
name="Info"]
To make it a little more user friendly, you could also rename the attributes to something that corresponds to the content, f.ex. heading > date
I hope this helps!

Codeigniter, loading views inside views, variables doesn't reset each time

I'm using the latest stable version of Codeigniter.
Here's part of the controller:
$data = array(
'tracks' => $this->tracks_model->get(NULL, 'start_date'),
'longest_distance' => $this->tracks_model->get(10, 'distance'),
'longest_time' => $this->tracks_model->get(10, 'moving_time')
);
$this->load->view('statistics_view', $data);
and here's part of the code inside the file statistics_view.php:
<section>
<h3>Section 1</h3>
<?php $this->load->view('podium_view', array('tracks' => $longest_time, 'unit' => 'ore', 'function' => 'minutes_to_hours')) ?>
</section>
<section>
<h3>Section 2</h3>
<?php $this->load->view('podium_view', array('tracks' => $longest_distance, 'unit' => 'km')) ?>
</section>
I've noticed that in the 2nd loaded view (inside section 2), the variable $function is still set from section 1, and the other values (traks, unit) are the one I passed.
Why the variable function is still set in the second view?
Shouldn't be reset?
In Codeigniter 3 you can also reset variables in view using method
$this->load->clear_vars();
See it in documentation
Codeigniter uses extract in order to convert the array of variables you are passing
see http://php.net/manual/en/function.extract.php
Extract keep adding variable to the symbol table and the view is just included as a PHP file.
So the variable scope is the same and you will have access to all the variables defined early. Default behaviour of extract is it overrides if a conflict is found.
If you want to reset, you will have to pass some value.
<?php $this->load->view('podium_view', array('tracks' => $longest_distance, 'unit' => 'km', 'function'=>'')) ?>

How do I set the 'value' in my Zend form checkbox?

The Zend Form is proving to be a bit tricky for me, even as much as I am working with it lately...
I have this form and I am attempting to dynamically create the several checkboxes for it. It is working out alright, except I cannot seem to get the 'value' attribute to change.
In my Zend form class I have this snippet...
// psychotic symptoms
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
));
In my view (phtml) I am calling it like this...
<div class="element">
<?php // Psychotic Symptoms
$Criteria = new Criteria();
$Criteria->add( DictionaryPeer::CATEGORY, 'MAR: Psychotic Symptoms' );
$Criteria->addAscendingOrderByColumn( 'Ordinal' );
$this->PsychoticSymptomsList = DictionaryPeer::doSelect( $Criteria );
foreach( $this->PsychoticSymptomsList as $Symptom ) {
$form->psychoticsymptom->setValue($Symptom->getDictionaryId());
$form->psychoticsymptom->setAttrib('name', $Symptom->getWord());
echo $Symptom->getDictionaryId(); // prove my id is coming through... (it is)
$form->psychoticsymptom->getDecorator('label')->setTag(null);
echo $form->psychoticsymptom->renderViewHelper();
$form->psychoticsymptom->setLabel($Symptom->getWord());
echo $form->psychoticsymptom->renderLabel();
echo '<br />';
}
?>
</div>
Everything seems to be working fine, except the value attribute on each checkbox is rendering a value of '1'. I have tried moving the 'setValue' line to several different positions, as to set the value before the form element renders but I am having no luck getting this to work. It's worth any effort to me because I need to do the same type of operation in many areas of my application. I would have done this a bit different too, but I am re-factoring another application and am trying to keep some things unchanged (like the database for instance).
Any help is much appriciated
Thanks
you can try to overwrite the "checkedValue" and "uncheckedValue". check this reference
$this->addElement('checkbox', 'psychoticsymptom', array(
'label' => 'psychoticsymptom',
'name' => 'psychoticsymptom',
'checkedValue' => 'checked Value',
'uncheckedValue' => 'unchecked Value'
));
You seem to only have one psychoticsymptom element "checkbox" which your adding (changing) the value too for each $this->PsychoticSymptomsList.
Maybe you would be better off using a multicheckbox element instead.

Drupal rendering order - how to define order of rendering?

I'm building a site with Drupal and I have a small problem. I'm rendering a form using hook_menu. The form renders fine and all is good. I am then adding some more markup to the page using hook_page_alter(). Hook_page_alter looks like this:
function renderer_page_alter(&$page) {
if(drupal_is_front_page()) {
$q = db_query('SELECT name, mname, number_of_instances FROM {event_type} ORDER BY number_of_instances DESC');
foreach($q as $event) {
$options['name'][] = $event->name;
$options['mname'][] = $event->mname;
}
$tri = array(
'#theme' => 'frontpage_canvas',
'#options' => $options
);
return $page['content']['triangles'] = $tri;
}
}
So in my local MAMP install, the content is displayed with the results from hook_page_alter() first then followed by the form. However, in the remote install, the order is reversed (with the submit button for the form at the top of the page and the rest of the content beneath it). The only difference between the installs (that I can think of) is that the remote install is Drupal 7.8 and the local one is Drupal 7.9.
I would like to have the remote install in the same way as the local one. Has anyone come across an issue like this before?
The basic structure of the rendered HTML is like this:
<div class=content>
//all the form information is in here
</div>
<div class=rendered>
//all the output from hook_page_alter() is here
</div>
EDIT: The issue is that for some reason, block.tpl.php is adding two divs:
<div id="block-system-main" class="block block-system first last odd">
<div class="content">
//My form markup is in here
</div>
</div>
//The hook_page_alter markup is in here.
So, is there any way to force Drupal to add the hook_page_alter markup to the same div as the form is being rendered in? Because the way it's being rendered at the moment, the #weight property doesn't affect the positioning.
Thanks,
The display order in render arrays is set using the #weight attribute, elements with a higher #weight will be rendered after those with a lower value.
If you want to force the content added in hook_page_alter() to be rendered at the top of the content area declare it like this:
$tri = array(
'#theme' => 'frontpage_canvas',
'#options' => $options,
'#weight' => -1000
);
or at the bottom of the content area:
$tri = array(
'#theme' => 'frontpage_canvas',
'#options' => $options,
'#weight' => 1000
);
You can also adjust the #weight for other elements that already exist in the $page array passed in to the function, so you have total control over the display order.

Categories