Can't seem to nest php variable inside shortcode - nested

I've had a look at various StackOverflow questions but still can't seem to find a successful way to use a variable inside a shortcode. The code in question is as follows (the variable I'm trying to insert into the shortcode is $track_audio.
<?php
$track_audio = get_sub_field('mp3_url');
$renderedShortcode = do_shortcode('[fusion_audio src=". $track_audio . loop="off"][/fusion_audio]');
echo $renderedShortcode; ?>
I've read a fair few conversations on Stackoverflow but have not found a solution that works. The last idea i tried was to concatenate the variable (see code) but that hasn't worked either. Is anyone able to tell me the correct way to do this?
Many thanks in advance for your help with this
Phil

Try this way working for me
Change code from
$renderedShortcode = do_shortcode('[fusion_audio src=". $track_audio . loop="off"][/fusion_audio]');
TO
$renderedShortcode = do_shortcode('[fusion_audio src="'.$track_audio.'" loop="off"][/fusion_audio]');
Final conclision:
$track_audio = get_sub_field('mp3_url');
$renderedShortcode = do_shortcode('[fusion_audio src="'.$track_audio.'" loop="off"][/fusion_audio]');
echo $renderedShortcode;
function fusion_audio_shortcode_func( $atts, $content = null ) {
extract( shortcode_atts(
array(
'loop' => '',
), $atts )
);
$loop;
return '<awesomeness>' . $content . '</awesomeness>';
}
add_shortcode( 'fusion_audio', 'fusion_audio_shortcode_func' );

Related

How does WPML work with replace woocommerce messages?

I'm running a WPML site with woocommerce and have replace some words in woocommerce with code such as:
add_filter( 'wc_add_to_cart_message_html', function ( $message ) {
$text = 'Product added to your cart.';
return sprintf(
'%s %s',
esc_url( wc_get_cart_url() ),
esc_html__( 'View cart', 'woocommerce' ),
esc_html( $text )
);
} );
(courtesy of Christopher, Thank you!)
My question is, how to I translate something like this?
I can only find in the strings the original text:
"%s has been added to your cart." and translating that does not work..
Is there any solutions?
I'd like to change the “%s has been added to your cart." text
while still keeping the "view cart" button there. (Also changed the text there)
Thanks in advance!
steve
Not sure I understand the question. What exactly do you want to translate?
If it's the value of $text, then you should wrap it with a gettext function, then use WPML's auto register strings for translation' functionality to find this string and translate it, because you're using a filter to modify the string (more on this below).
Before:
$text = 'Product added to your cart.';
After:
$text = __( 'Product added to your cart.', 'my-theme-domain' );
More info on how to use WPML's auto register strings for translation' functionality:
In some cases, the static code-scan cannot reliably find all strings. This often happens when strings are generated dynamically using code.
https://wpml.org/documentation/getting-started-guide/string-translation/finding-strings-that-dont-appear-on-the-string-translation-page/#auto-register-strings-for-translation

How do I find any string Perl?

I am new at Perl and I've got a problem.
I would like to find any string in this part of code:
my $change = $item->look_down(
_tag=> 'td',
class => 'colZmiana change ' #?????
);
In my HTML code, there is colZmiana change up, colZmiana change down or colZmiana change.
I would like class => find every colZmiana change.
Could anyone help me ?

modx how to filter out all resources with pagetitle starting on a spesific letter?

I have this resources with pagetitles:
Resource-tree at svalbardflora.net http://svalbardflora.net/modxrev/svalbardflora_net_modxrev_manager_index_php.png
How can I, in getResources, filter out only the documents with pagetitle starting on p or a to b or e to g (and not a container, but I know how to do that part)?
Xpdo query example:
$c = $modx->newQuery('modResource');
$c->where(array(
'pagetitle:REGEXP' => '^([a-b]|[e-g]|p){1}(.*)$'
,'isfolder' => 0
));
$d = $modx->getCollection('modResource',$c);
If you'd like to use this in getResources snippet just add pagetitle and isfolder condition to "where" property like this has been done there
Thanks to #proxyfabio for great help and to guide me in the right direction.
I got the easiest way thanks to #sottwell in the modx forums:
&where=`{"pagetitle:LIKE":"p%"}`
and
&where=`{"pagetitle:LIKE":"l%","OR:pagetitle:LIKE":"m%","OR:pagetitle:LIKE":"n%","OR:pagetitleon:LIKE":"o%"}`
As easy as that.

PHP Simple String Manipulation

I have a string with multiple lines. For this example, my string will be this:
Name:Jaxo
Description:A person on Stackoverflow
Question:$this->questionName();
How could I get just, say, the 'Description'? I need everything after the description, ie 'A person on Stackoverflow'. I've tried a regex like this, but it doesn't work: /^Description:(.+?)\n/i
Any help is much appreciated!
Thanks
-Jaxo
This should work for you:
if (preg_match('/Description:(.+)/im', $subject, $regs)) {
$result = $regs[1];
} else {
$result = "";
}
Where $result is the Description name.
If there is a newline character separating each part of the label you could explode.
$array = explode("\n",$string); // separate params
$desc = explode(":",$array[1]); // separate description
This way you could get any of the parameters.
Try this:
$a="Name:Jaxo
Description:A person on Stackoverflow
Question:\$this->questionName();";
preg_match("/Description:([^\n]+)/i",$a,$m);
print_r($m);
Output:
Array ( [0] => Description:A person on Stackoverflow [1] => A person on Stackoverflow )

preg_replace: reference object in replacement

Do you know of any way to reference an object in the replacement part of preg_replace. I'm trying to replace placeholders (delimited with precentage signs) in a string with the values of attributes of an object. This will be executed in the object itself, so I tried all kinds of ways to refer to $this with the /e modifier. Something like this:
/* for instance, I'm trying to replace
* %firstName% with $this->firstName
* %lastName% with $this->lastName
* etc..
*/
$result = preg_replace( '~(%(.*?)%)~e', "${'this}->{'\\2'}", $template );
I can't get any variation on this theme to work. One of the messages I've been getting is: Can't convert object Model_User to string.
But of course, it's not my intention to convert the object represented by $this to a string... I want to grab the attribute of the object that matches the placeholder (without the percentage signs of course).
I think I'm on the right track with the /e modifier. But not entirely sure about this either. Maybe this can be achieved much more simple?
Any ideas about this? Thank you in advance.
Like I commented to Paul's answer: in the meanwhile I found the solution myself. The solution is much more simple than I thought. I shouldn't have used double quotes.
The solution is as simple as this:
$result = preg_replace( '~(%(.*?)%)~e', '$this->\\2', $template );
Hope this helps anyone else for future reference.
Cheers.
Check out preg_replace_callback - here's how you might use it.
class YourObject
{
...
//add a method like this to your class to act as a callback
//for preg_replace_callback...
function doReplace($matches)
{
return $this->{$matches[2]};
}
}
//here's how you might use it
$result = preg_replace_callback(
'~(%(.*?)%)~e',
array($yourObj, "doReplace"),
$template);
Alternatively, using the /e modifier, you could maybe try this. I think the only way to make it work for your case would be to put your object into global scope
$GLOBALS['yourObj']=$this;
$result = preg_replace( '~(%(.*?)%)~e', "\$GLOBALS['yourObj']->\\2", $template );

Resources