I am trying to insert PHP code in a shortcode.
For now it look likes this:
<?php echo do_shortcode('[to_like][/to_like]'); ?>
<?php do_action('single_spot_after_content', get_the_ID(), 'after_content'); ?>
I have tried several plugins to combine this to one shortcode (PHP shortcode plugin, Advanced Custom fields, etc.)
This is what I want to accomplish, but it is not working:
<?php echo do_shortcode('[to_like]<?php do_action('single_spot_after_content', get_the_ID(), 'after_content'); ?>
[/to_like]'); ?>
In the front-end the user can select the value through a dropdown field --> for example 10% discount (this is going to be displayed when a visitor likes the box). The theme has a special markup field (you can create custom submission fields) where %% is going to be changed by the chosen value from the customer. This value is going to be displayed on the website (singe listing page).
How can I implement this? This is so vital for the website.
Try this:
<?php echo do_shortcode( '[to_like]' . do_action('single_spot_after_content', get_the_ID(), 'after_content') . '[/to_like]' ); ?>
Related
I'm working on a page template and I'm having trouble working out how I can echo a string of php. Basically I have set up a custom field inside Wordpress for the page called Slider Code, and it asks the user to paste in the slider code given to them so that the appropriate slider is displayed on the page. The slider code given looks like this: [soliloquy id="82"]
The custom field plugin gives me this code to use in the template to echo out whatever is in that field, which looks like this: <?php echo CFS()->get('slider_code'); ?>
In WordPress there is a php function called do_shortcode which enables me to use that code, so it would look like this: <?php do_shortcode('[soliloquy id="82"]'); ?> and the slider would be displayed. So is there any way I can merge that <?php echo CFS()->get('slider_code'); ?> string with the do_Shortcode string? If not, does anyone have any alternative solutions to this issue? I can only think of having to make separate page templates for each individual slider.
EDIT: I have just discovered that the do_shortcode function does not actually work at all with the soliloquy shortcode. It does give me the option of using this code: if ( function_exists( 'soliloquy' ) ) { soliloquy( '82' ); }. However, when I paste this code into the custom field as is, the <?php echo CFS()->get('slider_code'); ?> tag just echos out that text. If I wrap the code in the custom field in <?php and ?> it just comments the code out inside my inspector. How do I get around this?
Check this out.
$slider = CFS()->get('slider_code');
call_user_func('do_shortcode', $slider);
I don't understand why you can't just run do_shortcode(CFS()->get('slider_code')).
Ok. Then try this one. Paste this code in the template file.
<?php
if ( function_exists( 'soliloquy' ) ) {
soliloquy( CFS()->get('slider_code') );
}
?>
And for slider_code input just enter '82'. If you are unable to see what is the template file you have to paste the above given code, then try to see WordPress template conventions.
I have to use some WordPress short-codes for a project - but I want them in very specific places, and not in the main content. I need the client to just put them in specific Custom fields. Using ACF, I built a text field specifically for this.
// normal in-code use
<?php echo do_shortcode('[example_shortcode]'); ?>
// ACF field
<?php the_field('my_main_shortcode'); ?>
// ACF field in the do_shorcode
<?php echo do_shortcode( the_field('my_main_shortcode') ); ?>
This renders out [example_shortcode] on the page instead of what I would expect.
I'm guessing this is a standard PHP thing - and I don't know how to escape or concatenate it properly. EDIT - below using get_fields gets me closer...
THEN... with a more complex short-code
<?php echo do_shortcode( '[tf_listview imagesize="medium1" exclude="calendar-link,list-view-title" dateformat="D n.j"]' ); ?>
It breaks somethings like the image by adding "" - which could totally be the way the short-code was written / but basically - I haven't found a solution to using a short-code in a custom field like this.
Bottom line,
I'd like to write: <?php shit_out('my_annoying_shortcode'); ?>
Or something as close to that as possible. AND have it actually work.
Can anyone give me some more direction?
Since you're using echo, you need to use get_field(), which returns the custom meta value:
<?php echo do_shortcode( get_field('my_main_shortcode') ); ?>
I am working on a WordPress theme. I use a plugin to display pictures on a page (for each post 1 picture), Whenever one of this pictures is clicked the following code registers this and opens a lightbox with the content of the post in it:
<?php
if($_REQUEST['popup']!=''){
$postObj = get_post( $_REQUEST['pid'] );
echo '<div class="ostContent">'.$postObj->post_content.'</div>';
exit;
?>
This all works fine.
Now the problem is that all content get displayed nicely. but for some reason shortcodes don't work. and also when I use a widget in post plugin to display a widget in the post, it doesn't get displayed.
First I tought I needed to enable shortcodes. So I changed this:
echo '<div class="ostContent">'.$postObj->post_content.'</div>';
with this:
echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';
But still nothing. So now I have no idea what to change to make the lightbox show widgets
Hope anyone knows the solution!
EDIT: when I open the post outside the lightbox (by just going to the single page) the shortcode get used like it should be. so somehow the code above don't recognize the shortcode or...
According to a sample here: http://codex.wordpress.org/Function_Reference/do_shortcode
It looks like you need to change: echo '<div class="ostContent">'.do_shortcode( $postObj->post_content ).'</div>';
to:
echo '<div class="ostContent">'.do_shortcode([shortcode_in_brackets]).'</div>';
This should actually display the code. I am assuming that you have defined the actual text in the widget in which the shortcode applies.
Otherwise in the way you currently do it the PHP fires before the post_content even has a value.
I think I understand your problem.
The following should work, assuming the posts in question have a post_type of post:
<?php
// Check for existence of 'popup' & 'pid' query vars
if ( $_REQUEST['popup'] && $_REQUEST['pid'] ) {
// Select single post by ID (using value of the 'pid' query var)
$query = new WP_Query( array ( 'p' => $_REQUEST['pid'] ) );
// Check that the query has returned something
if ($query->have_posts()) {
/* Loop through query until we run out of posts
(should only happen once in this case!) */
while ($query->have_posts()) {
// Setup post, so we can use the_content() and stuff
the_post();
echo '<div class="ostContent">';
/* The part we've been waiting for! the_content() will
display your post content as expected */
the_content();
echo '</div>';
}
}
}
?>
WP_Query is the way to go the majority of the time when needing to retrieve posts: http://codex.wordpress.org/Class_Reference/WP_Query
Your code was retrieving and displaying data almost directly from the WordPress posts table, giving WordPress no chance to apply any of its internal actions and filters (e.g. automatic paragraphs from double line breaks, shortcode execution).
I am trying to incorporate an image slider shortcode into a custom WordPress theme template, so that the client will be able to paste the shortcode into the custom field and the template will read this and display it correctly. I do not want the client to paste the shortcode in the body of the post, as the slider needs to be displayed outside of the post wrapper (at full browser width).
I don't know much about php, so would really appreciate any help with this!
The code I have so far to display the slider via the template is:
<?php echo do_shortcode("[metaslider id=27]"); ?>
And to display the output of custom fields I have:
<?php echo get_post_meta($post->ID, 'slider', true); ?>
Each of these works on its own, but I need to combine them, so that the client doesn't have to edit the template just to add the shortcode. So I am thinking something like this should work:
<?php echo do_shortcode("[<?php echo get_post_meta($post->ID, 'slider', true); ?>]"); ?>
... but it doesn't.
Many thanks in advance for any help with this.
C
Simply pass the return value of get_post_meta (which would contain the shortcode as far as I understood) to the do_shortcode function as an argument:
do_shortcode(get_post_meta($post->ID, 'slider', true));
I have a custom wordpress post.php, all posts within a specific category will use this template. In nearly every post there will be scrolling testimonials, to handle this I am using a layer slider.
Within the page I have a custom field of testimonial that is filled with something like [layreslider id="2"]
In my php file I have:
<div class="trips-testimonials">
<?php do_shortcode(get_post_meta($post->ID, 'testimonial', true)); ?>
</div>
This seems to do nothing. If I add echo to the PHP:
<?php echo do_shortcode(get_post_meta($post-ID, 'testimonial', true)); ?>
I get the output of [layreslider id="2"]
Here is a sample page, the blue box under the photo is where the slider should show up.
http://www.ct-social.com/ctsdev/aff/capri/
Thank you very much for your help.
looks like you miss the > from post Id so it should read
<?php do_shortcode(get_post_meta($post->ID, 'testimonial', true)); ?>
Also you may need to call global $post; if outside the loop.