How to echo shortcodes within shortcodes within shortcodes - php

I'm all tied up in knots trying to echo shortcodes within shortcodes and I really need your help please!
Here's the code:
function custom_listify_single_job_listing_actions_after() {
global $post;
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'Contact me';
echo '<div id="my-popup" class="popup"><?php echo do_shortcode('[groups_member group="Active Employer"]<?php echo do_shortcode('[widget id="jmfe_widget-7"]');?>[/groups_member]');?></div>';
}
add_filter( 'listify_single_job_listing_actions_after', 'custom_listify_single_job_listing_actions_after' );
The function adds a button to my site which opens a modal popup when clicked. I'm tying to display content within that popup that is restricted based on membership. That part uses the groups_member shortcode. Then, I'm trying to display content within the groups_member shortcode that belongs to a widget.
Each of the shortcodes works individually - the button, the popup, the restricted content and the widget shortcode - problem is that it doesn't work when I try to put it all together. I've wondered about creating a shortcode that contains the other shortcodes and then somehow inserting that into the popup function, but I'm rubbish at this stuff as it turns out! Can you help please?

I'm not to sure what you are after but echo with php, using string operators would give something like this, which is untested and may be missing a dot or a ' somewhere
echo '<div id="my-popup" class="popup">'.do_shortcode('[groups_member group="Active Employer"]'.do_shortcode('[widget id="jmfe_widget-7"]').'[/groups_member]').'</div>';

Related

Wordpress/Woocommerce- Making a Shortcode a Clickable Link

below is php code that another member posted which allows for a shortcode to be inserted in the short description of every woocommerce product page (code goes into the theme's function.php file). This has worked perfectly for me, but I have one more thing I need to achieve. I assume this is simple but I have primitive knowledge of php:
How do I make the inserted shortcode into a clickable link that links to a url of my choosing? Below is the code the is currently working and needs a hyperlink:
add_action( 'woocommerce_before_add_to_cart_form', 'enfold_customization_extra_product_content', 15 ); function enfold_customization_extra_product_content() {
echo do_shortcode("[wpqr-code]");}
Try:
add_action('woocommerce_before_add_to_cart_form','enfold_customization_extra_product_content', 15);
function enfold_customization_extra_product_content() {
echo ''. do_shortcode("[wpqr-code]") . '';
}

How can I prevent HTML duplication in WordPress website?

I have written some PHP and HTML code to insert social share buttons at the top and bottom of each post.
Since the buttons appear twice on the same page, I had to copy paste the same HTML at two locations.
Is there any way for me to save the HTML code just once at some place and then output it using WordPress shortcodes or anything else like that? I just want to avoid copy pasting the same HTML whenever I need to add share buttons in different places.
Update:
The social media links are generated using the following code (similarly for other networks like Twitter etc.):
<i class="fab fa-facebook-f"></i>
I need a way to get the premalink and title etc. properly from the shortcode function.
Thanks.
Well you can achieve this by using hooks. I use the_content hook here. So you don't need to alter any template system. Create a small function in functions.php in your theme.
function yourtheme_before_after($content) {
global $post;
$socialIcons = 'Facebook';
$socialIcons .= '<a>Twitter</a>';
$socialIcons .= '<a>Pintrest</a>';
$beforecontent = $socialIcons;
$aftercontent = $socialIcons;
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'yourtheme_before_after');
Basically, you append the post content after your custom content, then return the result.
Note: You can modify the social icon part using css/html
EDIT :Added the global $post in the function, you can get pretty much everything related to post.

Place shortcode in php template file

this has been discussed before however it's not working for me. As the shortcode seems to be a mix of regular and open-close shortcode. Or a combination of nested shortcodes.
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
This is the shortcode I would like to display in my php template
[mk_page_section bg_stretch="true" full_width="true" padding_top="0" padding_bottom="0" sidebar="sidebar-1"][vc_column][mk_header hover_styles="5" logo="false"][/vc_column][/mk_page_section]
What I am trying to achieve is display my main navigation on a webinar page that doesn't use my themes header or footer.
Is mit possible to use it like this?
<?php echo do_shortcode('[mk_page_section bg_stretch="true" full_width="true" padding_top="0" padding_bottom="0" sidebar="sidebar-1"][vc_column][mk_header hover_styles="5" logo="false"][/vc_column][/mk_page_section]'); ?>
Or must it be a combination? totally lost here...
Why is the do_shortcode not working?

wordpress. show shortcodes in lightbox plugin

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).

php within php to include wordpress custom field

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));

Categories