Joomla setMetada and variables - php

We are building a custom component , for seo improve, and we are working now with joomla "setMetaData" function Docs -> here
The normal sintaxis is:
$doc->setMetaData( 'tag-name', 'tag-content' );
But we need to put a variable in "tag-content" , we use this code, but doesn´t work:
$doc->setMetaData( 'DC.Title', echo($params->get('page_description')) );
And this doesn´t work as previus:
$doc->setMetaData( 'DC.Title', $title );
Also we now that "tag-content" only accepts Strings, any idea to solve this?
Thanks for the help and you time.

You don't need to echo it. It is function parameter.
Use this:
$doc->setMetaData( 'DC.Title', $doc->getDescription() );

First, did you get the $doc object using?
$doc = JFactory::getDocument();
You don't need to echo the get function for $params.The following should work assuming that the $params object is initialized and not empty.
You can also pass a default value to the get function for testing:
$doc->setMetaData( 'DC.Title', $params->get('page_description' , 'defaultValue') );
This will return 'defaultValue' if 'page_description' is empty.

Related

creating a variable shortcode based on user input that returns (prechosen) variables Wordpress

I need to update values in all posts of a wordpress installation on a regular base. I was gooing to use shortcodes to insert the request into the wordpress post. Then use a custom functions.php that holds all the variables that need to be updated from time to time.
I got it working. Somehow but not the way I intended to use it. I'm a total beginner. Please consider this when answering my questions.
I want to have a function that reads what comes after honda_ and displays the correct value in wordpress without having to create a separate shortcode for each variable.
When entering [honda_link] wordpress should display the value from honda_link. When entering [honda_longlink] the value from honda_longlink variable should get displayed. I don't want to create a shortcode for each value.
I came up with this as a working solution...
// Honda
function honda() {
$honda_link = 'www.honda.com';
$honda_longlink = 'http://www.honda.com';
$honda_free = 'Free';
$honda_new = '23.688 $';
$honda_mileage = '00';
return $honda_link;
}
add_shortcode('neu', 'honda_link');
I tried some approaches by using an array but it ultimately failed all the time. I also tried it with if statements but wasn't able to get the right value displayed.
Someone willing to help a noob? I think I need to see a working example in order to understand it. The code snippets I have been looking at (that do something similiar but not the same I want to achieve) did confuse me more than they helped me.
I came up with this / Which works in a way but... This isn't very comfortable to use.
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
shortcode_atts(array(
'var1' => 'default var1',
'var2' => false,
'var3' => false,
'var4' => false
), $atts);
if ($atts['var2'])
return 'honda2';
else if ($atts['var3'])
return 'honda3';
else if ($atts['var4'])
return 'honda4';
else
return 'honda1';
}
So now when using:
[HONDA var1="novalue"][/HONDA]
[HONDA var2="novalue"][/HONDA]
[HONDA var3="novalue"][/HONDA]
[HONDA var4="novalue"][/HONDA]
it shows:
honda1
honda2
honda3
honda4
and so on.
Is there a better way to achieve the intended goal from post #1 ? Any way I could import the $variables from 1st post in bulk for example?
I don't have a working WP setup right now to test, but could you try this:
add_shortcode('HONDA','HONDA_TEST');
function HONDA_TEST($atts = array(), $content = null, $tag){
$atts = shortcode_atts(array(
'model' => 1,
), $atts);
$myHondas = [1 => 'honda1', 'honda2', 'honda3'];
return isset($myHondas[$atts['model']]) ? $myHondas[$atts['model']] : 'unknown model id';
}
And use it with [HONDA model="1"], [HONDA model="2"]

Unable to put php code in return of shortcode

I need to hide the URLs of downloads in wordpress posts. I have found a great script to do this but it is not a plugin. I have installed the script and created a function to include it. I am not a pro with php at all.
However the script has a line to normally call it:
<a href="<?php downloadurl('http://yourdomainname.comdownloadables.zip','veryspecials'); ?>" >Your Downloadables</a>
I am not able to place this directly in posts so I am trying to make a shortcode for it, but I am getting stuck. The shortcode that I have is:
function secshort_func($atts, $content = null) {
extract(shortcode_atts(array(
"linkurl" => '#Download_Does_Not_Exist',
"linktitle" => 'Download',
), $atts));
return '<a href="<?php downloadurl(' .$linkurl. ','veryspecials'); ?>" >' .$linktitle. '</a>';
}
add_shortcode( 'secdown', 'secshort_func' );
I am getting errors when trying to run this, and through a process of elimination I know that it is from this part of the return code:
"<?php downloadurl(' .$linkurl. ','veryspecials'); ?>"
After searching the internet for solutions and trying everything that I can think of, I am completely stuck.
Any help would be very much appreciated - it is driving me crazy being stuck on such a little thing!
A few observations along with the answer:
Format your code. It makes life much easier when troubleshooting. Good indenting is huge (see below for your code, formatted).
Don't use cryptic / abbreviated function names. Type them out so that you create self documenting code.
It's better to not use extract. There are some who say it's OK, but it can create confusing code that is hard to troubleshoot because you don't know where a variable came from. It's preferable to explicitly set the variables. (In your case, because you use them only once, it's simplest to just reference them in the array form - $atts['link_url'])
You can call the function, but it has to be concatenated into the string (see below). Your code (and the other answer), are passing php into the string, rather than calling the function and passing the result into the string.
Formatted code, with answer:
// Use a clearer function name. No need for "func", that's implied
function download_link_shortcode($atts, $content = NULL) {
// Declare $defaults in a separate variable to be clear, easy to read
$defaults = array(
"link_url" => '#Download_Does_Not_Exist',
"link_title" => 'Download',
);
// Merge the shortcode attributes
$atts = shortcode_atts( $defaults, $atts );
// Concatenate in the results of the `download` function call....
return '' . $atts['link_title'] . '';
}
add_shortcode( 'secdown', 'download_link_shortcode' );
Try using double outer quotes and escape inner single quotes like this:
return "<a href=\'<?php downloadurl(\'" . $linkurl . "\',\'veryspecials\'); ?>\' >" .$linktitle. '</a>';

How to submit an outside variable to Gravity Forms through WP filter

This type of work is new to me, so please be patient if the answer is something easy.
I'm using Wordpress and Gravity Forms on my site, and I want to pre-populate the form with data from an object (the object comes from an API, so I can't just use wordpress current_user object).
How can I use an outside variable inside a wordpress filter?
For Example:
$fname = $object->first_name;
add_filter('gform_field_value_firstname', *Populate the field with $fname*);
The below is the normal usage of the function, from Gravity Forms Docs (http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name)
add_filter('gform_field_value_firstname', "dothis");
Where "dothis" points to a function.
The below also works (based on this excellent article: http://www.doitwithwp.com/pre-populate-fields-using-gravity-forms/):
add_filter('gform_field_value_firstname', create_function("", 'return "John";' ));
However, I can't figure out how to get it to accept an outside variable also. For example, I'd like to do:
$fname = $object->first_name;
add_filter('gform_field_value_firstname', create_function("", 'return $fname;' ));
But php tells me that fname is an undefined variable.
I've reviewed this thread PHP: How to make variable visible in create_function()? , but I could not get the Closure solutions to work. I have PHP Version 5.2.17.
Would you please post an example of how to do this correctly?
Make $fname a global variable and you can reference it in your create_function as a global.
global $fname = $object->first_name;
add_filter( 'gform_field_value_firstname', create_function( '', 'global $fname; return $fname;' ) );
However if you have multiple values to return, it's better to make $object global:
global $object;
add_filter( 'gform_field_value_firstname', create_function( '', 'global $object; return
$object->first_name;' ));
add_filter( 'gform_field_value_lastname', create_function( '', 'global $object; return $object->last_name;' ));
And so on...

Laravel link_to attributes aren't working for me..?

I'm using the following...
$params = array('amount' => $transaction_details['AMT']);
echo link_to('/transaction/'.$transaction_details['TRANSACTIONID'].'/refund', 'Refund', $params);
I'm getting my link that goes to the correct URL, but it's not adding URL parameters to it at all, which is what I thought the 3rd attribute would do where I'm passing in the $params array. I can verify that $transaction_details['AMT'] does have a value.
Any info on what I'm doing wrong here would be greatly appreciated. Thanks!
I think you misunderstood the 3rd parameter. The attributes refer to the actual HTML element. For example:
$params = array('class' => 'btn');
echo link_to('/link', 'I am a link', $params);
Would result in:
I am a link
#geevCookie is correct. To add the parameters to your URL you can use the URL helper.
echo link_to(url('/transaction/'.$transaction_details['TRANSACTIONID'].'/refund', $params), 'Refund');

How would I format Zend_Form_Element_Radio so the label comes after the input

I have been trying to implement the solution in an earlier version of this question at:
How would I format Zend_Form_Element_Radio so the label follows the input?
By creating an extended helper MyLib_View_Helper_FormRadio but must be missing something obvious with this!
My question is how do I get Zend_Form_Element_Radio() to use this now instead of the version of the helper in Zend_View_Helper_FormRadio?
I thought initially that this was done by creating the element with
$radio = new MyLib_View_Helper_FormRadio();
but realised its not.
Changing the way the label and input are ordered in the Zend_View_Helper_FormRadio.php does the trick but I realise that the Zend files should not be altered.
If anyone can help me with t his I would be very grateful!
In your MyLib_View_Helper_FormRadio class, you have a method, like this (it's an example):
public function formRadioCustom($name, $value = null, $attribs = null,
$options = null, $listsep = "<br />\n"){...}
So to call it instead of formRadio of Zend_View_Helper_FormRadio, if I'm not mistaken, you have to do:
$element->addDecorators(array(array('ViewHelper',array('helper' => 'formRadioCustom'))));
And in your bootstrap add a method to add helper like this:
protected function _initViewHelpers() {
$view = new Zend_View();
$view->->addHelperPath('MyLib/View/Helper/', 'MyLib_View_Helper');
}

Categories