How would you combine this wordpress shortcode template code? - php

I am using a plugin that is generating my price value and pulling it from an array:
<?php echo __('Price: ', 'event_espresso'); ?></span> <?php echo
$org_options['currency_symbol'].$event->event_cost; ?>
I want to convert this generated value into my visitors own currency depending on the country their in with the plugin 'Worldcurrency'
However you have to input a value in the short code like so:
[worldcurrency curr="EUR" value="25"]
in united states will show:
(~30$ USD)
Now I know how to use a shortcode in a template php file but I don't know if its possible to insert my array value and currency symbol into this shortcode. Rather than using value="25" i need to use:
value="<?php echo $org_options['currency_symbol'].$event->event_cost; ?>"
Is this possible?

I'm not sure whether it's possible to use PHP code inline in a shortcode - and I suspect not, however an easy enough way to implement this would be to write a shortcode of your own that then called the other shortcode.
Your shortcode would generate the text you want (eg '[worldcurrency curr="EUR" value="25"]'), and call "do_shortcode($content)" which would then cause the other plugin to do the currency lookup for you.
You could put this in a plugin file and it would probably amount to less than 15 lines of code.
The other option is to modify the currency conversion plugin you are using to produce the output you want.

Related

Grab text between tag and specific property names and store into PHP array

i have a question, if i want to grab ALL the product names in url http://www.tokopedia.com/lbagstore
in the url above will display all products
i see in the View Source menu product name is between tag
<b itemprop="name"> [product name] <b>
i have PHP script like below:
<?
$html=file_get_contents("https://www.tokopedia.com/lbagstore");
preg_match("'<b itemprop=\"name\">(.*?)</b>'si", $html, $match);
$productname = $match[1];
echo $productname;
?>
but it seems all blank page.
i have difficulty especially to put in array and display them all
does anyone can help me to fix this code? Thanks!
You are "inspecting" the html code of the page instead of "displaying the SOURCE CODE". If you want to extract the data from a website, you need to display its source code, then you can get what you want from it using a regex.
I checked the code myself and there are no <b itemprop="name"> [product name] <b> within the source code, that's why you don't have any results. The only way to see that piece of code was to inspect the code displayed instead of the source code ;)
If you change your code to this, you will be able to see what the real code looks like and then you will be able to adapt your regex to grab the names of the products you want.
$html = "https://www.tokopedia.com/lbagstore";
var_dump($html);
preg_match("'<b itemprop=\"name\">(.*?)</b>'si", $html, $match);
$productname = $match[1];
echo $productname;
Just add the var_dump to display the text. Also if you can't scrap what you want from the website and you need to do it quick, i may recommand you a free google chrome extension called "grepsr" (https://chrome.google.com/webstore/search/grepsr), I tested it and i could extract the names of the products within 5 minuts.
edit : Also if you want to grab the name of all the products in the page you will have to use preg_match_all() instead of preg_match.
I hope this helps ;)

How to combine PHP echo and get together?

My website has multiple languages, and I have a PHP line in below that it get data from sql and it shows text on page:
<?php get_footer_menu_items(3, "col-md-6 go-right","ftitle go-text-right","footerlist go-right go-text-right" );?>
The number 3 in above code is for text (About Us)
But in my page, I have another PHP echo that it show text based on selected language:
<?php echo trans('0295');?>
This above line is for text (About Us)
how can I combine these two lines together that it will change text to selected language?
get_footer_menu_items function probably takes integer as first argument and then as index in an array of some sort with text strings.
So if you call it like this:
get_footer_menu_items(trans('0295'), "col-md-6 go-right","ftitle go-text-right","footerlist go-right go-text-right" );
it won't work anyway.
Without mentioned function's code it's hard to help.

PHP Variable within Wordpress shortcode

I need to use a shortcode but insert the variables the customer enters into the shortcode.
I have tried to use the below:
echo do_shortcode('[su_accordion][su_spoiler title="What is your name and your position in the company?"]'.the_field('whats_your_name_and_your_position_in_the_company').'[/su_spoiler][/su_accordion]');
However it just puts the echo'd text above the accordion shortcode http://prntscr.com/3zzfv9.
Any ideas?
Thanks!
Found the answer here https://wordpress.stackexchange.com/questions/77384/echo-do-shortcode-with-custom-field-not-working
Need to use get_field as opposed to the_field

Generating an HTML color code for event category in ai1ec

Although this question relates to a particular Wordpress plugin called the All in One Event Calender by Time.ly it can also be a general PHP related question.
I am trying to modify a theme to use event colours selected in the ai1ec back end and would like to produce a simple HTML colour code - ie "#f2f2f2"
The plugin itself has loads of functions and php shortcodes to pull a wealth of information off each event such as the one listed below.
<?php echo $event->get_category_text_color(); ?> which will print style="color: #f2a011;"
Can also change to print style="background-color: #f2a011;" with the use of $event->get_category_bg_color();
Now the real meat of the question
All I want to do is get that HTML colour so I can also code it into buttons and other visual elements. I have scoured through blogs and code to try and find something that does it to no avail.
What I was wondering is if you could write a filter of some sort to just take the information within the "#f2f2f2" quotation marks. I know it's not called a filter as searches for php filter return information about something completely different - I'm a self taught PHP programmer and so searching for a lot of terms I don't know can be pretty tough!
As pointed above, substr would be a great solution but it wouldn't solve the case where the color code is expressed in this format:
#FFF;
instead of:
#FFFFFF;
Therefore, this regex should do the job quite well:
'/?=#(.*(?=;)))/'
Example:
$matches = array();
preg_match('/?=#(.*(?=;)))/', $event->get_category_text_color(), $matches);
$colorCode = "#{$matches[0]};";
You could use the substr() function like so:
echo substr($event->get_category_text_color(),14,-2);
Which in the example, would return #f2f2f2.

Wordpress apply get_excerpt formatting to a string

I would like to apply wordpress' get_excerpt format to a string I pass to it.
I know the standard excerpt only works within the loop - but I'm hoping there is a function or something I'm missing that generates the excerpt when you don't manually define one.
Fixed answer:
You need wp_trim_words(). Yes - you're right. wp_trim_excerpt() doesn't play nice with passed strings.
http://codex.wordpress.org/Function_Reference/wp_trim_words
wp_trim_words( "I would like to apply wordpress' get_excerpt format to a string I pass to it. I know the standard excerpt only works within the loop - but I'm hoping there is a function or something I'm missing that generates the excerpt when you don't manually define one.", 5, '[...]' ); returns "I would like to apply[...]"
Previous answer:
WordPress uses wp_trim_excerpt() to generate excerpts.
http://codex.wordpress.org/Function_Reference/wp_trim_excerpt
You probably want some filtering on there too before you output (depending on the source of your text).

Categories