PHP Variable within Wordpress shortcode - php

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

Related

How to get the title of wordpress post

I know there is a question with similar title but my question is different. How can I get the title of wordpress post. I know the_title() returns AND prints the title of the post. But I don't want it to print. I just want to take it in a PHP variable that I can use later on. Anybody knows?
Look in to the link below which is available in the wordpress docs.
This function will return the title of a post for a given post ID. If the post is protected or private, the word "Protected: " or "Private: " will be prepended to the title. It can be used inside or outside of The Loop. If used outside the loop an ID must be specified.
Quoted from the link below.
https://codex.wordpress.org/Function_Reference/get_the_title
Capture it with the ob_ functions:
ob_start();
the_title()
$title = ob_get_clean();
What this does, is you print it, but only after starting an output buffer, so you print it to that buffer, then get it from the buffer and close the buffer.

Include tag count with Wordpress get_tag_link

I'm using Wordpress, and i'm trying to create an index for a particular part of my site. I'd like to be able to list certain tags (not the entire tag list) with their respective post-count in brackets next to each link.
For example, I can get tag #16 to show as a link using:
tagname
but for the life of me I can't figure out how to get it to show with the post-count next to it, like 'tagname (20)', etc.
I'm sure there's a simple answer (it seems like it should be easy?) but i've googled every permutation of 'get_tag_link with post count' that I can think of and have come up empty.
Any help would really be appreciated!
Kate.
First you need to use get_term_by, then you need to pass in(as the first argument), either the id, slug, or name of the category/term/tag, whatever.
Then you can simply call ->count on your new object.
$term = get_term_by('id', 16, 'post_tag');
Then you can simply call:
$term->count;

How do I mix php inside php strings?

I'm trying to mix <?php echo do_shortcode('[...]') with a field from Advanced Custom Fields within Wordpress.
So basically what I'm trying to do is give the user a text field in the page edit screen where she can paste in the ID of a youtube vide. This field will then update my do_shortcode to display the correct video.
I'm not sure what I'm doing wrong considering I've done this several times before and been succesful. I do have a feeling I'm not escaping the string correctly?
<?php echo do_shortcode('[video_lightbox_youtube video_id="' . the_field("youtube_video") . '" width="640" height="480" anchor="Play Video"]'); ?>
Anyone able to lead me in the right direction? :)
EDIT
The code above returns q_cHD1lcEOo with multiple spaces in front of it as well as this: "Error! You must specify a value for the Video ID, Width, Height parameters to use this shortcode!" That's why I was thinking I'm not escaping it correctly as these are all specified.
I'll add that if I remove the_field("...") and replace it with just an ID it displays perfectly.
SECOND EDIT
Since I was not supposed to echo it, I was using the wrong function to get the field. Instead of using the_field() which prints the value, I was supposed to use get_field() to simply return it to the string.
Your question is somewhat unclear, but I'm also 20 hours without sleep.
Anyways, as far as mixing PHP within a PHP string, there's numerous ways to do it..
You can use concatenation or { } within the string itself.
For example, say we want to echo out the property of an object within a string.
We could do the following
echo "This is my property " . $object->property;
Or, we can do this
echo "This is my property {$object->property}";
You can even do cool things like access associative arrays within strings like so
echo "This is my property {$object->property['cool']}";
Hopefully this leads you in the ride direction.
At first glance it looks like you should be using get_field instead of the_field. the_field will print without being prompted, whereas get_field will return its value, which is what you want.
I see you've also mentioned whitespace at the start, you should consider wrapping the function in trim.
See below:
<?php echo do_shortcode('[video_lightbox_youtube video_id="' . trim(get_field("youtube_video")) . '" width="640" height="480" anchor="Play Video"]'); ?>

How would you combine this wordpress shortcode template code?

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.

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